or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

css-rules.mdindex.mdparsing.mdselectors.mdserialization.mdstyle-declarations.mdstylesheets.mdutilities.md
tile.json

tessl/pypi-cssutils

A CSS Cascading Style Sheets library for Python implementing DOM Level 2 Style specifications

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/cssutils@1.0.x

To install, run

npx @tessl/cli install tessl/pypi-cssutils@1.0.0

index.mddocs/

cssutils

A comprehensive CSS Cascading Style Sheets library for Python that implements DOM Level 2 Style specifications. cssutils enables parsing, manipulation, and generation of CSS stylesheets with complete CSS 2.1 and CSS3 module support.

Package Information

  • Package Name: cssutils
  • Language: Python
  • Installation: pip install cssutils
  • Version: 1.0.2
  • License: LGPL 3.0

Core Imports

import cssutils

Common imports for CSS parsing and manipulation:

from cssutils import CSSParser, CSSSerializer

Access CSS classes through the css module:

import cssutils.css as css
# Then use: css.CSSStyleSheet, css.CSSStyleRule, css.CSSStyleDeclaration

Import all main functionality:

from cssutils import *
# Imports: css, stylesheets, CSSParser, CSSSerializer

Basic Usage

import cssutils

# Parse CSS from string
css_text = """
a {
    color: red;
    font-weight: bold;
}
"""

# Using convenience function
stylesheet = cssutils.parseString(css_text)
print(stylesheet.cssText)

# Using CSSParser directly
parser = cssutils.CSSParser()
stylesheet = parser.parseString(css_text)

# Access and modify rules
for rule in stylesheet:
    if rule.type == rule.STYLE_RULE:
        print(f"Selector: {rule.selectorText}")
        print(f"Properties: {rule.style.cssText}")
        
        # Modify properties
        rule.style.color = 'blue'
        rule.style.setProperty('background-color', 'yellow')

# Serialize back to CSS
print(stylesheet.cssText)

Architecture

cssutils implements the DOM Level 2 CSS specification with the following key components:

  • CSSStyleSheet: Top-level container for CSS rules and stylesheet metadata
  • CSS Rules: Complete hierarchy of CSS rule types (@import, @media, style rules, etc.)
  • CSSStyleDeclaration: Container for CSS property declarations within rules
  • Values: Structured representation of CSS property values with type-specific handling
  • Selectors: Parser and representation for CSS selectors
  • CSSParser: Configurable parser for CSS strings, files, and URLs
  • CSSSerializer: Customizable serializer for converting CSS objects back to text

The library provides both high-level convenience functions and low-level DOM access, enabling use cases from simple CSS processing to complex stylesheet manipulation and validation.

Capabilities

CSS Parsing

Parse CSS from strings, files, and URLs with comprehensive error handling and validation. Support for CSS 2.1, CSS3 modules, and real-world CSS variations.

def parseString(cssText, encoding=None, href=None, media=None, title=None, validate=None):
    """Parse CSS from string"""
    
def parseFile(filename, encoding=None, href=None, media=None, title=None, validate=None):
    """Parse CSS from file"""
    
def parseUrl(href, encoding=None, media=None, title=None, validate=None):
    """Parse CSS from URL"""
    
def parseStyle(cssText, encoding='utf-8', validate=None):
    """Parse CSS style attribute"""

CSS Parsing

CSS Stylesheets

Create, manipulate, and manage CSS stylesheets with full DOM Level 2 compliance. Handle rules, namespaces, media queries, and variables.

class CSSStyleSheet:
    """Complete CSS stylesheet representation"""
    cssRules: CSSRuleList
    media: MediaList
    href: str
    title: str
    
    def insertRule(rule, index): ...
    def deleteRule(index): ...
    def add(rule): ...

CSS Stylesheets

CSS Rules

Complete implementation of all CSS rule types including style rules, @-rules, and nested structures with proper inheritance and containment.

class CSSStyleRule:
    """CSS style rule (selector + declarations)"""
    selectorText: str
    style: CSSStyleDeclaration
    
class CSSMediaRule:
    """@media rule with nested rules"""
    media: MediaList
    cssRules: CSSRuleList
    
class CSSImportRule:
    """@import rule"""
    href: str
    media: MediaList
    styleSheet: CSSStyleSheet

CSS Rules

CSS Selectors

Parse, validate, and manipulate CSS selectors with support for CSS3 selector syntax and pseudo-classes.

class Selector:
    """Individual CSS selector"""
    selectorText: str
    specificity: tuple
    
class SelectorList:
    """List of CSS selectors"""
    selectorText: str
    
    def appendSelector(selector): ...

CSS Selectors

Style Declarations

Manage CSS property declarations with validation, shorthand expansion, and value parsing.

class CSSStyleDeclaration:
    """CSS property declarations container"""
    cssText: str
    length: int
    
    def getPropertyValue(propertyName): ...
    def setProperty(propertyName, value, priority=''): ...
    def removeProperty(propertyName): ...
    def getProperties(all=False): ...

Style Declarations

CSS Serialization

Convert CSS objects back to formatted CSS text with extensive customization options for output formatting.

class CSSSerializer:
    """CSS serialization with formatting control"""
    prefs: Preferences
    
    def do_CSSStyleSheet(stylesheet): ...
    def do_CSSStyleRule(rule): ...
    def do_css_CSSStyleDeclaration(style): ...

CSS Serialization

Utility Functions

Helper functions for CSS processing, URL handling, import resolution, and stylesheet manipulation.

def getUrls(sheet):
    """Generator yielding all URLs in stylesheet"""
    
def replaceUrls(sheetOrStyle, replacer, ignoreImportRules=False):
    """Replace URLs in stylesheet or style"""
    
def resolveImports(sheet, target=None):
    """Recursively resolve @import rules"""
    
def setSerializer(serializer):
    """Set global serializer"""

Utility Functions

Command Line Tools

Console scripts for CSS processing and manipulation from the command line.

# Installed as console scripts via pip install cssutils

cssparse   # Parse and format CSS files
csscombine # Combine CSS files by resolving @import rules  
csscapture # Capture CSS from web pages

Global Configuration

Global objects for controlling cssutils behavior across all parsing and serialization operations.

cssutils.log       # Global error handler (ErrorHandler instance)
cssutils.ser       # Global serializer (CSSSerializer instance)  
cssutils.profile   # Global validation profiles (Profiles instance)
cssutils.VERSION   # Package version string