From 56008d1204d88458ab81aeef8a63714bd490eeaa Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 8 Oct 2013 15:42:35 -0400 Subject: [PATCH] Fix issue 59 (preserve whitespace in "call" function) --- csscompressor/__init__.py | 21 +++++++++++++-------- csscompressor/tests/test_other.py | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/csscompressor/__init__.py b/csscompressor/__init__.py index 9afe489..9f1ec07 100644 --- a/csscompressor/__init__.py +++ b/csscompressor/__init__.py @@ -18,6 +18,7 @@ import re _url_re = re.compile(r'''url\s*\(\s*(['"]?)data\:''', re.I) +_calc_re = re.compile(r'calc\s*\(', re.I) _ws_re = re.compile(r'\s+') _str_re = re.compile(r'''("([^\\"]|\\.|\\)*")|('([^\\']|\\.|\\)*')''') _yui_comment_re = re.compile(r'___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_(?P\d+)___') @@ -100,15 +101,15 @@ _colors_map = { _colors_re = re.compile(r'(:|\s)' + '(\\#(' + '|'.join(_colors_map.keys()) + '))' + r'(;|})', re.I) -def _extract_data_urls(css, preserved_tokens): +def _preserve_call_tokens(css, name, regexp, preserved_tokens, *, remove_ws=False): max_idx = len(css) - 1 append_idx = 0 sb = [] - for match in _url_re.finditer(css): - start_idx = match.start(0) + 4 # "len" of "url(" - term = match.group(1) + for match in regexp.finditer(css): + start_idx = match.start(0) + len(name) + 1 # "len" of "url(" + term = match.group(1) if match.lastindex else None if not term: term = ')' @@ -126,9 +127,13 @@ def _extract_data_urls(css, preserved_tokens): if found_term: token = css[start_idx:end_idx] - token = _ws_re.sub('', token) - preserver = 'url(___YUICSSMIN_PRESERVED_TOKEN_{}___)'.format(len(preserved_tokens)) + if remove_ws: + token = _ws_re.sub('', token) + + preserver = ('{}(___YUICSSMIN_PRESERVED_TOKEN_{}___)' + .format(name, len(preserved_tokens))) + preserved_tokens.append(token) sb.append(preserver) @@ -198,7 +203,6 @@ def _compress_hex_colors(css): buf.append('#' + (match.group(2) + match.group(3) + match.group(4) + match.group(5) + match.group(6) + match.group(7)).lower()) - index = match.end(7) buf.append(css[index:]) @@ -211,7 +215,8 @@ def compress(css, *, max_linelen=0): total_len = len(css) preserved_tokens = [] - css = _extract_data_urls(css, preserved_tokens) + css = _preserve_call_tokens(css, 'url', _url_re, preserved_tokens, remove_ws=True) + css = _preserve_call_tokens(css, 'calc', _calc_re, preserved_tokens, remove_ws=False) # Collect all comments blocks... comments = [] diff --git a/csscompressor/tests/test_other.py b/csscompressor/tests/test_other.py index 6c9e785..ceded1a 100644 --- a/csscompressor/tests/test_other.py +++ b/csscompressor/tests/test_other.py @@ -25,3 +25,19 @@ class Tests(BaseTest): output = '''table thead tr td{color:#cedb00;padding:.5em 0 1.0em 0;text-transform:uppercase;vertical-align:bottom}''' self._test(input, output) + + def test_issue_59(self): + # https://github.com/yui/yuicompressor/issues/59 + + input = ''' + .issue-59 { + width:100%; + width: -webkit-calc(100% + 30px); + width: -moz-calc(100% + 30px); + width: calc(100% + 30px); + } + ''' + + output = '''.issue-59{width:100%;width:-webkit-calc(100% + 30px);width:-moz-calc(100% + 30px);width:calc(100% + 30px)}''' + + self._test(input, output)