Skip to content

Proper CSS selectors implementation

What does the merge request do?

Speedups to make style lookups faster. In particular, a proper CSS implementation that isn't unusably slow for large documents. Also, the amount of string operations during element construction has been reduced by restructuring the parser (also helps quite a bit for large documents, because LXML doesn't cache the created elements when iterating the document tree - as soon as they go out of scope they get destroyed). The getElementById method now uses a direct iterator instead of an xpath, which is weirdly slow in this case.

In my testing, the execution time for lookup_specificity in test_display_is_visible reduced from 8.55 seconds to 0.00301 seconds.

Partial fix for #548 (closed) and #446 (closed). Remaining: Caching of the style sheets in the document, which is up next. But this is independent, so this change can be reviewed already.

This adds tinycss2 as a depencency. I needed to fully fork cssselect2 because it is far too old in the Ubuntu repos and doesn't even exist on msys2. tinycss2 doesn't have such problems.

Implementation notes

The outwards-facing API hasn't changed except that ConditionalStyle.to_xpath is deprecated and will be removed in 1.4, together with the dependency on cssselect.

The change in ink2canvas reflects the fact that previously IDs were generated in the style lookup, and the random generator yielded different results.

Summary for release notes

Style evaluations in documents that contain CSS have been sped up dramatically. For extensions authors, no need to change anything. Also, complex CSS4 selectors (like :has) are now supported.

Checklist

  • Add unit tests (if applicable)
  • Changes to inkex/ are well documented
  • Clean merge request history
Edited by Jonathan Neuhauser

Merge request reports