Skip to content

Commit

Permalink
Fix svg compress fault due to csscompressor
Browse files Browse the repository at this point in the history
For at least all versions under 0.9.5 of csscompressor, there is a faulty
compression on the url() func of css rules; spaces are wrongly removed,
thus destroying the meaning of any embedded svg file.
See sprymix/csscompressor#9
  • Loading branch information
ysard committed Feb 18, 2022
1 parent d9a4ab0 commit f3d639a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions minification/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Standard imports
import os
from packaging import version
from fnmatch import fnmatch
from codecs import open

Expand All @@ -12,6 +13,25 @@
from pelican import signals


if version.parse(csscompressor.__version__) <= version.parse("0.9.5"):
# Monkey patch csscompressor 0.9.5
_preserve_call_tokens_original = csscompressor._preserve_call_tokens
_url_re = csscompressor._url_re

def my_new_preserve_call_tokens(*args, **kwargs):
"""If regex is for url pattern, switch the keyword remove_ws to False
Such configuration will preserve svg code in url() pattern of CSS file.
"""
if _url_re == args[1]:
kwargs["remove_ws"] = False
return _preserve_call_tokens_original(*args, **kwargs)

csscompressor._preserve_call_tokens = my_new_preserve_call_tokens

assert csscompressor._preserve_call_tokens == my_new_preserve_call_tokens


class Minification(object):
"""
Class that does file content minification.
Expand Down

0 comments on commit f3d639a

Please sign in to comment.