From 006ed3d965d5f3b77829b87a43eacd5ca0b4f997 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 20:46:05 -0500 Subject: [PATCH] svg: share bounded simple stylesheet parsing and matching (#1968) --- crates/svg/README.md | 15 ++ crates/svg/src/lib.rs | 3 + crates/svg/src/simple_css.rs | 346 +++++++++++++++++++++++++++++++++++ 3 files changed, 364 insertions(+) create mode 100644 crates/svg/README.md create mode 100644 crates/svg/src/simple_css.rs diff --git a/crates/svg/README.md b/crates/svg/README.md new file mode 100644 index 000000000..a387e58c2 --- /dev/null +++ b/crates/svg/README.md @@ -0,0 +1,15 @@ + +### Simple stylesheet compatibility subset + +`simple_css` preserves EffectCraft's compound tag/class/id stylesheet grammar and indexed +matching budget, rather than substituting the richer selector semantics of `paths`. +Property values remain text; color precision, path-prefix behavior and geometry policy +stay with the importer. Call `parse` for each sheet, then `StyleSheet::index`; discard the +sheet after a parse error and reject the import when its matching-work callback refuses. + +The parser/matcher and structural tests were ported from EffectCraft +`60e2cbbbe3d73c0a9074c25a1a615ccfec18721c`, originally by FredApps. Nested at-rule skipping +and its regression test preserve NetterSilver's +`10e173c0e49696ad335e3da8a069889c089b09a6`; both contributors retain primary Git authorship +for their respective port commits. Source and destination use MIT OR Apache-2.0. +The shared adapter adds an aggregate 64 MiB stylesheet-text limit and idempotent indexing. diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index 7cd36692e..286bd36d5 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -30,3 +30,6 @@ pub mod svgz; #[cfg(feature = "tree")] pub mod tree; pub mod write; + +/// Compatibility CSS subset for simple SVG importers. +pub mod simple_css; diff --git a/crates/svg/src/simple_css.rs b/crates/svg/src/simple_css.rs new file mode 100644 index 000000000..767b25257 --- /dev/null +++ b/crates/svg/src/simple_css.rs @@ -0,0 +1,346 @@ +//! Compatibility stylesheet subset for simple SVG importers, extracted from EffectCraft. +//! +//! Supports compound tag/class/id selectors and comma lists. Unsupported selectors/at-rules +//! are skipped; malformed trailing rules keep the accepted prefix. This deliberately is not +//! the richer `paths` cascade or a full CSS parser. Property values are retained as text, so +//! caller colour precision and geometry policy are unaffected. `!important` is stripped as in +//! the source importer; it does not change precedence here. +//! +//! Call `parse` for each style element, then `StyleSheet::index` before matching. Discard a +//! sheet after a parse error. Matching charges the caller's aggregate budget before applying +//! any declarations. The caller must stop using results when its budget callback refuses. +use std::collections::HashMap; + +/// Aggregate stylesheet byte bound before copying or allocating declaration strings. +pub const MAX_CSS_BYTES: usize = 64 * 1024 * 1024; + +/// A stylesheet exceeds its byte, selector or declaration limit. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Error(pub String); +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} +impl std::error::Error for Error {} + +/// The most selectors (each entry of a comma list counts) the `