Repository: sprymix/csscompressor

Issue:
Major breakage when using some calc statements
I noticed csscompressor completely breaks some css rules with + in them. For example:

`>>> csscompressor.compress("calc( (10vh - 100px) + 30px )");
'calc( (10vh - 100px)+30px)'

> > > csscompressor.compress("calc( (10vh - 100px) / 4 + 30px )");
> > > 'calc( (10vh - 100px) / 4+30px)'
> > > `

The + and - operators must always be surrounded by whitespace, according to the [spec](https://developer.mozilla.org/en-US/docs/Web/CSS/calc), and when compressed, this rule is now broken and doesn't work in the browser.

Considering calc should be well-supported by just about every browser updated in the last 4 years, calc is super handy... but unfortunately breaks if you decide to compress with csscompress.


Comments:

Patch:
diff --git a/csscompressor/__init__.py b/csscompressor/__init__.py
index e1ae16b..e34af04 100644
--- a/csscompressor/__init__.py
+++ b/csscompressor/__init__.py
@@ -113,6 +113,7 @@ def _preserve_call_tokens(css, regexp, preserved_tokens, remove_ws=False):
     max_idx = len(css) - 1
     append_idx = 0
     sb = []
+    nest_term = None
 
     for match in regexp.finditer(css):
         name = match.group(1)
@@ -121,17 +122,29 @@ def _preserve_call_tokens(css, regexp, preserved_tokens, remove_ws=False):
         term = match.group(2) if match.lastindex > 1 else None
         if not term:
             term = ')'
+            nest_term = '('
 
         found_term = False
         end_idx = match.end(0) - 1
+        nest_idx = end_idx if nest_term else 0
+        nested = False
         while not found_term and (end_idx + 1) <= max_idx:
+            if nest_term:
+                nest_idx = css.find(nest_term, nest_idx + 1)
             end_idx = css.find(term, end_idx + 1)
 
             if end_idx > 0:
+                if nest_idx > 0 and nest_idx < end_idx and \
+                                css[nest_idx - 1] != '\\':
+                    nested = True
+
                 if css[end_idx - 1] != '\\':
-                    found_term = True
-                    if term != ')':
-                        end_idx = css.find(')', end_idx)
+                    if nested:
+                        nested = False
+                    else:
+                        found_term = True
+                        if term != ')':
+                            end_idx = css.find(')', end_idx)
             else:
                 raise ValueError('malformed css')
 
@@ -139,7 +152,7 @@ def _preserve_call_tokens(css, regexp, preserved_tokens, remove_ws=False):
 
         assert found_term
 
-        token = css[start_idx:end_idx]
+        token = css[start_idx:end_idx].strip()
 
         if remove_ws:
             token = _ws_re.sub('', token)


Test patch:
diff --git a/csscompressor/tests/test_compress.py b/csscompressor/tests/test_compress.py
index 8d11e01..e65768a 100644
--- a/csscompressor/tests/test_compress.py
+++ b/csscompressor/tests/test_compress.py
@@ -52,3 +52,16 @@ class Tests(unittest.TestCase):
             a {content: calc(10px-10%}
         '''
         self.assertRaises(ValueError, compress, input)
+
+    def test_nested_1(self):
+        input = '''
+            a { width: calc( (10vh - 100px) / 4 + 30px ) }
+        '''
+        output = compress(input)
+        assert output == "a{width:calc((10vh - 100px) / 4 + 30px)}"
+
+    def test_nested_2(self):
+        input = '''
+            a { width: calc( ((10vh - 100px) / 4 + 30px ) }
+        '''
+        self.assertRaises(ValueError, compress, input)