Skip to content

Commit 2a92757

Browse files
committed
add preserve_exclamation_comments flag to compress more!
1 parent 399e0a4 commit 2a92757

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

csscompressor/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def _compress_hex_colors(css):
213213
return ''.join(buf)
214214

215215

216-
def _compress(css, max_linelen=0):
216+
def _compress(css, max_linelen=0, preserve_exclamation_comments=True):
217217
start_idx = end_idx = 0
218218
total_len = len(css)
219219

@@ -272,7 +272,7 @@ def _replace(match):
272272

273273
# ! in the first position of the comment means preserve
274274
# so push to the preserved tokens while stripping the !
275-
if token.startswith('!'):
275+
if preserve_exclamation_comments and token.startswith('!'):
276276
preserved_tokens.append(token)
277277
css = css.replace(placeholder, '___YUICSSMIN_PRESERVED_TOKEN_{0}___'.
278278
format(len(preserved_tokens)-1))
@@ -447,7 +447,7 @@ def _apply_preserved(css, preserved_tokens):
447447
return css
448448

449449

450-
def compress(css, max_linelen=0):
450+
def compress(css, max_linelen=0, preserve_exclamation_comments=True):
451451
"""Compress given CSS stylesheet.
452452
453453
Parameters:
@@ -463,14 +463,15 @@ def compress(css, max_linelen=0):
463463
Returns a ``str`` object with compressed CSS.
464464
"""
465465

466-
css, preserved_tokens = _compress(css, max_linelen=max_linelen)
466+
css, preserved_tokens = _compress(css, max_linelen=max_linelen, preserve_exclamation_comments=preserve_exclamation_comments)
467467
css = _apply_preserved(css, preserved_tokens)
468468
return css
469469

470470

471471
def compress_partitioned(css,
472472
max_linelen=0,
473-
max_rules_per_file=4000):
473+
max_rules_per_file=4000,
474+
preserve_exclamation_comments=True):
474475
"""Compress given CSS stylesheet into a set of files.
475476
476477
Parameters:
@@ -490,7 +491,7 @@ def compress_partitioned(css,
490491

491492
assert max_rules_per_file > 0
492493

493-
css, preserved_tokens = _compress(css, max_linelen=max_linelen)
494+
css, preserved_tokens = _compress(css, max_linelen=max_linelen, preserve_exclamation_comments=preserve_exclamation_comments)
494495
css = css.strip()
495496

496497
bufs = []

0 commit comments

Comments
 (0)