Skip to content

Breaking css minification bug fix: condense_hex_colors breaks 8-digit hex strings #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion css_html_js_minify/css_minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ def condense_hex_colors(css):
"""Shorten colors from #AABBCC to #ABC where possible."""
regex = re.compile(
r"""([^\"'=\s])(\s*)#([0-9a-f])([0-9a-f])([0-9a-f])"""
r"""([0-9a-f])([0-9a-f])([0-9a-f])""", re.I | re.S)
r"""([0-9a-f])([0-9a-f])([0-9a-f])(?![0-9a-f])""", re.I | re.S)
# The above matches a hex string that is 6 digits long, never 8
match = regex.search(css)
while match:
first = match.group(3) + match.group(5) + match.group(7)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_css_minifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from css_html_js_minify import css_minifier

class Test_CSS_Minifier(unittest.TestCase):
def test_css_hex_condense_do_condense(self):
css_in = "color: #aabbcc;"
css_out = "color: #abc;"
self.assertEqual(css_minifier.condense_hex_colors(css_in), css_out)

def test_hex_condense_no_condense_six(self):
css_in = "color: #abcabc;"
css_out = "color: #abcabc;"
self.assertEqual(css_minifier.condense_hex_colors(css_in), css_out)

def test_hex_condense_no_condense_eight(self):
css_in = "color: #aabbccdd;"
css_out = "color: #aabbccdd;"
self.assertEqual(css_minifier.condense_hex_colors(css_in), css_out)

if __name__ == "__main__":
unittest.main()
8 changes: 4 additions & 4 deletions tests/test_html_minifier.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from css_html_js_minify.html_minifier import remove_html_comments

class Test_Html_Minifier(unittest.TestCase):
class Test_HTML_Minifier(unittest.TestCase):
def test_remove_html_comments(self):
html_in = '<p>This is a test</p><!-- Begone -->'
html_out = '<p>This is a test</p>'
html_in = "<p>This is a test</p><!-- Begone -->"
html_out = "<p>This is a test</p>"

self.assertEqual(remove_html_comments(html_in), html_out)

if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()