From 042b45f6fdf204a1801fafb259ffe25ba0771be7 Mon Sep 17 00:00:00 2001 From: Jere Malinen Date: Fri, 10 Oct 2014 12:03:16 +0300 Subject: [PATCH] Fix reading UTF-8 files which have BOM https://github.com/django-compressor/django-compressor/issues/567 --- compressor/base.py | 3 +++ compressor/tests/static/css/utf-8_with-BOM.css | 1 + compressor/tests/test_base.py | 8 ++++++++ 3 files changed, 12 insertions(+) create mode 100644 compressor/tests/static/css/utf-8_with-BOM.css diff --git a/compressor/base.py b/compressor/base.py index de9c9ce..ae3a2c4 100644 --- a/compressor/base.py +++ b/compressor/base.py @@ -140,6 +140,9 @@ class Compressor(object): """ Reads file contents using given `charset` and returns it as text. """ + if charset == 'utf-8': + # Removes BOM + charset = 'utf-8-sig' with codecs.open(filename, 'r', charset) as fd: try: return fd.read() diff --git a/compressor/tests/static/css/utf-8_with-BOM.css b/compressor/tests/static/css/utf-8_with-BOM.css new file mode 100644 index 0000000..06a6209 --- /dev/null +++ b/compressor/tests/static/css/utf-8_with-BOM.css @@ -0,0 +1 @@ +.compress-test {color: red;} \ No newline at end of file diff --git a/compressor/tests/test_base.py b/compressor/tests/test_base.py index c6efe5c..d4de9d1 100644 --- a/compressor/tests/test_base.py +++ b/compressor/tests/test_base.py @@ -112,6 +112,14 @@ class CompressorTestCase(SimpleTestCase): hunks = '\n'.join([h for h in self.css_node.hunks()]) self.assertEqual(out, hunks) + def test_css_output_with_bom_input(self): + out = 'body { background:#990; }\n.compress-test {color: red;}' + css = (""" + """) + css_node_with_bom = CssCompressor(css) + hunks = '\n'.join([h for h in css_node_with_bom.hunks()]) + self.assertEqual(out, hunks) + def test_css_mtimes(self): is_date = re.compile(r'^\d{10}[\.\d]+$') for date in self.css_node.mtimes: