From 37352b0436b7b3f02357ebd37c9b66567b2b4873 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Thu, 9 Jan 2025 19:20:31 +0800 Subject: [PATCH] feat: +feature debug to disable DebugGroup --- float-pigment-css/Cargo.toml | 3 ++- float-pigment-css/src/ffi.rs | 1 + float-pigment-css/src/parser/mod.rs | 16 +++++++++++++++- float-pigment-css/src/query.rs | 2 ++ float-pigment-css/src/sheet/borrow.rs | 1 + float-pigment-css/src/sheet/rule.rs | 15 ++++++++++++++- 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/float-pigment-css/Cargo.toml b/float-pigment-css/Cargo.toml index 6f74fb2..6e11660 100644 --- a/float-pigment-css/Cargo.toml +++ b/float-pigment-css/Cargo.toml @@ -35,7 +35,7 @@ name = "parse" harness = false [features] -default = ["std", "deserialize", "serialize", "ffi"] +default = ["std", "deserialize", "serialize", "ffi", "debug"] std = ["serde/std", "serde_json/std", "bit-set/std", "num-traits/std", "half/std", "float-pigment-consistent-bincode/std"] no-std-lock = [] wasm-entrance = ["wasm-bindgen", "console_log", "console_error_panic_hook", "js-sys"] @@ -50,6 +50,7 @@ compatibility_test = [] compile_cache = ["toml", "colorful"] skip_compare_cache = ["float-pigment-css-macro/skip_compare_cache"] ffi = [] +debug = [] [dependencies] wasm-bindgen = { version = "0.2", optional = true } diff --git a/float-pigment-css/src/ffi.rs b/float-pigment-css/src/ffi.rs index f0159dd..808b3a8 100644 --- a/float-pigment-css/src/ffi.rs +++ b/float-pigment-css/src/ffi.rs @@ -465,6 +465,7 @@ pub unsafe extern "C" fn parse_inline_style( bs_empty = false; property } + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { .. } => Property::Unknown, }) .collect(); diff --git a/float-pigment-css/src/parser/mod.rs b/float-pigment-css/src/parser/mod.rs index 497b30f..925be4d 100644 --- a/float-pigment-css/src/parser/mod.rs +++ b/float-pigment-css/src/parser/mod.rs @@ -1801,6 +1801,19 @@ fn parse_property_item<'a, 't: 'a, 'i: 't>( Ok(()) } +#[cfg(not(feature = "debug"))] +#[inline(always)] +fn parse_property_item_debug<'a, 't: 'a, 'i: 't>( + parser: &'a mut Parser<'i, 't>, + properties: &'a mut Vec, + st: &mut ParseState, + disabled: bool, + rule_end_position: Option, +) -> Result<(), ParseError<'i, CustomError>> { + Err(parser.new_custom_error(CustomError::Unsupported)) +} + +#[cfg(feature = "debug")] #[inline(always)] fn parse_property_item_debug<'a, 't: 'a, 'i: 't>( parser: &'a mut Parser<'i, 't>, @@ -1908,6 +1921,7 @@ fn parse_property_value_with_important<'a, 't: 'a, 'i: 't>( *pm = match pm2 { PropertyMeta::Normal { property } => PropertyMeta::Important { property }, PropertyMeta::Important { .. } => unreachable!(), + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { .. } => unreachable!(), }; } @@ -1923,6 +1937,7 @@ fn parse_property_value_with_important<'a, 't: 'a, 'i: 't>( if let Some(p) = match &mut pm { PropertyMeta::Normal { property } => Some(property), PropertyMeta::Important { property } => Some(property), + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { .. } => None, } { let ctx = &mut hooks::ParserHooksContext { @@ -1983,7 +1998,6 @@ fn parse_custom_property_value_with_important<'a, 't: 'a, 'i: 't>( #[cfg(test)] mod test { - use super::{is_url, parse_color_to_rgba, resolve_relative_path}; #[test] diff --git a/float-pigment-css/src/query.rs b/float-pigment-css/src/query.rs index b8a80e3..8d2d46e 100644 --- a/float-pigment-css/src/query.rs +++ b/float-pigment-css/src/query.rs @@ -370,6 +370,7 @@ impl MatchedRuleList { } } } + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { properties, important, @@ -442,6 +443,7 @@ impl MatchedRuleList { node_properties.merge_property(p, parent_node_properties, current_font_size) } } + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { properties, important, diff --git a/float-pigment-css/src/sheet/borrow.rs b/float-pigment-css/src/sheet/borrow.rs index 426725d..041ee7a 100644 --- a/float-pigment-css/src/sheet/borrow.rs +++ b/float-pigment-css/src/sheet/borrow.rs @@ -328,6 +328,7 @@ impl Rule { bs_empty = false; property } + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { .. } => Property::Unknown, }) .collect(); diff --git a/float-pigment-css/src/sheet/rule.rs b/float-pigment-css/src/sheet/rule.rs index dda05e6..3d6511a 100644 --- a/float-pigment-css/src/sheet/rule.rs +++ b/float-pigment-css/src/sheet/rule.rs @@ -21,9 +21,10 @@ pub enum PropertyMeta { /// /// It is designed for debugging only. /// In production environment, properties are well-normalized - - /// shorthand properties (e.g. `font` `background`) are splitted in advance. + /// shorthand properties (e.g. `font` `background`) are split in advance. /// However, we may add new shorthand properties in debugger - /// we can keep the shorthand properties as-is with `DebugGroup`s. + #[cfg(feature = "debug")] DebugGroup { /// The original name-value string pair. original_name_value: Box<(String, String)>, @@ -51,6 +52,7 @@ impl PropertyMeta { /// Note that the new property is in *debug* mode so that: /// * it cannot be serialized even if it has been inserted to a rule; /// * it has a little performance penalty. + #[cfg(feature = "debug")] pub fn to_debug_state(&self, disabled: bool) -> Self { match self { Self::Normal { property } => Self::DebugGroup { @@ -90,6 +92,7 @@ impl PropertyMeta { match self { Self::Normal { .. } => false, Self::Important { .. } => true, + #[cfg(feature = "debug")] Self::DebugGroup { important, .. } => *important, } } @@ -99,6 +102,7 @@ impl PropertyMeta { match self { Self::Normal { property } => property.get_property_name().into(), Self::Important { property } => property.get_property_name().into(), + #[cfg(feature = "debug")] Self::DebugGroup { original_name_value, .. @@ -117,6 +121,7 @@ impl PropertyMeta { v.push_str(" !important"); v } + #[cfg(feature = "debug")] Self::DebugGroup { original_name_value, .. @@ -129,6 +134,7 @@ impl PropertyMeta { match self { Self::Normal { .. } => false, Self::Important { .. } => false, + #[cfg(feature = "debug")] Self::DebugGroup { disabled, .. } => *disabled, } } @@ -138,6 +144,7 @@ impl PropertyMeta { match self { Self::Normal { .. } => false, Self::Important { .. } => false, + #[cfg(feature = "debug")] Self::DebugGroup { properties, .. } => properties.len() == 0, } } @@ -148,6 +155,7 @@ impl PropertyMeta { Self::Normal { property, .. } | Self::Important { property, .. } => { property.is_deprecated() } + #[cfg(feature = "debug")] Self::DebugGroup { .. } => false, } } @@ -166,6 +174,7 @@ impl PropertyMeta { PropertyMeta::Important { property: p } => { node_properties.merge_property(p, parent_node_properties, current_font_size) } + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { properties, disabled, @@ -189,6 +198,7 @@ impl PropertyMeta { pub fn property(&self) -> Option { match self { Self::Normal { property } | Self::Important { property } => Some(property.clone()), + #[cfg(feature = "debug")] Self::DebugGroup { .. } => None, } } @@ -233,6 +243,7 @@ impl<'a> Iterator for PropertyMetaIter<'a> { None } } + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { properties, .. } => { if self.cur < properties.len() { let ret = &properties[self.cur]; @@ -317,6 +328,7 @@ impl Rule { _ => {} } } + #[cfg(feature = "debug")] PropertyMeta::DebugGroup { properties, .. } => { for property in properties.iter() { match property { @@ -377,6 +389,7 @@ impl Rule { } /// Enable or disable the rule (and construct a new one as the result if success) + #[cfg(feature = "debug")] pub fn set_property_disabled(&self, index: usize, disabled: bool) -> Option> { let media = self.media.clone(); let selector = self.selector.clone();