From 04d61b47427b550c4befe78f7123f604e58cde23 Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl Date: Fri, 4 Dec 2026 23:08:53 +0100 Subject: [PATCH] Make CSS serialization an optional feature --- Cargo.toml | 9 ++++++--- color/Cargo.toml | 2 +- src/color.rs | 7 ++++++- src/lib.rs | 3 +++ src/unicode_range.rs | 23 ++++++++++++++++------- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8bc1dae8..3665feec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,17 +15,19 @@ rust-version = "1.85" exclude = ["src/css-parsing-tests/**", "src/big-data-url.css"] [dependencies] -dtoa-short = "0.3" -itoa = "1.0" smallvec = "1.0" # Optional dependencies cssparser-macros = { path = "./macros", version = "0.7.0", optional = true } +dtoa-short = { version = "0.3", optional = true } +itoa = { version = "1.0", optional = true } malloc_size_of = { version = "0.1", default-features = false, optional = true } phf = { version = "0.14.0", features = ["macros"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true } [dev-dependencies] +dtoa-short = "0.3" +itoa = "1.0" serde_json = "1.0.25" difference = "2.0" encoding_rs = "0.8" @@ -35,10 +37,11 @@ inherits = "release" debug = true [features] -default = ["fast_match_byte", "fast_match_color"] +default = ["fast_match_byte", "fast_match_color", "serialize"] bench = [] fast_match_byte = ["dep:cssparser-macros"] fast_match_color = ["dep:phf"] +serialize = ["dep:dtoa-short", "dep:itoa"] # Useful for skipping tests when execution is slow, e.g., under miri skip_long_tests = [] diff --git a/color/Cargo.toml b/color/Cargo.toml index aec88f27..a08a0a6c 100644 --- a/color/Cargo.toml +++ b/color/Cargo.toml @@ -13,7 +13,7 @@ rust-version = "1.85" path = "lib.rs" [dependencies] -cssparser = { path = "..", version = "0.38", default-features = false } +cssparser = { path = "..", version = "0.38", default-features = false, features = ["serialize"] } serde = { version = "1.0", features = ["derive"], optional = true } [features] diff --git a/src/color.rs b/src/color.rs index 448b142f..6cb5fe55 100644 --- a/src/color.rs +++ b/src/color.rs @@ -14,7 +14,10 @@ /// The opaque alpha value of 1.0. pub const OPAQUE: f32 = 1.0; -use crate::{BasicParseError, Parser, ToCss}; +#[cfg(any(feature = "serialize", test))] +use crate::ToCss; +use crate::{BasicParseError, Parser}; +#[cfg(any(feature = "serialize", test))] use std::fmt; /// Clamp a 0..1 number to a 0..255 range to u8. @@ -46,6 +49,7 @@ pub fn clamp_floor_256_f32(val: f32) -> u8 { /// Serialize the alpha copmonent of a color according to the specification. /// +#[cfg(any(feature = "serialize", test))] #[inline] pub fn serialize_color_alpha( dest: &mut impl fmt::Write, @@ -118,6 +122,7 @@ impl PredefinedColorSpace { } } +#[cfg(any(feature = "serialize", test))] impl ToCss for PredefinedColorSpace { fn to_css(&self, dest: &mut W) -> fmt::Result where diff --git a/src/lib.rs b/src/lib.rs index 2730e14a..7a371f00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,9 @@ pub use crate::rules_and_declarations::{AtRuleParser, QualifiedRuleParser}; pub use crate::rules_and_declarations::{DeclarationParser, RuleBodyItemParser, RuleBodyParser}; pub use crate::rules_and_declarations::{StyleSheetParser, parse_one_rule}; pub use crate::rules_and_declarations::{parse_important, parse_one_declaration}; +#[cfg(any(feature = "serialize", test))] pub use crate::serializer::{CssStringWriter, ToCss, TokenSerializationType}; +#[cfg(any(feature = "serialize", test))] pub use crate::serializer::{serialize_identifier, serialize_name, serialize_string}; pub use crate::tokenizer::{SourceLocation, SourcePosition, Token}; pub use crate::unicode_range::UnicodeRange; @@ -131,6 +133,7 @@ mod cow_rc_str; mod from_bytes; mod nth; mod parser; +#[cfg(any(feature = "serialize", test))] mod serializer; mod unicode_range; diff --git a/src/unicode_range.rs b/src/unicode_range.rs index 5d69507e..ea856c83 100644 --- a/src/unicode_range.rs +++ b/src/unicode_range.rs @@ -4,8 +4,10 @@ //! https://drafts.csswg.org/css-syntax/#urange +#[cfg(any(feature = "serialize", test))] +use crate::ToCss; use crate::tokenizer::Token; -use crate::{BasicParseError, Parser, ToCss}; +use crate::{BasicParseError, Parser}; use std::char; use std::fmt; @@ -157,21 +159,28 @@ fn consume_question_marks(text: &mut &[u8]) -> usize { question_marks } +impl UnicodeRange { + fn write(&self, dest: &mut W) -> fmt::Result { + write!(dest, "U+{:X}", self.start)?; + if self.end != self.start { + write!(dest, "-{:X}", self.end)?; + } + Ok(()) + } +} + impl fmt::Debug for UnicodeRange { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - self.to_css(formatter) + self.write(formatter) } } +#[cfg(any(feature = "serialize", test))] impl ToCss for UnicodeRange { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write, { - write!(dest, "U+{:X}", self.start)?; - if self.end != self.start { - write!(dest, "-{:X}", self.end)?; - } - Ok(()) + self.write(dest) } }