Merge pull request #198 from Kami/add_css_view

Add CSS view which beautifies minified CSS files
This commit is contained in:
Aldo Cortesi 2014-01-04 14:04:02 -08:00
commit ac31039ad3
5 changed files with 63 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import logging
import re, cStringIO, traceback, json
import urwid
@ -19,6 +20,18 @@ try:
except ImportError: # pragma nocover
pyamf = None
try:
import cssutils
except ImportError: # pragma nocover
cssutils = None
else:
cssutils.log.setLevel(logging.CRITICAL)
cssutils.ser.prefs.keepComments = True
cssutils.ser.prefs.omitLastSemicolon = False
cssutils.ser.prefs.indentClosingBrace = False
cssutils.ser.prefs.validOnly = False
VIEW_CUTOFF = 1024*50
@ -318,7 +331,23 @@ class ViewJavaScript:
opts = jsbeautifier.default_options()
opts.indent_size = 2
res = jsbeautifier.beautify(content[:limit], opts)
return "JavaScript", _view_text(res, len(content), limit)
return "JavaScript", _view_text(res, len(res), limit)
class ViewCSS:
name = "CSS"
prompt = ("css", "c")
content_types = [
"text/css"
]
def __call__(self, hdrs, content, limit):
if cssutils:
sheet = cssutils.parseString(content)
beautified = sheet.cssText
else:
beautified = content
return "CSS", _view_text(beautified, len(beautified), limit)
class ViewImage:
@ -409,6 +438,7 @@ views = [
ViewHTML(),
ViewHTMLOutline(),
ViewJavaScript(),
ViewCSS(),
ViewURLEncoded(),
ViewMultipart(),
ViewImage(),

View File

@ -61,6 +61,10 @@ class HelpView(urwid.ListBox):
common.highlight_key("json", "s") +
[("text", ": JSON")]
),
(None,
common.highlight_key("css", "c") +
[("text", ": CSS")]
),
(None,
common.highlight_key("urlencoded", "u") +
[("text", ": URL-encoded data")]

View File

@ -14,3 +14,4 @@ requests>=1.2.2
urwid>=1.1.1
wsgiref>=0.1.2
jsbeautifier>=1.4.0
cssutils>=1.0,<1.1

1
test/data/1.css Normal file
View File

@ -0,0 +1 @@
body,html{height:100%}body{font-family:'Open Sans',sans-serif;font-size:1.5em;padding-top:80px}

View File

@ -13,6 +13,11 @@ try:
except ImportError:
pyamf = None
try:
import cssutils
except:
cssutils = None
class TestContentView:
def test_trailer(self):
@ -112,6 +117,26 @@ class TestContentView:
assert v([], "[1, 2, 3", 100)
assert v([], "function(a){[1, 2, 3]}", 100)
def test_view_css(self):
v = cv.ViewCSS()
with open('./test/data/1.css', 'r') as fp:
fixture_1 = fp.read()
result = v([], 'a', 100)
if cssutils:
assert len(result[1]) == 0
else:
assert len(result[1]) == 1
result = v([], fixture_1, 100)
if cssutils:
assert len(result[1]) > 1
else:
assert len(result[1]) == 1
def test_view_hex(self):
v = cv.ViewHex()
assert v([], "foo", 1000)