Commits

Anonymous committed 490db0c

add option keepLeadingZero

  • Participants
  • Parent commits 317193a

Comments (0)

Files changed (2)

src/cssutils/serialize.py

     keepEmptyRules = False
         defines if empty rules like e.g. ``a {}`` are kept in the resulting
         serialized sheet
+    keepLeadingZero = True
+        defines if values between -1 and 1 should omit the 0, like ``.5px``
     keepUnknownAtRules = True
         defines if unknown @rules like e.g. ``@three-dee {}`` are kept in the
         serialized sheet
         self.keepAllProperties = True
         self.keepComments = True
         self.keepEmptyRules = False
+        self.keepLeadingZero = True
         self.keepUnknownAtRules = True
         self.keepUsedNamespaceRulesOnly = False
         self.lineNumbers = False
         self.indent = u''
         self.keepComments = False
         self.keepEmptyRules = False
+        self.keepLeadingZero = False
         self.keepUnknownAtRules = False
         self.keepUsedNamespaceRulesOnly = True
         self.lineNumbers = False
                 elif value.value == int(value.value):
                     # cut off after . which is zero anyway
                     val = unicode(int(value.value))
+                elif not self.prefs.keepLeadingZero and -1 < value.value < 1:
+                    v = unicode(value.value)
+                    val = v
+                    if value._sign == u'-':
+                        val = v[0] + v[2:]
+                    else:
+                        val = v[1:]
                 else:
                     val = unicode(value.value)
 

src/tests/test_serialize.py

         self.assertEqual(cssutils.ser.prefs.keepAllProperties, True)
         self.assertEqual(cssutils.ser.prefs.keepComments, True)
         self.assertEqual(cssutils.ser.prefs.keepEmptyRules, False)
+        self.assertEqual(cssutils.ser.prefs.keepLeadingZero, True)
         self.assertEqual(cssutils.ser.prefs.keepUnknownAtRules, True)
         self.assertEqual(cssutils.ser.prefs.keepUsedNamespaceRulesOnly, False)
         self.assertEqual(cssutils.ser.prefs.lineNumbers, False)
     }'''
         s = cssutils.parseString(css)
         self.assertEqual(s.cssText, parsedcss)
+        
+        tests = {
+            u'0.1 .1 0.1px .1px 0.1% .1% +0.1 +.1 +0.1px +.1px +0.1% +.1% -0.1 -.1 -0.1px -.1px -0.1% -.1%': 
+            u'0.1 0.1 0.1px 0.1px 0.1% 0.1% +0.1 +0.1 +0.1px +0.1px +0.1% +0.1% -0.1 -0.1 -0.1px -0.1px -0.1% -0.1%' 
+        }
+        cssutils.ser.prefs.useDefaults()
+        for test, exp in tests.items():
+            s = cssutils.parseString(u'a{x:%s}' % test)
+            self.assertEqual(u'a {\n    x: %s\n    }' % exp, s.cssText)
+
 
     def test_useMinified(self):
         "Preferences.useMinified()"
         self.assertEqual(cssutils.ser.prefs.keepAllProperties, True)
         self.assertEqual(cssutils.ser.prefs.keepComments, False)
         self.assertEqual(cssutils.ser.prefs.keepEmptyRules, False)
+        self.assertEqual(cssutils.ser.prefs.keepLeadingZero, False)
         self.assertEqual(cssutils.ser.prefs.keepUnknownAtRules, False)
         self.assertEqual(cssutils.ser.prefs.keepUsedNamespaceRulesOnly, True)
         self.assertEqual(cssutils.ser.prefs.lineNumbers, False)
             u'a  0px  a  .0px  a  0.0px  a  -0px  a  -.0px  a  -0.0px  a  +0px  a  +.0px  a  +0.0px ': 
                 u'a 0 a 0 a 0 a 0 a 0 a 0 a 0 a 0 a 0', 
             u'a  1  a  .1  a  1.0  a  0.1  a  -1  a  -.1  a  -1.0  a  -0.1  a  +1  a  +.1  a  +1.0': 
-                u'a 1 a 0.1 a 1 a 0.1 a -1 a -0.1 a -1 a -0.1 a +1 a +0.1 a +1',
+                u'a 1 a .1 a 1 a .1 a -1 a -.1 a -1 a -.1 a +1 a +.1 a +1',
             u'  url(x)  f()': 'url(x) f()', 
             u'#112233': '#123', 
             u'#112234': '#112234', 
             u'#123': '#123', 
             u'#123 url() f()': '#123 url() f()',
-            u'1 +2 +3 -4': u'1 +2 +3 -4' # ?  
+            u'1 +2 +3 -4': u'1 +2 +3 -4', # ?
+            u'0.1 .1 0.1px .1px 0.1% .1% +0.1 +.1 +0.1px +.1px +0.1% +.1% -0.1 -.1 -0.1px -.1px -0.1% -.1%': 
+            u'.1 .1 .1px .1px .1% .1% +.1 +.1 +.1px +.1px +.1% +.1% -.1 -.1 -.1px -.1px -.1% -.1%' 
         }
         for test, exp in valuetests.items():
             s = cssutils.parseString(u'a{x:%s}' % test)
             self.assertEqual(u'a{x:%s}' % exp, s.cssText)
+
+        
             
     def test_defaultAtKeyword(self):
         "Preferences.defaultAtKeyword"