Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions scripts/minify_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,18 @@
import sys
from pathlib import Path

_COMMENT = re.compile(r"/\*.*?\*/", flags=re.DOTALL)
_TOKEN = re.compile(r"/\*.*?\*/" r"|\"(?:\\.|[^\"\\])*\"" r"|'(?:\\.|[^'\\])*'", flags=re.DOTALL)
_TRAILING_WS = re.compile(r"[ \t]+(?=\n)")
_BLANK_RUN = re.compile(r"\n{3,}")
_STRING = re.compile(r'"[^"]*"')


def minify_css(source: str) -> str:
"""Return the stylesheet with comments and blank runs removed."""
# Hold string literals aside so a `/*` inside one is not mistaken for a
# comment (a background-image url could legally contain it).
held: list[str] = []

def _hold(match: re.Match[str]) -> str:
held.append(match.group(0))
return f"__BRCSS_STRING_{len(held) - 1}__"

protected = _STRING.sub(_hold, source)
cleaned = _COMMENT.sub("", protected)
for index, literal in enumerate(held):
cleaned = cleaned.replace(f"__BRCSS_STRING_{index}__", literal)
# Match comments and strings in one pass: quotes inside comments cannot
# hide the terminator, and comment markers inside strings remain literal.
cleaned = _TOKEN.sub(
lambda match: "" if match.group(0).startswith("/*") else match.group(0), source
)
cleaned = _TRAILING_WS.sub("", cleaned)
return _BLANK_RUN.sub("\n\n", cleaned).strip() + "\n"

Expand Down
13 changes: 13 additions & 0 deletions tests/test_minify_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ def test_selectors_and_declarations_are_never_rewritten():
result = minify_css(source)
assert "@media (max-width: 700px)" in result
assert ".a,.b { color: rgb(1 2 3 / 50%); margin: 0 auto; }" in result


def test_cli_preserves_single_quoted_and_escaped_literals(tmp_path):
source = 'x { background: url(\'a/*b*/c.png\'); content: "say \\"/*literal*/\\""; }\n'
path = tmp_path / "styles.css"
path.write_text(source + "/* removable comment */\n")
assert mc.main([str(path)]) == 0
assert path.read_text() == source


def test_quotes_inside_comments_do_not_hide_following_rules():
source = '/* a " quote */\nx { color: red; }\n/* another " quote */\n'
assert minify_css(source) == "x { color: red; }\n"
Loading