mirror of
https://github.com/tcsenpai/Zundler.git
synced 2025-06-06 19:45:27 +00:00
Improve CSS embedding
This commit is contained in:
parent
d5b30f9f52
commit
e05de76bad
@ -202,10 +202,24 @@ def to_data_uri(filename, mime_type=None):
|
|||||||
|
|
||||||
|
|
||||||
def embed_css_resources(css, filename):
|
def embed_css_resources(css, filename):
|
||||||
"""Replace url(<path>) with url(data:<mime_type>;base64, ...)"""
|
"""Replace url(<path>) with url(data:<mime_type>;base64, ...)
|
||||||
# This uses some heuristics which could technically fail
|
|
||||||
|
Also, handle @import."""
|
||||||
|
# This uses some heuristics which will fail in general.
|
||||||
|
# Eventually a library like tinycss2 might be preferable.
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
# First, make sure all @import's are using url(), because these are both valid:
|
||||||
|
# @import url("foo.css");
|
||||||
|
# @import "foo.css";
|
||||||
|
regex = rb'''(?P<rule>@import\s*['"]?(?P<url>.*?)['"]?\s*;)'''
|
||||||
|
replace_rules = {}
|
||||||
|
for m in re.finditer(regex, css, flags=re.IGNORECASE):
|
||||||
|
if not m['url'].lower().startswith(b'url('):
|
||||||
|
replace_rules[m['rule']] = b"@import url('%s');" % m['url']
|
||||||
|
for orig, new in replace_rules.items():
|
||||||
|
css = css.replace(orig, new)
|
||||||
|
|
||||||
# Quotes are optional. But then URLs can contain escaped characters.
|
# Quotes are optional. But then URLs can contain escaped characters.
|
||||||
regex = (
|
regex = (
|
||||||
rb'''(?P<url_statement>url\(['"]?(?P<url>.*?)['"]?\))'''
|
rb'''(?P<url_statement>url\(['"]?(?P<url>.*?)['"]?\))'''
|
||||||
@ -214,7 +228,7 @@ def embed_css_resources(css, filename):
|
|||||||
|
|
||||||
replace_rules = {}
|
replace_rules = {}
|
||||||
|
|
||||||
for m in re.finditer(regex, css):
|
for m in re.finditer(regex, css, flags=re.IGNORECASE):
|
||||||
if re.match(b'''['"]?data:.*''', m['url']):
|
if re.match(b'''['"]?data:.*''', m['url']):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user