Skip to content

Commit

Permalink
Merge pull request #852 from webcompat/fixmyurl
Browse files Browse the repository at this point in the history
Handle ?url query string for css-fixme, #847
  • Loading branch information
Mike Taylor committed Dec 17, 2015
2 parents 0312d81 + 0471936 commit 84b8bc5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ ua-parser==0.5.0
nose==1.3.1
blinker==1.3
pep8==1.6.2
requests==2.2.1
12 changes: 12 additions & 0 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,17 @@ def test_rate_limit(self):
self.assertEqual(rv.status_code, 200)
self.assertTrue = rv.data.startswith(response_start)

def test_tools_cssfixme_with_URL(self):
'''Test that the /tools/cssfixme route gets 200 with ?url query.'''
url = '/tools/cssfixme?url=https://webcompat.com/css/webcompat.min.css'
rv = self.app.get(url)
self.assertEqual(rv.status_code, 200)

def test_tools_cssfixme_with_nonsense_URL(self):
'''Test that the /tools/cssfixme route gets 200 with bad ?url query.'''
rv = self.app.get('/tools/cssfixme?url=foobar')
self.assertEqual(rv.status_code, 200)


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion webcompat/templates/cssfixme.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<p>This uses the <a href="https://github.com/reworkcss/rework">ReworkCSS</a> library for parsing and stringifying CSS.</p>
<p>Paste CSS in this box to see what needs fixing:</p>

<textarea style="width:100%;height:20rem;"></textarea><br/>
<textarea style="width:100%;height:20rem;">{{ csscode }}</textarea><br/>
<button type="button" id="btn_do_fixup">Add standard equivalents for prefixed CSS</button>
<label>
<input type="checkbox" name="compact">Compact output (whitespace only after closing brace)</label>
Expand Down
22 changes: 21 additions & 1 deletion webcompat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import json
import urllib

import requests

from flask.ext.github import GitHubError
from flask import flash
from flask import g
Expand Down Expand Up @@ -275,7 +277,25 @@ def contributors():
@app.route('/tools/cssfixme')
def cssfixme():
'''Route for CSS Fix me tool'''
return render_template('cssfixme.html')

url = request.args.get('url')
csscode = ''
req = None
if url:
try:
req = requests.get(url)
except requests.exceptions.MissingSchema:
# Somebody gave us an URL not prefixed by http(s)://
try:
req = requests.get('http://{0}'.format(url))
except Exception:
pass # Still failing - TEXTARE will be empty.
except Exception:
pass # If we can't load this URL, the TEXTAREA loads empty.
if req:
# No escaping of HTML required, render_template takes care of it
csscode = req.text
return render_template('cssfixme.html', csscode=csscode)


@app.errorhandler(GitHubError)
Expand Down

0 comments on commit 84b8bc5

Please sign in to comment.