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)

_callback()[source]
append(value)[source]

Append object to the end of the list.

remove(value)[source]

Remove first occurrence of value.

Raises ValueError if the value is not present.

toggle(value)[source]

If exists, remove it, if not, add it

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

Bases: object

A single css rule

get_specificity()[source]

gets the css specificity of this selector

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

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, inkex.properties.BaseStyleValue]]

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>
get_specificities()[source]

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

to_xpath()[source]

Convert all rules to an xpath

to_xpaths()[source]

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, inkex.properties.BaseStyleValue]]

A list of style directives

_abc_impl = <_abc._abc_data object>
static _parse_str(style: str, element=None) Iterable[inkex.properties.BaseStyleValue][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.

Parameters
  • 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.)

Yields

BaseStyleValue – the parsed attribute

add_inherited(parent)[source]

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

Parameters

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

Returns

the merged Style object

Return type

Style

apply_shorthands()[source]

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

see https://www.w3.org/TR/CSS22/cascade.html#cascading-order

Parameters

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

Returns

the cascaded style

Return type

Style

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_color(name='fill')[source]

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

get_store(key)[source]

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

Parameters

key (str) – the attribute name

Returns

the BaseStyleValue struct of this attribute

Return type

BaseStyleValue

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 https://www.w3.org/TR/CSS22/cascade.html#specified-value

Parameters

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

Returns

the specified style

Return type

Style

to_str(sep=';')[source]

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.

_callback(style=None)[source]
add(rule, style)[source]

Append a rule and style combo to this stylesheet

append(other)[source]

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

Parameters
  • 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

Yields

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.