CSSOM

The CSS Object Model: parse stylesheets into typed rules and resolve the cascade to a computed style.

computed_style() runs the CSS Cascade for one element – it collects every <style> sheet in the element’s document plus the inline style along its ancestor chain, matches the native selector engine, orders the declarations by origin importance, the style attribute, specificity, and source order, then applies inheritance and each property’s initial value. The parse, selector match, and cascade all run in the C core; the classes here are the typed, read-only result shapes. They are the turbohtml-native spelling of the CSSOM CSSStyleSheet / CSSRuleList / CSSStyleRule / CSSStyleDeclaration interfaces.

The value computed_style() returns is the computed value, not the used value: turbohtml renders nothing, so no layout runs and lengths, percentages, relative units, and system colors come back as written rather than resolved to pixels. See The cascade and computed style for that boundary and the property set the cascade covers.

turbohtml.cssom.computed_style(element)[source]

Resolve the CSS cascade for one element and return its computed style.

The cascade reads every <style> element in the element’s document plus the inline style attributes along its ancestor chain, matches the native selector engine, and orders declarations by importance, the style attribute, specificity, and source order before applying inheritance and initial values.

Parameters:

element (Element) – the element to compute the style of.

Return type:

ComputedStyle

Returns:

the resolved, read-only computed style.

final class turbohtml.cssom.ComputedStyle(values)[source]

An element’s resolved computed style, the read-only result of computed_style().

It exposes one value per supported longhand property (never a shorthand), each already resolved through the cascade, inheritance, and initial values. Missing properties fall back to their initial value, so get() never returns None for a supported property.

Parameters:

values (tuple[tuple[str, str], ...])

get(name, default=None)[source]

Return a property’s computed value, or default when it is not a supported property.

Parameters:
  • name (str) – the longhand property name.

  • default (str | None) – the value to return for an unsupported property.

Return type:

str | None

Returns:

the computed value, or default.

properties()[source]

Return every supported property name, in the cascade’s canonical order.

Return type:

tuple[str, ...]

Returns:

the property names.

__getitem__(name)[source]

Return a property’s computed value, raising KeyError for an unsupported property.

Parameters:

name (str)

Return type:

str

__contains__(name)[source]

Return whether a property is a supported longhand.

Parameters:

name (str)

Return type:

bool

__iter__()[source]

Iterate the supported property names.

Return type:

Iterator[str]

__len__()[source]

Return the number of supported properties.

Return type:

int

final class turbohtml.cssom.StyleSheet(text)[source]

A parsed stylesheet, the turbohtml spelling of CSSOM’s CSSStyleSheet.

At-rules (@media, @import, and the like) are skipped: only top-level style rules join rules.

Parameters:

text (str) – the CSS source, such as the text of a <style> element.

property rules: RuleList

The stylesheet’s style rules, in document order.

final class turbohtml.cssom.RuleList(rules)[source]

A read-only sequence of StyleRule, the turbohtml spelling of CSSOM’s CSSRuleList.

Parameters:

rules (tuple[StyleRule, ...])

__getitem__(index)[source]

Return the rule at an index.

Parameters:

index (int)

Return type:

StyleRule

__iter__()[source]

Iterate the rules in document order.

Return type:

Iterator[StyleRule]

__len__()[source]

Return the number of rules.

Return type:

int

final class turbohtml.cssom.StyleRule(selector_text, style)[source]

One CSS style rule, the turbohtml spelling of CSSOM’s CSSStyleRule: a selector and its declarations.

Parameters:
selector_text: str

The rule’s selector list, verbatim (for example "a, .box").

property style: StyleDeclaration

The rule’s declaration block.

final class turbohtml.cssom.StyleDeclaration(items)[source]

A read-only block of CSS declarations, the turbohtml spelling of CSSOM’s CSSStyleDeclaration.

Each declaration keeps its property name, value, and !important flag in source order. A property may appear more than once (a later duplicate wins, as the cascade would resolve it); get() and indexing return the winning value, while iteration and properties() visit each distinct property once, in first-seen order.

Parameters:

items (tuple[tuple[str, str, bool], ...])

classmethod parse(text)[source]

Parse a declaration block – an inline style attribute or a rule body – into a declaration.

Parameters:

text (str) – the CSS declaration text, such as "color: red; margin: 0 auto".

Return type:

StyleDeclaration

Returns:

the parsed declaration.

get(name, default=None)[source]

Return a property’s winning value, or default when it is not set.

Parameters:
  • name (str) – the property name.

  • default (str | None) – the value to return when the property is absent.

Return type:

str | None

Returns:

the property value, or default.

important(name)[source]

Return whether a property’s winning declaration carries !important.

Parameters:

name (str) – the property name.

Return type:

bool

Returns:

whether the property is set and marked important.

properties()[source]

Return the distinct property names, in first-seen order.

Return type:

tuple[str, ...]

Returns:

the property names.

property text: str

The declaration serialized as name: value pairs joined by "; ".

__getitem__(name)[source]

Return a property’s winning value, raising KeyError when it is not set.

Parameters:

name (str)

Return type:

str

__contains__(name)[source]

Return whether a property is set.

Parameters:

name (str)

Return type:

bool

__iter__()[source]

Iterate the distinct property names, in first-seen order.

Return type:

Iterator[str]

__len__()[source]

Return the number of distinct properties.

Return type:

int