deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
322 stars 79 forks source link

cssselect2.parser.SelectorError on use of `@import url(...)` #285

Closed abathur closed 1 year ago

abathur commented 3 years ago

Just trying out svglib for the first time today and ran into this error. I couldn't find another report, so I figured I should at least document it. I'm not really surprised by this, so I realize it may be a can't/won't-fix.

$ svg2pdf simple-css-case-url.svg 
Rendering failed.
Traceback (most recent call last):
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/bin/.svg2pdf-wrapped", line 133, in <module>
    _main()
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/bin/.svg2pdf-wrapped", line 129, in _main
    svg2pdf(path, outputPat=args.output)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/bin/.svg2pdf-wrapped", line 41, in svg2pdf
    drawing = svglib.svg2rlg(path)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 1217, in svg2rlg
    drawing = svgRenderer.render(svg)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 521, in render
    main_group = self.renderSvg(node, outermost=True)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 638, in renderSvg
    self.renderNode(child, group)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 543, in renderNode
    item = self.renderG(n)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 666, in renderG
    item = self.renderNode(child, parent=gr)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 553, in renderNode
    self.renderStyle(n)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 676, in renderStyle
    self.attrConverter.css_rules = CSSMatcher(node.text)
  File "/nix/store/dzpygvmg8gji0j7awp4id20n9qgmgi5q-python3.6-svglib-0.9.0b0/lib/python3.6/site-packages/svglib/svglib.py", line 155, in __init__
    selectors = cssselect2.compile_selector_list(rule.prelude)
  File "/nix/store/9n18j5c1jxshxa48xx3r9vn26qn7yvyp-python3.6-cssselect2-0.4.1/lib/python3.6/site-packages/cssselect2/compiler.py", line 32, in compile_selector_list
    for selector in parser.parse(input, namespaces)
  File "/nix/store/9n18j5c1jxshxa48xx3r9vn26qn7yvyp-python3.6-cssselect2-0.4.1/lib/python3.6/site-packages/cssselect2/compiler.py", line 31, in <listcomp>
    CompiledSelector(selector)
  File "/nix/store/9n18j5c1jxshxa48xx3r9vn26qn7yvyp-python3.6-cssselect2-0.4.1/lib/python3.6/site-packages/cssselect2/parser.py", line 23, in parse
    yield parse_selector(tokens, namespaces)
  File "/nix/store/9n18j5c1jxshxa48xx3r9vn26qn7yvyp-python3.6-cssselect2-0.4.1/lib/python3.6/site-packages/cssselect2/parser.py", line 36, in parse_selector
    result, pseudo_element = parse_compound_selector(tokens, namespaces)
  File "/nix/store/9n18j5c1jxshxa48xx3r9vn26qn7yvyp-python3.6-cssselect2-0.4.1/lib/python3.6/site-packages/cssselect2/parser.py", line 73, in parse_compound_selector
    % (peek.type if peek else 'EOF'))
cssselect2.parser.SelectorError: (<FunctionBlock url( … )>, 'expected a compound selector, got function')
<!-- simple-css-case-url.svg -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <style type="text/css">
      @import url('https://fonts.googleapis.com/css2?family=Ubuntu+Condensed');
    </style>
  </defs>
</svg>
claudep commented 3 years ago

You may try to report this against cssselect2, but it is known that css support in svglib is still limited, so yes, it's not a surprise.

abathur commented 3 years ago

liZe responded (and closed) in https://github.com/Kozea/cssselect2/issues/22#issuecomment-860178689:

Hello!

The bug is in svg2pdf: it calls compile_selector_list on each rule prelude it finds, but this only works when rules preludes are actually selectors. Here, you have an at-rule (@import), not a selector list with rules, that’s why it crashes.