inkex.styles module

Functions for handling styles and embedded css

class inkex.styles.Classes(classes=None, callback=None)[source]

Bases: list

A list of classes applied to an element (used in css and js)


Append object to the end of the list.


Remove first occurrence of value.

Raises ValueError if the value is not present.


If exists, remove it, if not, add it

class inkex.styles.ConditionalRule(rule)[source]

Bases: object

A single css rule


gets the css specificity of this selector

step_to_xpath = [(re.compile('(::|\\/)([a-z]+)(?=\\W)(?!-)'), '\\1svg:\\2')]

Attempt to convert the rule into a simplified xpath

class inkex.styles.ConditionalStyle(rules='*', style=None, callback=None, **kwargs)[source]

Bases: collections.OrderedDict, MutableMapping[str, Union[str,]]

Just like a Style object, but includes one or more conditional rules which places this style in a stylesheet rather than being an attribute style.

_abc_impl = <_abc._abc_data object>

gets an iterator of the specificity of all rules in this ConditionalStyle


Convert all rules to an xpath


Gets a list of xpaths for all rules of this ConditionalStyle

class inkex.styles.Style(style=None, callback=None, element=None, **kw)[source]

Bases: collections.OrderedDict, MutableMapping[str, Union[str,]]

A list of style directives

_abc_impl = <_abc._abc_data object>
static _parse_str(style: str, element=None) Iterable[][source]

Create a dictionary from the value of a CSS rule (such as an inline style or from an embedded style sheet), including its !important state, parsing the value if possible.

  • style – the content of a CSS rule to parse

  • element – the element this style is working on (can be the root SVG, is used for parsing gradients etc.)


BaseStyleValue – the parsed attribute


Creates a new Style containing all parent styles with importance “important” and current styles with importance “important”


parent – the parent style that will be merged into this one (will not be altered)


the merged Style object

Return type



Apply all shorthands in this style.

classmethod cascaded_style(element)[source]

Returns the cascaded style of an element (all rules that apply the element itself), based on the stylesheets, the presentation attributes and the inline style using the respective specificity of the style



element (BaseElement) – the element that the cascaded style will be computed for


the cascaded style

Return type


color_props = ('stroke', 'fill', 'stop-color', 'flood-color', 'lighting-color')
copy() a shallow copy of od[source]
get(key, default=None)[source]

Return the value for key if key is in the dictionary, else default.


Get the color AND opacity as one Color object

get_importance(key, default=False)[source]

Returns whether the declaration with key is marked as !important


Gets the BaseStyleValue of this key, since the other interfaces - __getitem__ and __call__ - return the original and parsed value, respectively.


key (str) – the attribute name


the BaseStyleValue struct of this attribute

Return type


interpolate(other: inkex.styles.Style, fraction: inkex.styles.Style) inkex.styles.Style[source]

Interpolate all properties.

items() a set-like object providing a view on D's items[source]
opacity_props = ('stroke-opacity', 'fill-opacity', 'opacity', 'stop-opacity')
static parse_str(style: str, element=None)[source]

Parse a style passed as string

set_color(color, name='fill')[source]

Sets the given color AND opacity as rgba to the fill or stroke style properties.

set_importance(key, importance)[source]

Sets the !important state of a declaration with key key

classmethod specified_style(element)[source]

Returns the specified style of an element, i.e. the cascaded style + inheritance, see


element (BaseElement) – the element that the specified style will be computed for


the specified style

Return type



Convert to string using a custom delimiter

unit_props = 'stroke-width'
update([E, ]**F) None.  Update D from dict/iterable E and F.[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

update_urls(old_id, new_id)[source]

Find urls in this style and replace them with the new id

class inkex.styles.StyleSheet(content=None, callback=None)[source]

Bases: list

A style sheet, usually the CDATA contents of a style tag, but also a css file used with a css. Will yield multiple Style() classes.

add(rule, style)[source]

Append a rule and style combo to this stylesheet


Make sure callback is called when updating

comment_strip = re.compile('(\\/\\/.*?\\n)|(\\/\\*.*?\\*\\/)|@.*;')
lookup(element_id, svg)[source]

Lookup the element_id against all the styles in this sheet

lookup_specificity(element_id, svg)[source]

Lookup the element_id against all the styles in this sheet and return the specificity of the match

  • element_id (str) – the id of the element that styles are being queried for

  • svg (SvgDocumentElement) – The document that contains both element and the styles


Tuple[ConditionalStyle, Tuple[int, int, int]] – all matched styles and the specificity of the match

class inkex.styles.StyleSheets(svg=None)[source]

Bases: list

Special mechanism which contains all the stylesheets for an svg document while also caching lookups for specific elements.

This caching is needed because data can’t be attached to elements as they are re-created on the fly by lxml so lookups have to be centralised.

lookup(element_id, svg=None)[source]

Find all styles for this element.

lookup_specificity(element_id, svg=None)[source]

Find all styles for this element and return the specificity of the match.