diff --git a/css_html_js_minify/css_minifier.py b/css_html_js_minify/css_minifier.py index 1bb7c17..5599dbf 100644 --- a/css_html_js_minify/css_minifier.py +++ b/css_html_js_minify/css_minifier.py @@ -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) diff --git a/tests/test_css_minifier.py b/tests/test_css_minifier.py new file mode 100644 index 0000000..f4ec775 --- /dev/null +++ b/tests/test_css_minifier.py @@ -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() \ No newline at end of file diff --git a/tests/test_html_minifier.py b/tests/test_html_minifier.py index 9bb788a..cdc13c0 100644 --- a/tests/test_html_minifier.py +++ b/tests/test_html_minifier.py @@ -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 = '
This is a test
' - html_out = 'This is a test
' + html_in = "This is a test
" + html_out = "This is a test
" self.assertEqual(remove_html_comments(html_in), html_out) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() \ No newline at end of file