From 7d2bdcdc2e6786e323a3e929b79645caf89b9b25 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 8 Oct 2013 15:00:42 -0400 Subject: [PATCH] Implement line breaking ('max_linelen' param of 'compress') --- csscompressor/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/csscompressor/__init__.py b/csscompressor/__init__.py index 6009afb..df1fa3c 100644 --- a/csscompressor/__init__.py +++ b/csscompressor/__init__.py @@ -312,6 +312,7 @@ def compress(css, *, max_linelen=0): css = _spaces2_re.sub(lambda match: match.group(1), css) + # Restore spaces for !important css = css.replace('!important', ' !important'); # bring back the colon @@ -398,6 +399,27 @@ def compress(css, *, max_linelen=0): # Add "\" back to fix Opera -o-device-pixel-ratio query css = css.replace('___YUI_QUERY_FRACTION___', '/') + # Some source control tools don't like it when files containing lines longer + # than, say 8000 characters, are checked in. The linebreak option is used in + # that case to split long lines after a specific column. + if max_linelen and len(css) > max_linelen: + buf = [] + start_pos = 0 + while True: + buf.append(css[start_pos:start_pos + max_linelen]) + start_pos += max_linelen + while start_pos < len(css): + if css[start_pos] == '}': + buf.append('}\n') + start_pos += 1 + break + else: + buf.append(css[start_pos]) + start_pos += 1 + if start_pos >= len(css): + break + css = ''.join(buf) + # Replace multiple semi-colons in a row by a single one # See SF bug #1980989 css = _many_semi_re.sub(';', css)