Permalink
Browse files

Minification: Specify colors by name if the name is shorter

  • Loading branch information...
1 parent 573c94b commit 0f96c00b146a9fd4965fb573fcaa551fcd6abc46 @encukou encukou committed Oct 16, 2011
Showing with 24 additions and 5 deletions.
  1. +12 −3 clevercss/expressions.py
  2. +1 −1 tests/mediatype.py
  3. +11 −1 tests/minify.py
View
@@ -353,10 +353,19 @@ def div(self, other, context):
return Literal.div(self, other, context)
def to_string(self, context):
- if context.minified and all(x >> 4 == x & 15 for x in self.value):
- return '#%x%x%x' % tuple(x & 15 for x in self.value)
code = '#%02x%02x%02x' % self.value
- return self.from_name and consts.REV_COLORS.get(code) or code
+ if not context.minified:
+ return self.from_name and consts.REV_COLORS.get(code) or code
+ else:
+ if all(x >> 4 == x & 15 for x in self.value):
+ min_code = '#%x%x%x' % tuple(x & 15 for x in self.value)
+ else:
+ min_code = code
+ name = consts.REV_COLORS.get(code)
+ if name and len(name) < len(min_code):
+ return name
+ else:
+ return min_code
def _calc(self, other, method):
is_number = isinstance(other, Number)
View
@@ -190,7 +190,7 @@ def test_06_minimal_media_type(self):
text-decoration:none}
}
a{
- color:#f00}
+ color:red}
@media handheld{
td{
background-color:green}
View
@@ -10,7 +10,7 @@ class MinifiedConvertTestCase(TestCase):
def test_01_min_convert(self):
self.assertEqual(convert('''body:
color: $color
- ''',{'color':'#eee'}, minified=True),
+ ''',{'color':'#eeeeee'}, minified=True),
u'body{color:#eee}')
def test_02_min_convert(self):
@@ -19,6 +19,16 @@ def test_02_min_convert(self):
''', {'background_color': 'red.darken(10)'}, minified=True),
u'body{background-color:#c00}')
+ def test_02_min_convert_colors(self):
+ self.assertEqual(convert('''body:
+ background-color: #ffff00
+ color: #fffafa
+ p:
+ background-color: #ff0000
+ color: khaki
+ ''', minified=True),
+ u'body{background-color:#ff0;color:snow}body p{background-color:red;color:khaki}')
+
def all_tests():
return unittest.TestSuite(case.toSuite() for case in [MinifiedConvertTestCase])

0 comments on commit 0f96c00

Please sign in to comment.