Skip to content

Respect style inheritance and proper CSS + high-level interface for style manipulation

This merge request implements the CSS specified style for nodes in Inkex.

This means that Inkex respects the specificity of styles, inheritance (limited to the attributes that are actually inherited), the font shorthand attribute, "!important" and "inherit" keywords and uses all style sources (stylesheets, inline style, presentation attributes) to compute the specified style of an element.

For parsing of the CSS rule, computation of the specificity and conversion to XPATH, I added the cssselect module.

Furthermore, this merge request gives attributes an object-oriented structure. They are parsed automatically.

For the interface see doc strings and unit tests. Highlights:


elem : BaseElement

full_style = elem.specified_style() # returns the CSS "specified style" of an object, including inheritance, importance, and pretty much everything CSS2 supports (plus whatever CSS3 csselect supports)

elem.style("fill") # returns a Gradient object if gradient, a Pattern object if pattern, a Color object if color...

elem.style["fill"] = document.getElementById("linearGradient1234")
elem.style["fill"] # returns "url(#linearGradient1234)"
elem.style("fill") # returns LinearGradient object of #linearGradient1234

new_style = Style()

new_style["font"] = "10px Verdana"
new_style("font-family") # returns "Verdana"

elem.style["fill-rule"] # raises KeyError, as the attribute is not defined explicitly within this style
elem.style("fill-rule") # returns "nonzero" since this is the default value of the fill-rule attribute

Edit: removed unfair comparison

Further TODOs:

  • Add correctness unit tests for cascade, "inherit" and "!important"
  • Add unit tests for parsing and value rejection
  • Discuss addition of cssselect
  • Removal of mypy annotations, so it can pass the Python 3.6 tests
  • Open MR removing the mypy check from python 3.6
  • Check whether we can speed up xpath requests by running them on the element directly and not the SvgDocumentElement
Edited by Jonathan Neuhauser

Merge request reports