tpt-css
RustClean-room CSS engine in Rust. Zero third-party core dependencies, no unsafe code, deterministic style resolution for AppFront and web trees. Dual licensed MIT OR Apache-2.0.
Languages
tpt-css
A clean-room CSS engine in Rust. It takes CSS text through tokens, a stylesheet
AST, selector matching, the cascade, and property parsing to computed styles,
over any tree that implements a small StyleTree trait.
Status: see docs/STATUS.md for what works, what is partly done, and what is missing. Open items are in TODO.md. Spec-by-spec traceability is in SPEC_TRACE.md.
Design
- Spec first. The design is in spec.txt. Decisions and deviations are in DESIGN.md.
- Clean room. Implemented from public CSS specifications only. See CLEAN_ROOM.md.
- No dependencies. The core has no third-party crates.
unsafeis forbidden. - Not tied to a document model. The engine styles an abstract tree. AppFront and DOM are adapters, and the core has no HTML names.
- Deterministic. Same input, same output. No hash-map order affects results.
- Bounded. Input size, token count, nesting, selector work, and rule count
are all limited and configurable (
ParseLimits).
Example
A complete program is in examples/minimal.rs. It
implements StyleTree for a small arena tree and prints computed colors:
cargo run --example minimal
More examples are in examples/: dark_mode, diagnostics,
media_and_container, and cssom_edit (feature cssom).
The core calls are:
use tpt_css::{parse_stylesheet, Environment, Origin, ParseOptions, ResolverConfig, StyleResolver};
let parsed = parse_stylesheet("p { color: red }", ParseOptions::default());
let mut resolver = StyleResolver::new(ResolverConfig::default());
resolver.add_stylesheet(parsed.stylesheet, Origin::Author);
// `tree` implements tpt_css::StyleTree; see examples/minimal.rs.
let styles = resolver.resolve(&tree, &Environment::default());
tests/examples.rs checks the example's output, so it stays in step with the API.
Command line and playground
The crate ships a dependency-free tpt-css binary. See
docs/CLI.md for the full usage.
cargo run -- check style.css # diagnostics for a stylesheet
cargo run -- compute tree.json # computed styles for a fixture tree
cargo run -- inspect tree.json # winning declaration of each property
The WASM playground in playground/ runs the engine in a
browser page. Its build.sh builds the module.
Build and test
cargo test # default features (appfront)
cargo test --no-default-features
cargo test --features web
cargo test --features cssom
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo deny check licenses bans
To regenerate the tokenizer golden files after an intended change:
TPT_UPDATE_GOLDEN=1 cargo test --test golden
Review the diff before committing the new files.
Enable the pre-commit hook (cargo fmt --check and clippy) once per clone:
git config core.hooksPath .githooks
The fuzz targets are a separate package in fuzz/. Their bodies also run as a
seeded smoke test under cargo test.
Layout
src/ engine (see DESIGN.md section 5 for the module map)
src/bin/ the tpt-css command line
tests/ one test target per area, plus tests/common (a test tree)
examples/ runnable examples
fixtures/ own MIT fixtures (fixtures/mit) and golden files (fixtures/golden)
fuzz/ fuzz targets (cargo-fuzz)
playground/ WASM playground
docs/ status, command line, and generated property support table
spec.txt design specification
License
Dual licensed under MIT or Apache-2.0, at your option. Copyright TPT Solutions. See NOTICE.
Every first-party source file carries the SPDX identifier
MIT OR Apache-2.0.