-
Notifications
You must be signed in to change notification settings - Fork 9
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
Incorrect removal of whitespace in svg #9
Comments
For those wondering how to get around this issue, I'm currently using the following workaround: import re
from csscompressor import compress as _compress
capture_svg = re.compile(r'url\("(data:image/svg.*?svg%3E)\"\)')
def compress(css, **kwargs):
svg = re.findall(capture_svg, css)
css = _compress(css, **kwargs)
errors = re.findall(capture_svg, css)
for find, replace in zip(errors, svg):
css = css.replace(find, replace)
return css |
@Paradoxis thanks for the suggested workaround! I've borrowed from this to handle the same issue with rccsmin. One suggestion, the regular expression assumes def compress(css, **kwargs):
capture_svg = re.compile(r'url\("(data:image/svg.*?svg%3[Ee])\"\)')
data_urls = re.findall(capture_svg, css)
for data_url in data_urls:
css = css.replace(data_url, data_url.replace(' ', '%20'))
css = cssmin(css, **kwargs)
return css |
Hi, I purpose another "cleaner" resolution via Monkey patching. The line responsible for the modification in the csscompressor/csscompressor/__init__.py Lines 157 to 158 in 0857438
In the function: csscompressor/csscompressor/__init__.py Line 112 in 0857438
In csscompressor/csscompressor/__init__.py Lines 239 to 242 in 0857438
The first call is what we are interested in because it handles the csscompressor/csscompressor/__init__.py Line 22 in 0857438
...but it wrongly uses the keyword parameter I wonder how the url pattern content can be optimized anyway? The solution here is to wrap the function import csscompressor
# Monkey patch
_preserve_call_tokens_original = csscompressor._preserve_call_tokens
_url_re = csscompressor._url_re
def my_new_preserve_call_tokens(*args, **kwargs):
"""Patch the keyword for url pattern handling in csscompressor"""
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
print("original func", id(_preserve_call_tokens_original))
print("modified func", id(my_new_preserve_call_tokens))
assert csscompressor._preserve_call_tokens == my_new_preserve_call_tokens Another solution much less maintainable and readable is to monkey patch the Abstract Syntax Tree of the function: import ast
import inspect
# Get source code of _compress
source_func = inspect.getsource(csscompressor._compress)
# Parse the source code into syntax tree
ast_func = ast.parse(source_func)
# Patch the keyword
ast_func.body[0].body[3].value.keywords[0].value.value = False
# Get python bytecode
bytecode = compile(ast_func, '<string>', 'exec')
# Inject bytecode in the module
exec(bytecode, csscompressor.__dict__) |
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
* sort imports * Fix svg compress fault due to csscompressor 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 * fix typos with black * Add logger/logging * Ability to minify inline JS & CSS tags jsmin is used for JavaScript minification; csscompressor is used as for css files * fix typos * packaging: add setup.cfg * packaging: drop old setup.py (replaced by setup.cfg) * add version to __init__ * Move src files to new pelican plugins architecture * Drop python2.7 open method * Set pelican 4 dep * Add license text in src file as it should be * README: update; - simplify maintenance process by removing references to specific versions of dependencies (See setup.cfg instead) - Add jsmin dep - Add script & style related minification * Add Makefile for release & test process * Remove useless lambda * Update changelog * README: More complete plugin description * README: mention to beautifulsoup dep * packaging: add beautifulsoup dep * Fix rest typo
@ysard Thanks for the solution! |
* sort imports * Fix svg compress fault due to csscompressor 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 * fix typos with black * Add logger/logging * Ability to minify inline JS & CSS tags jsmin is used for JavaScript minification; csscompressor is used as for css files * fix typos * packaging: add setup.cfg * packaging: drop old setup.py (replaced by setup.cfg) * add version to __init__ * Move src files to new pelican plugins architecture * Drop python2.7 open method * Set pelican 4 dep * Add license text in src file as it should be * README: update; - simplify maintenance process by removing references to specific versions of dependencies (See setup.cfg instead) - Add jsmin dep - Add script & style related minification * Add Makefile for release & test process * Remove useless lambda * Update changelog * README: More complete plugin description * README: mention to beautifulsoup dep * packaging: add beautifulsoup dep * Fix rest typo
* sort imports * Fix svg compress fault due to csscompressor 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 * fix typos with black * Add logger/logging * Ability to minify inline JS & CSS tags jsmin is used for JavaScript minification; csscompressor is used as for css files * fix typos * packaging: add setup.cfg * packaging: drop old setup.py (replaced by setup.cfg) * add version to __init__ * Move src files to new pelican plugins architecture * Drop python2.7 open method * Set pelican 4 dep * Add license text in src file as it should be * README: update; - simplify maintenance process by removing references to specific versions of dependencies (See setup.cfg instead) - Add jsmin dep - Add script & style related minification * Add Makefile for release & test process * Remove useless lambda * Update changelog * README: More complete plugin description * README: mention to beautifulsoup dep * packaging: add beautifulsoup dep * Fix rest typo
* sort imports * Fix svg compress fault due to csscompressor 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 * fix typos with black * Add logger/logging * Ability to minify inline JS & CSS tags jsmin is used for JavaScript minification; csscompressor is used as for css files * fix typos * packaging: add setup.cfg * packaging: drop old setup.py (replaced by setup.cfg) * add version to __init__ * Move src files to new pelican plugins architecture * Drop python2.7 open method * Set pelican 4 dep * Add license text in src file as it should be * README: update; - simplify maintenance process by removing references to specific versions of dependencies (See setup.cfg instead) - Add jsmin dep - Add script & style related minification * Add Makefile for release & test process * Remove useless lambda * Update changelog * README: More complete plugin description * README: mention to beautifulsoup dep * packaging: add beautifulsoup dep * Fix rest typo
I realize this is a hacky use case, but it'd be neat if it worked, nevertheless.
The text was updated successfully, but these errors were encountered: