Registry / serialization / tinycss2

tinycss2

JSON →
library1.5.1pypypi✓ verified 35d ago

tinycss2 is a low-level CSS parser and generator written in Python. It can parse CSS strings, return objects representing tokens and blocks, and generate CSS strings from these objects. Based on the CSS Syntax Level 3 specification, it handles the grammar of CSS but does not know specific rules, properties, or values. It is actively maintained with regular releases.

serialization
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Primary function for parsing full CSS stylesheets.

import tinycss2 rules = tinycss2.parse_stylesheet('body { color: red; }')

While often not directly imported, AST node classes reside in the `tinycss2.ast` module for inspection and manipulation of parsed output.

from tinycss2.ast import QualifiedRule, IdentToken

Parses a CSS string into an Abstract Syntax Tree (AST) of tokens and blocks, then iterates through the parsed rules and declarations.

import tinycss2 css_input = '#my-id div { width: 50%; height: 100px }' rules = tinycss2.parse_stylesheet(css_input) # The parsed output is a list of nodes for rule in rules: if rule.type == 'qualified-rule': print(f"Qualified Rule: {rule.prelude[0].value} {rule.prelude[2].value}") # Example: 'my-id', 'div' for declaration in tinycss2.parse_declaration_list(rule.content): if declaration.type == 'declaration': print(f" Declaration: {declaration.name}: {declaration.value[0].value}{declaration.value[0].unit}") # To serialize back to CSS (approximate) serialized_css = ''.join(node.serialize() for node in rules) print(f"\nSerialized CSS: {serialized_css}")
Debug
Known footguns
deprecatedThe `tinycss2.parse_declaration_list` function was deprecated in v1.3.0.
breakingPython 3.6 support was dropped in v1.2.0, and Python 3.5 support in v1.1.0. The library currently requires Python 3.10 or newer.
gotchatinycss2 is a low-level CSS parser. It understands CSS syntax (tokens, blocks, qualified rules, at-rules) but does not interpret the semantics of specific CSS properties, values, or selectors. It does not provide high-level representations of browser-rendered styles.
breakingIn tinycss2 v0.5, the default behavior for parsing functions changed to preserve CSS comments and top-level whitespace in the output AST. Previously, these were skipped by default.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources