Skip to content

SVG: apply CSS rules with id, compound and descendant selectors - #2201

Open
RavSinghChandan wants to merge 9 commits into
py-pdf:masterfrom
RavSinghChandan:svg-css-descendant-selectors
Open

RavSinghChandan wants to merge 9 commits into
py-pdf:masterfrom
RavSinghChandan:svg-css-descendant-selectors

Conversation

@RavSinghChandan

Copy link
Copy Markdown

Mermaid styles flowcharts with rules such as #mermaid-svg .node rect { fill: #ECECFF; stroke: #9370DB }. fpdf2 only reads class, element and * selectors, so these rules are ignored and every node of a Mermaid flowchart is drawn as a black box.

Selectors made of element names, ids and classes, joined by descendant or child combinators (#m .node rect, rect#a.b, g.node > rect), now apply. The rules of an element are applied by specificity, then in order, as in CSS; class, element and * rules behave as before. Selectors with pseudo-classes, attribute selectors or sibling combinators are still ignored. What is known about each element is kept, so matching takes linear time in the number of elements, even for a long selector over deeply nested groups.

test_svg_css_selectors_with_ids_and_combinators and test_svg_css_descendant_selectors_linear_time cover these; they fail without the change. None of the 258 SVGs with a <style> I tried fails where it did not before.

@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 found two small cascade cases worth covering before merging:

  • *.a { fill: red } .a { fill: blue } currently renders red. These selectors have equal specificity, so the later rule should win.
  • .a.a { fill: red } *.a { fill: blue } currently renders blue. Repeated classes count toward specificity, so .a.a should win here.
    Could you preserve the original stylesheet order across all rule types and count class occurrences separately from the set used for matching?

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, fixed. All rules now keep their position in the stylesheets, whatever their selector type, so with equal specificity the later rule wins: *.a { fill: red } .a { fill: blue } is blue. Specificity now counts every class, so .a.a beats *.a, while matching still uses the set of classes. Your two cases and their reverse order are in the tests.

1 similar comment
@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, fixed. All rules now keep their position in the stylesheets, whatever their selector type, so with equal specificity the later rule wins: *.a { fill: red } .a { fill: blue } is blue. Specificity now counts every class, so .a.a beats *.a, while matching still uses the set of classes. Your two cases and their reverse order are in the tests.

@andersonhc

Copy link
Copy Markdown
Collaborator

Thanks, the specificity and rule-order fixes look good. I found one remaining case: descendant and child selectors don’t apply inside pattern tiles. Pattern resolution deep-copies the elements, but _pattern_parents only contains the original nodes, so selectors such as pattern rect, #p rect, and g > rect fail to match.

Could you preserve ancestor information when resolving patterns?

Pattern resolution deep-copies the pattern, and only the original elements had a
parent, so pattern rect, #p rect or g > rect did not match inside a tile. The
copy now has the ancestors of the pattern and its children have it as parent.
@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, fixed. Pattern resolution deep-copies the pattern, and only the original elements had a parent, so selectors with ancestors did not match inside a tile. The copy now has the pattern's ancestors and its children have it as parent, so pattern rect, #p rect, g > rect, pattern > rect and defs rect apply, also for a pattern that refers to another by href. Tests cover them and the cases that must not match; they fail without the change.

This branch has not been deployed

No deployments
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