Conversion: Fix <style> tags inside <svg> tags not being processed

See #2058798 (unwanted changes of svg code during conversion)
This commit is contained in:
Kovid Goyal 2024-04-01 11:04:55 +05:30
parent b0e899d53c
commit a227d5e3be
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 7 additions and 7 deletions

View File

@ -303,7 +303,7 @@ def rewrite_links(root, link_repl_func, resolve_base_href=False):
except UnicodeDecodeError:
continue
if tag == XHTML('style') and el.text and \
if tag in (XHTML('style'), SVG('style')) and el.text and \
(_css_url_re.search(el.text) is not None or '@import' in
el.text):
stylesheet = parser.parseString(el.text, validate=False)

View File

@ -14,7 +14,7 @@ from tinycss.fonts3 import parse_font_family, serialize_font_family
from calibre import as_unicode
from calibre.ebooks.css_transform_rules import all_properties
from calibre.ebooks.oeb.base import OEB_STYLES, XHTML, css_text
from calibre.ebooks.oeb.base import OEB_STYLES, SVG, XHTML, css_text
from calibre.ebooks.oeb.normalize_css import DEFAULTS, normalizers
from calibre.ebooks.oeb.stylizer import INHERITED, media_ok
from calibre.utils.resources import get_path as P
@ -187,7 +187,7 @@ def resolve_styles(container, name, select=None, sheet_callback=None):
process_sheet(html_css_stylesheet(container), 'user-agent.css')
for elem in root.iterdescendants(XHTML('style'), XHTML('link')):
for elem in root.iterdescendants(XHTML('style'), SVG('style'), XHTML('link')):
if elem.tag.lower().endswith('style'):
if not elem.text:
continue

View File

@ -24,7 +24,7 @@ from tinycss.media3 import CSSMedia3Parser
from calibre import as_unicode, force_unicode
from calibre.ebooks import unit_convert
from calibre.ebooks.oeb.base import CSS_MIME, OEB_STYLES, XHTML, XHTML_NS, urlnormalize, xpath
from calibre.ebooks.oeb.base import CSS_MIME, OEB_STYLES, SVG, XHTML, XHTML_NS, urlnormalize, xpath
from calibre.ebooks.oeb.normalize_css import DEFAULTS, normalizers
from calibre.utils.resources import get_path as P
from polyglot.builtins import iteritems
@ -247,7 +247,7 @@ class Stylizer:
parser = CSSParser(fetcher=self._fetch_css_file,
log=logging.getLogger('calibre.css'))
for elem in style_tags:
if (elem.tag == XHTML('style') and elem.get('type', CSS_MIME) in OEB_STYLES and media_ok(elem.get('media'))):
if (elem.tag in (XHTML('style'), SVG('style')) and elem.get('type', CSS_MIME) in OEB_STYLES and media_ok(elem.get('media'))):
text = elem.text if elem.text else ''
for x in elem:
t = getattr(x, 'text', None)

View File

@ -19,7 +19,7 @@ from lxml import etree
from calibre import guess_type
from calibre.ebooks import unit_convert
from calibre.ebooks.oeb.base import CSS_MIME, OEB_STYLES, SVG_NS, XHTML, XHTML_NS, XPath, barename, css_text, namespace
from calibre.ebooks.oeb.base import CSS_MIME, OEB_STYLES, SVG, SVG_NS, XHTML, XHTML_NS, XPath, barename, css_text, namespace
from calibre.ebooks.oeb.stylizer import Stylizer
from calibre.utils.filenames import ascii_filename, ascii_text
from calibre.utils.icu import numeric_sort_key
@ -590,7 +590,7 @@ class CSSFlattener:
and safe_lower(node.get('rel', 'stylesheet')) == 'stylesheet' \
and safe_lower(node.get('type', CSS_MIME)) in OEB_STYLES:
node.getparent().remove(node)
elif node.tag == XHTML('style') \
elif node.tag in (XHTML('style'), SVG('style')) \
and node.get('type', CSS_MIME) in OEB_STYLES:
node.getparent().remove(node)
href = item.relhref(href)