Skip to content

SVG: read style declarations marked !important - #2172

Merged
andersonhc merged 2 commits into
py-pdf:masterfrom
RavSinghChandan:svg-css-important
Nov 12, 2026
Merged

andersonhc merged 2 commits into
py-pdf:masterfrom
RavSinghChandan:svg-css-important

Conversation

@RavSinghChandan

Copy link
Copy Markdown

A style declaration with !important fails:

SVGObject('<svg xmlns="http://www.w3.org/2000/svg"><rect style="fill:#f9f !important" width="10" height="10"/></svg>')
# ValueError: unsupported color specification #f9f !important

Mermaid writes the style of a node this way: a style A fill:#f9f statement in a diagram gives style="fill:#f9f !important" on its shapes.

The !important flag is now removed from the declarations of style attributes and of <style> rules. As in CSS, an !important declaration of a <style> rule wins over a normal style attribute, and over a later rule for the same class; an !important style attribute still wins.

Added a style with !important case to test_svg_attribute_conversion and test_svg_css_important for the order of the rules and the style attribute; 4 of the 5 cases fail on master. The SVG tests, black, pylint and mypy pass

@andersonhc andersonhc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed one precedence case worth covering: fill:red !important; fill:blue currently renders blue, both in an inline style and inside a class rule. Red should win, but html.parse_css_style() collapses duplicate declarations before the importance checks run.

Could we resolve duplicates while their !important flags are still available and add regression tests for both forms?

html.parse_css_style() keeps the last declaration of a property, so
"fill: red !important; fill: blue" lost red before the importance
checks ran, in a style attribute as in a class rule.
@RavSinghChandan

Copy link
Copy Markdown
Author

Good catch. Duplicate declarations are now resolved while their !important flags are still known (_parse_css_declarations()), so fill: red !important; fill: blue keeps red, both inline and in a class rule. Both are now test cases.

@andersonhc
andersonhc merged commit a1c4044 into py-pdf:master Nov 12, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants