parsec_css/lib.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 James Conway
3
4//! CSS Syntax Level 3: the tokenizer, the parser and its entry points, the
5//! An+B and `<urange>` microsyntaxes, and serialization back to CSS, as
6//! the W3C's current editor's draft defines them, nesting included.
7//!
8//! [`parse_stylesheet_deep`] parses a whole stylesheet at once, every
9//! block's contents included; [`parse_stylesheet`] keeps each rule's
10//! `{}` block as component values, for [`SimpleBlock::contents`] to parse
11//! when you want it, since what a block holds depends on its rule.
12//!
13//! Every rule, declaration, block, function and token carries its
14//! [`Span`] in the input, and [`LineIndex`] turns one into a line and
15//! column. Names and values borrow the input unless an escape changes
16//! them. `tests/` runs the css-parsing-tests suite, and the CSS tokenizer
17//! tests, against it.
18//!
19//! ```
20//! use parsec_css::{Item, Rule};
21//!
22//! let css = "a { color: red; &:hover { color: blue } }";
23//! let sheet = parsec_css::parse_stylesheet(css);
24//! let Rule::Qualified(rule) = &sheet.rules[0] else { unreachable!() };
25//! assert_eq!(parsec_css::to_css(&rule.prelude), "a ");
26//!
27//! let contents = rule.block.clone().contents(css);
28//! let Item::Declaration(color) = &contents.items[0] else { unreachable!() };
29//! assert_eq!((&*color.name, color.span.start), ("color", 4));
30//! assert!(matches!(contents.items[1], Item::Rule(_)));
31//! ```
32
33mod bytes;
34pub mod cascade;
35mod deep;
36pub mod media;
37mod microsyntax;
38mod parser;
39mod position;
40pub mod rules;
41mod serialize;
42pub mod supports;
43mod tokenizer;
44pub mod values;
45
46pub use bytes::{decode_stylesheet, fallback_encoding};
47pub use deep::parse_stylesheet_deep;
48pub use encoding_rs::Encoding;
49pub use microsyntax::{UnicodeRange, anb, parse_anb, parse_unicode_ranges, serialize_anb};
50pub use parser::{
51 AtRule, Block, BlockContents, Bracket, ComponentValue, Declaration, ErrorKind, Function, Item,
52 ParseError, QualifiedRule, Rule, SimpleBlock, Stylesheet, parse_block_contents,
53 parse_comma_separated, parse_component_value, parse_component_values, parse_declaration,
54 parse_declaration_list, parse_rule, parse_rule_list, parse_stylesheet,
55 parse_unicode_range_value,
56};
57pub use position::LineIndex;
58pub use serialize::{to_css, write_identifier, write_string, write_values};
59pub use tokenizer::{Number, Span, Token, Tokenizer};