2022-03-25 13:47:27 +03:00
|
|
|
"""Apply CSS to SVG documents."""
|
2021-04-12 16:49:06 +03:00
|
|
|
|
2021-04-12 18:15:10 +03:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
2021-03-23 17:06:33 +03:00
|
|
|
import cssselect2
|
|
|
|
import tinycss2
|
|
|
|
|
2022-04-28 18:35:25 +03:00
|
|
|
from ..logger import LOGGER
|
2021-03-23 17:06:33 +03:00
|
|
|
from .utils import parse_url
|
|
|
|
|
|
|
|
|
|
|
|
def find_stylesheets_rules(tree, stylesheet_rules, url):
|
2021-04-12 16:49:06 +03:00
|
|
|
"""Find rules among stylesheet rules and imports."""
|
2021-03-23 17:06:33 +03:00
|
|
|
for rule in stylesheet_rules:
|
|
|
|
if rule.type == 'at-rule':
|
|
|
|
if rule.lower_at_keyword == 'import' and rule.content is None:
|
|
|
|
# TODO: support media types in @import
|
|
|
|
url_token = tinycss2.parse_one_component_value(rule.prelude)
|
|
|
|
if url_token.type not in ('string', 'url'):
|
|
|
|
continue
|
2021-04-12 18:15:10 +03:00
|
|
|
css_url = parse_url(urljoin(url, url_token.value))
|
2021-03-23 17:06:33 +03:00
|
|
|
stylesheet = tinycss2.parse_stylesheet(
|
2022-02-14 12:55:47 +03:00
|
|
|
tree.fetch_url(css_url, 'text/css').decode())
|
2021-04-12 18:15:10 +03:00
|
|
|
url = css_url.geturl()
|
|
|
|
yield from find_stylesheets_rules(tree, stylesheet, url)
|
2021-03-23 17:06:33 +03:00
|
|
|
# TODO: support media types
|
|
|
|
# if rule.lower_at_keyword == 'media':
|
2021-04-12 16:49:06 +03:00
|
|
|
elif rule.type == 'qualified-rule':
|
2021-03-23 17:06:33 +03:00
|
|
|
yield rule
|
|
|
|
# TODO: warn on error
|
|
|
|
# if rule.type == 'error':
|
|
|
|
|
|
|
|
|
|
|
|
def parse_declarations(input):
|
2021-04-12 16:49:06 +03:00
|
|
|
"""Parse declarations in a given rule content."""
|
2021-03-23 17:06:33 +03:00
|
|
|
normal_declarations = []
|
|
|
|
important_declarations = []
|
2024-03-17 19:31:32 +03:00
|
|
|
for declaration in tinycss2.parse_blocks_contents(input):
|
2021-03-23 17:06:33 +03:00
|
|
|
# TODO: warn on error
|
|
|
|
# if declaration.type == 'error':
|
|
|
|
if (declaration.type == 'declaration' and
|
|
|
|
not declaration.name.startswith('-')):
|
|
|
|
# Serializing perfectly good tokens just to re-parse them later :(
|
|
|
|
value = tinycss2.serialize(declaration.value).strip()
|
|
|
|
declarations = (
|
|
|
|
important_declarations if declaration.important
|
|
|
|
else normal_declarations)
|
|
|
|
declarations.append((declaration.lower_name, value))
|
|
|
|
return normal_declarations, important_declarations
|
|
|
|
|
|
|
|
|
|
|
|
def parse_stylesheets(tree, url):
|
2021-04-12 16:49:06 +03:00
|
|
|
"""Find stylesheets and return rule matchers in given tree."""
|
2021-03-23 17:06:33 +03:00
|
|
|
normal_matcher = cssselect2.Matcher()
|
|
|
|
important_matcher = cssselect2.Matcher()
|
2021-04-11 17:13:59 +03:00
|
|
|
|
2021-04-12 16:49:06 +03:00
|
|
|
# Find stylesheets
|
2021-04-11 17:13:59 +03:00
|
|
|
# TODO: support contentStyleType on <svg>
|
|
|
|
stylesheets = []
|
|
|
|
for element in tree.etree_element.iter():
|
2022-06-28 16:57:49 +03:00
|
|
|
# https://www.w3.org/TR/SVG/styling.html#StyleElement
|
2021-04-11 17:13:59 +03:00
|
|
|
if (element.tag == '{http://www.w3.org/2000/svg}style' and
|
|
|
|
element.get('type', 'text/css') == 'text/css' and
|
|
|
|
element.text):
|
|
|
|
# TODO: pass href for relative URLs
|
|
|
|
# TODO: support media types
|
|
|
|
# TODO: what if <style> has children elements?
|
|
|
|
stylesheets.append(tinycss2.parse_stylesheet(
|
|
|
|
element.text, skip_comments=True, skip_whitespace=True))
|
|
|
|
|
2021-04-12 16:49:06 +03:00
|
|
|
# Parse rules and fill matchers
|
2021-04-11 17:13:59 +03:00
|
|
|
for stylesheet in stylesheets:
|
2021-03-23 17:06:33 +03:00
|
|
|
for rule in find_stylesheets_rules(tree, stylesheet, url):
|
|
|
|
normal_declarations, important_declarations = parse_declarations(
|
|
|
|
rule.content)
|
2022-04-28 18:35:25 +03:00
|
|
|
try:
|
|
|
|
selectors = cssselect2.compile_selector_list(rule.prelude)
|
|
|
|
except cssselect2.parser.SelectorError as exception:
|
|
|
|
LOGGER.warning(
|
|
|
|
'Failed to apply CSS rule in SVG rule: %s', exception)
|
|
|
|
break
|
|
|
|
for selector in selectors:
|
2021-03-23 17:06:33 +03:00
|
|
|
if (selector.pseudo_element is None and
|
|
|
|
not selector.never_matches):
|
|
|
|
if normal_declarations:
|
|
|
|
normal_matcher.add_selector(
|
|
|
|
selector, normal_declarations)
|
|
|
|
if important_declarations:
|
|
|
|
important_matcher.add_selector(
|
|
|
|
selector, important_declarations)
|
2021-04-11 17:13:59 +03:00
|
|
|
|
2021-03-23 17:06:33 +03:00
|
|
|
return normal_matcher, important_matcher
|