Commit 59865e13 by Sander Steffann Committed by Timothée Peignier

Fix python3 bug when inserting data-URIs

The current code inserts bytes into a string. In Python 3 those are different types, causing the URI to look like:

    url("data:image/png;charset=utf-8;base64,b'iVBORw0KGgoAAAANSUhEUgAAAA...'")

Because Base64 data is always ASCII it is safe to decode it as such, and insert the resulting string, which causes the correct output:

    url("data:image/png;charset=utf-8;base64,iVBORw0KGgoAAAANSUhEUgAAAA...")
parent 47eaf9d6
......@@ -177,7 +177,7 @@ class Compressor(object):
if path in self.__class__.asset_contents:
return self.__class__.asset_contents[path]
data = self.read_bytes(path)
self.__class__.asset_contents[path] = base64.b64encode(data)
self.__class__.asset_contents[path] = force_text(base64.b64encode(data))
return self.__class__.asset_contents[path]
def mime_type(self, path):
......
......@@ -46,12 +46,21 @@ class CompressorTest(TestCase):
@patch.object(base64, 'b64encode')
def test_encoded_content(self, mock):
self.compressor.asset_contents.clear()
self.compressor.encoded_content(_('pipeline/images/arrow.png'))
self.assertTrue(mock.called)
mock.reset_mock()
self.compressor.encoded_content(_('pipeline/images/arrow.png'))
self.assertFalse(mock.called)
def test_encoded_content_output(self):
self.compressor.asset_contents.clear()
encoded = self.compressor.encoded_content(_('pipeline/images/arrow.png'))
expected = ('iVBORw0KGgoAAAANSUhEUgAAAAkAAAAGCAYAAAARx7TFAAAAMk'
'lEQVR42oXKwQkAMAxC0Q7rEk5voSEepCHC9/SOpLV3JPULgArV'
'RtDIMEEiQ4NECRNdciCfK3K3wvEAAAAASUVORK5CYII=')
self.assertEqual(encoded, expected)
def test_relative_path(self):
relative_path = self.compressor.relative_path("images/sprite.png", 'css/screen.css')
self.assertEqual(relative_path, '../images/sprite.png')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment