From 73553a3111c43b05523a88586ca29fda9e8d8a03 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Wed, 25 Nov 2026 18:57:36 +0800 Subject: [PATCH] refactor(cssom): use Stylo parsed declarations throughout CSSOM --- Cargo.lock | 22 +- Cargo.toml | 14 +- moli-css-parse/src/cssom_block.rs | 143 ++++------ moli-css-parse/src/cssom_property.rs | 246 +++--------------- moli-css-parse/src/lib.rs | 4 +- .../declaration/parse/cssom_mutation.rs | 41 +-- .../styles/declaration/parse/pdb_compat.rs | 14 +- .../element/styles/declaration/parse/tests.rs | 2 +- 8 files changed, 130 insertions(+), 356 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d86bf60ae2a..58787d069c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8580,7 +8580,7 @@ dependencies = [ [[package]] name = "selectors" version = "0.41.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "bitflags", "cssparser", @@ -8755,7 +8755,7 @@ dependencies = [ [[package]] name = "servo_arc" version = "0.5.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "serde", "stable_deref_trait", @@ -9217,7 +9217,7 @@ dependencies = [ [[package]] name = "stylo" version = "0.22.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "app_units", "arrayvec", @@ -9273,7 +9273,7 @@ dependencies = [ [[package]] name = "stylo_atoms" version = "0.22.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "string_cache 0.11.0", "string_cache_codegen 0.11.2", @@ -9282,7 +9282,7 @@ dependencies = [ [[package]] name = "stylo_derive" version = "0.22.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "darling 0.20.11", "proc-macro2", @@ -9294,7 +9294,7 @@ dependencies = [ [[package]] name = "stylo_dom" version = "0.22.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "bitflags", "stylo_malloc_size_of", @@ -9303,7 +9303,7 @@ dependencies = [ [[package]] name = "stylo_malloc_size_of" version = "0.22.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "app_units", "cssparser", @@ -9321,7 +9321,7 @@ dependencies = [ [[package]] name = "stylo_static_prefs" version = "0.22.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "toml", ] @@ -9340,7 +9340,7 @@ dependencies = [ [[package]] name = "stylo_traits" version = "0.22.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "app_units", "bitflags", @@ -9895,7 +9895,7 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "to_shmem" version = "0.6.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "cssparser", "servo_arc", @@ -9908,7 +9908,7 @@ dependencies = [ [[package]] name = "to_shmem_derive" version = "0.2.0" -source = "git+https://github.com/lexmount/stylo?rev=db43a7a011d98407392d4a23f7c30d1a3a95aef8#db43a7a011d98407392d4a23f7c30d1a3a95aef8" +source = "git+https://github.com/lexmount/stylo?rev=e6b36a449b61d17568a6e7f56c6574115fe3f73b#e6b36a449b61d17568a6e7f56c6574115fe3f73b" dependencies = [ "darling 0.20.11", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 6e275dfe540..22277e4e6b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -128,13 +128,13 @@ curl-sys = { git = "https://github.com/lexmount/curl-rust", rev = "0392e12475d8d cookie = { git = "https://github.com/ldm0/cookie-rs", branch = "priority" } v8 = { path = "vendor/v8-152.2.0" } deno_v8 = { path = "vendor/deno_v8-0.3.0" } -selectors = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" } -servo_arc = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" } -stylo = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" } -stylo_atoms = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" } -stylo_dom = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" } -stylo_static_prefs = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" } -stylo_traits = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" } +selectors = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" } +servo_arc = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" } +stylo = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" } +stylo_atoms = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" } +stylo_dom = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" } +stylo_static_prefs = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" } +stylo_traits = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" } svgtypes = { git = "https://github.com/lexmount/svgtypes", rev = "520374d714d69f8e2dc93c81a9d6be559ecde3ff" } # Preserve original SVG selector inputs and collect unclipped source geometry. usvg = { git = "https://github.com/lexmount/resvg", rev = "a137793a5749c27862b724eef60c4734526b14a0" } diff --git a/moli-css-parse/src/cssom_block.rs b/moli-css-parse/src/cssom_block.rs index 24ee25a09db..f4bd12d8c28 100644 --- a/moli-css-parse/src/cssom_block.rs +++ b/moli-css-parse/src/cssom_block.rs @@ -1,18 +1,14 @@ -use crate::{ParsedCssomProperty, cssom_property::with_detached_style_context}; -use cssparser::Parser; use style::{ moli_declaration_block::{ CssDeclarationBlock as NativeBlock, CssDeclarationEntry, CssMutationProjection, - CssRemoveResult, CssSetResult, - }, - properties::{ - Importance, PropertyDeclaration, PropertyDeclarationBlock, PropertyDeclarationId, - PropertyId, parse_property_declaration_list, + CssRemoveResult, CssSetResult, ParsedCssomProperty, declaration_block_css_text, + declaration_entry, parse_declarations, property_declaration_name, property_value_by_id, + with_detached_style_context, }, + properties::{PropertyDeclarationBlock, PropertyId, SourcePropertyDeclarationUpdate}, servo_arc::Arc, shared_lock::{Locked, SharedRwLock}, }; -use style_traits::CssString; /// A writable CSSOM block or the immutable block already shared with Stylo. #[derive(Clone, Debug)] @@ -89,13 +85,13 @@ impl CssDeclarationBlock { } pub fn css_text(&self) -> String { - self.with_declarations(serialize_block) + self.with_declarations(declaration_block_css_text) } pub fn item(&self, index: usize) -> Option { self.with_declarations(|block| { let (declaration, _) = block.declaration_importance_iter().nth(index)?; - Some(declaration_name(declaration.id())) + Some(property_declaration_name(declaration.id()).into_owned()) }) } @@ -142,52 +138,43 @@ impl CssDeclarationBlock { value: &str, priority: bool, ) -> CssMutationProjection { - let projection = with_detached_style_context(|context| { - let property = PropertyId::parse(name, context).ok()?; - let affected_names = Self::affected_names_for_property(name)?; - if value.is_empty() { - let result = self.remove_property_by_id(&property); - return Some(CssMutationProjection { - set_result: if result.changed { - CssSetResult::ChangedPropertySet - } else { - CssSetResult::Unchanged - }, - entries: Vec::new(), - affected_names, - stored_names: Vec::new(), - has_unresolved_value: false, - }); - } - let parsed = ParsedCssomProperty::parse_value(property, value, priority, context)?; - let entries = parsed.entries()?; - let has_unresolved_value = parsed.has_unresolved_value(); - let before = self.css_text(); - let modified_existing = self - .with_declarations(|b| b.first_declaration_to_remove(parsed.property()).is_some()); - self.mutate(|block| parsed.replace_in_declaration_list(block)); - let set_result = if self.css_text() == before { - CssSetResult::Unchanged - } else if modified_existing { - CssSetResult::ModifiedExisting - } else { - CssSetResult::ChangedPropertySet - }; - Some(CssMutationProjection { - set_result, - affected_names, - stored_names: entries.iter().map(|entry| entry.name.clone()).collect(), - entries, - has_unresolved_value, + let Some(parsed) = with_detached_style_context(|context| { + ParsedCssomProperty::parse(name, value, priority, context) + }) else { + return CssMutationProjection::parse_error(); + }; + if parsed.is_removal() + && !self.with_declarations(|block| { + block + .first_declaration_to_remove(parsed.property()) + .is_some() }) - }); - projection.unwrap_or_else(|| CssMutationProjection { - set_result: CssSetResult::ParseError, - affected_names: Vec::new(), - entries: Vec::new(), - stored_names: Vec::new(), - has_unresolved_value: false, - }) + { + return CssMutationProjection::removal(parsed.affected_names(), false); + } + self.mutate(|block| parsed.replace_with_projection(block)) + } + + pub fn apply_parsed_property( + &mut self, + parsed: ParsedCssomProperty, + compare_single_declaration: bool, + ) -> bool { + if compare_single_declaration + && parsed.is_single_declaration() + && parsed.is_ordinary_declaration_list() + { + let mut updates = SourcePropertyDeclarationUpdate::default(); + if self.with_declarations(|block| parsed.prepare_for_update(block, &mut updates)) { + self.mutate(|block| parsed.apply_update(block, &mut updates)); + return true; + } + if self.with_declarations(|block| parsed.has_same_specified_value(block)) { + return false; + } + } + self.mutate(|block| parsed.replace_expanded_declarations(block)); + true } pub fn remove_property(&mut self, name: &str) -> CssRemoveResult { @@ -212,43 +199,8 @@ impl CssDeclarationBlock { } } -fn property_value_by_id(block: &PropertyDeclarationBlock, property: &PropertyId) -> String { - let mut value = CssString::new(); - let _ = block.property_value_to_css(property, &mut value); - value -} - -fn serialize_block(block: &PropertyDeclarationBlock) -> String { - let mut output = CssString::new(); - let _ = block.to_css(&mut output); - output -} - -fn declaration_name(property: PropertyDeclarationId<'_>) -> String { - match property.name().as_ref() { - "font-width" => "font-stretch".to_owned(), - name => name.to_owned(), - } -} - -pub(crate) fn declaration_entry( - declaration: &PropertyDeclaration, - importance: Importance, -) -> Option { - let mut value = CssString::new(); - declaration.to_css(&mut value).ok()?; - Some(CssDeclarationEntry { - name: declaration_name(declaration.id()), - value, - priority: importance.important(), - }) -} - pub fn parse_declaration_block(text: &str) -> CssDeclarationBlock { - with_detached_style_context(|context| { - let block = parse_property_declaration_list(context, &mut Parser::new(text), &[]); - CssDeclarationBlock::new(block) - }) + CssDeclarationBlock::new(parse_declarations(text)) } #[cfg(test)] @@ -312,17 +264,22 @@ mod tests { let published = block.publish(); assert!(Arc::ptr_eq(&published, &block.publish())); assert_eq!(block.property_value("width").as_deref(), Some("10px")); + assert_eq!( + block.set_property("margin", "", false), + CssSetResult::Unchanged + ); + assert!(Arc::ptr_eq(&published, &block.publish())); assert!(block.set_property("width", "30px", false) != CssSetResult::ParseError); let next = block.publish(); assert!(!Arc::ptr_eq(&published, &next)); let lock = SharedRwLock::new(); let guard = lock.read(); assert_eq!( - serialize_block(published.read_with(&guard)), + declaration_block_css_text(published.read_with(&guard)), "width: 10px; height: 20px;" ); assert_eq!( - serialize_block(next.read_with(&guard)), + declaration_block_css_text(next.read_with(&guard)), "height: 20px; width: 30px;" ); } diff --git a/moli-css-parse/src/cssom_property.rs b/moli-css-parse/src/cssom_property.rs index 125e8cfc672..25a8af5a584 100644 --- a/moli-css-parse/src/cssom_property.rs +++ b/moli-css-parse/src/cssom_property.rs @@ -1,190 +1,41 @@ -use crate::{ - CssDeclarationBlock, CssDeclarationEntry, cssom_block::declaration_entry, - declaration::split_declaration_priority, -}; -use cssparser::Parser; +use crate::declaration::split_declaration_priority; use style::{ context::QuirksMode, - parser::ParserContext, - properties::{ - AllShorthand, Importance, PropertyDeclaration, PropertyDeclarationBlock, - PropertyDeclarationId, PropertyId, SourcePropertyDeclaration, - SourcePropertyDeclarationUpdate, - }, - stylesheets::{CssRuleType, Origin, UrlExtraData}, + moli_declaration_block::{ParsedCssomProperty, style_context, with_detached_style_context}, + stylesheets::UrlExtraData, }; -use style_traits::{CssString, ParsingMode}; -/// One parsed CSSOM declaration, including the longhands of a shorthand. -pub struct ParsedCssomProperty { - property: PropertyId, - source: SourcePropertyDeclaration, - importance: Importance, +/// Parse a nonempty CSSOM setter value in the element's style context. +pub fn parse_cssom_property( + name: &str, + value: &str, + important: bool, + url_data: &UrlExtraData, + quirks_mode: QuirksMode, +) -> Option { + if !cssom_value_allowed(value) { + return None; + } + ParsedCssomProperty::parse( + name, + value, + important, + &style_context(url_data, quirks_mode), + ) } -impl ParsedCssomProperty { - pub fn parse_without_document(name: &str, value: &str, important: bool) -> Option { - if !cssom_value_allowed(value) { - return None; - } - with_detached_style_context(|context| { - let property = PropertyId::parse(name, context).ok()?; - Self::parse_value(property, value, important, context) - }) - } - - pub fn parse( - name: &str, - value: &str, - important: bool, - url_data: &UrlExtraData, - quirks_mode: QuirksMode, - ) -> Option { - if !cssom_value_allowed(value) { - return None; - } - let context = style_context(url_data, quirks_mode); - let property = PropertyId::parse(name, &context).ok()?; - Self::parse_value(property, value, important, &context) - } - - pub(crate) fn parse_value( - property: PropertyId, - value: &str, - important: bool, - context: &ParserContext<'_>, - ) -> Option { - let mut source = SourcePropertyDeclaration::default(); - Parser::new(value) - .parse_entirely(|input| { - PropertyDeclaration::parse_into(&mut source, property.clone(), context, input) - }) - .ok()?; - Some(Self { - property, - source, - importance: if important { - Importance::Important - } else { - Importance::Normal - }, - }) - } - - pub fn is_ordinary_declaration_list(&self) -> bool { - !self.source.declarations.is_empty() && !self.has_unresolved_value() - } - - pub fn has_unresolved_value(&self) -> bool { - matches!(self.source.all_shorthand, AllShorthand::WithVariables(_)) - || self - .source - .declarations - .iter() - .any(|declaration| matches!(declaration, PropertyDeclaration::WithVariables(_))) - } - - pub fn entries(&self) -> Option> { - if !matches!(self.source.all_shorthand, AllShorthand::NotSet) { - let value = self.value()?; - return (!value.is_empty()).then(|| { - vec![CssDeclarationEntry { - name: "all".to_owned(), - value, - priority: self.importance.important(), - }] - }); - } - self.source - .declarations - .iter() - .map(|declaration| declaration_entry(declaration, self.importance)) - .collect() - } - - pub fn value(&self) -> Option { - let mut block = PropertyDeclarationBlock::new(); - for declaration in self.source.declarations.iter().cloned() { - block.push(declaration, self.importance); - } - for declaration in self.source.all_shorthand.declarations() { - block.push(declaration, self.importance); - } - block.first_declaration_to_remove(&self.property)?; - let mut value = CssString::new(); - block - .property_value_to_css(&self.property, &mut value) - .ok()?; - Some(value) - } - - pub fn is_single_declaration(&self) -> bool { - self.source.declarations.len() == 1 - } - - pub fn append_to_declaration_list(mut self, target: &mut PropertyDeclarationBlock) { - target.extend(self.source.drain(), self.importance); - } - - pub(crate) fn property(&self) -> &PropertyId { - &self.property - } - - pub(crate) fn replace_in_declaration_list(self, target: &mut PropertyDeclarationBlock) { - remove(target, &self.property); - self.append_to_declaration_list(target); - } - - pub fn apply( - mut self, - target: &mut CssDeclarationBlock, - compare_single_longhand: bool, - ) -> bool { - let mut updates = SourcePropertyDeclarationUpdate::default(); - if compare_single_longhand - && self.is_single_declaration() - && self.is_ordinary_declaration_list() - { - let in_place = target.with_declarations(|block| { - block.prepare_for_update(&self.source, self.importance, &mut updates) - }); - if in_place { - // Stylo keeps independent longhands in place and moves declarations - // across conflicting logical/physical declarations when needed. - let mut block = std::mem::take(target).into_inner(); - block.update(self.source.drain(), self.importance, &mut updates); - *target = CssDeclarationBlock::new(block); - return true; - } - let incoming = self.source.declarations.iter().next().unwrap(); - let same_serialization = target.with_declarations(|block| { - block.declaration_importance_iter().any(|(old, _)| { - old.id() == incoming.id() && specified_text(old) == specified_text(incoming) - }) - }); - if same_serialization { - return false; - } - // Typed equality can hide a difference in specified CSS text, for - // example two URL spellings that resolve to the same address. - } - let mut block = std::mem::take(target).into_inner(); - // Preserve the CSSOM order and priority of the existing append path. - if !matches!(self.source.all_shorthand, AllShorthand::NotSet) { - remove(&mut block, &self.property); - } else { - for declaration in &self.source.declarations { - let property = match declaration.id() { - PropertyDeclarationId::Longhand(id) => PropertyId::NonCustom(id.into()), - PropertyDeclarationId::Custom(name) => PropertyId::Custom(name.clone()), - }; - remove(&mut block, &property); - } - } - block.extend(self.source.drain(), self.importance); - *target = CssDeclarationBlock::new(block); - true - } +/// Parse a nonempty CSSOM setter value without a document base URL. +pub fn parse_cssom_property_without_document( + name: &str, + value: &str, + important: bool, +) -> Option { + if !cssom_value_allowed(value) { + return None; + } + with_detached_style_context(|context| { + ParsedCssomProperty::parse(name, value, important, context) + }) } fn cssom_value_allowed(value: &str) -> bool { @@ -192,36 +43,3 @@ fn cssom_value_allowed(value: &str) -> bool { && (!value.contains(';') || crate::escape_top_level_semicolons(value) == value) && !split_declaration_priority(value).is_some_and(|(_, important)| important) } - -pub(crate) fn with_detached_style_context( - use_context: impl FnOnce(&ParserContext<'_>) -> T, -) -> T { - let url_data = UrlExtraData::from(url::Url::parse("about:blank").unwrap()); - use_context(&style_context(&url_data, QuirksMode::NoQuirks)) -} - -fn style_context(url_data: &UrlExtraData, quirks_mode: QuirksMode) -> ParserContext<'_> { - ParserContext::new( - Origin::Author, - url_data, - Some(CssRuleType::Style), - ParsingMode::DEFAULT, - quirks_mode, - Default::default(), - None, - None, - Default::default(), - ) -} - -fn remove(block: &mut PropertyDeclarationBlock, property: &PropertyId) { - if let Some(index) = block.first_declaration_to_remove(property) { - block.remove_property(property, index); - } -} - -fn specified_text(declaration: &PropertyDeclaration) -> String { - let mut text = CssString::new(); - let _ = declaration.to_css(&mut text); - text -} diff --git a/moli-css-parse/src/lib.rs b/moli-css-parse/src/lib.rs index bb0e1c71e80..118e97dadd5 100644 --- a/moli-css-parse/src/lib.rs +++ b/moli-css-parse/src/lib.rs @@ -25,7 +25,7 @@ pub use color::{ parse_css_color_to_opaque_srgb_hex, parse_css_color_to_srgb_bytes, }; pub use cssom_block::{CssDeclarationBlock, parse_declaration_block}; -pub use cssom_property::ParsedCssomProperty; +pub use cssom_property::{parse_cssom_property, parse_cssom_property_without_document}; pub use declaration::{ CssDeclaration, DeclarationParseOptions, parse_declaration_list, parse_declaration_list_with_source_check, @@ -53,7 +53,7 @@ pub use media::{ }; pub use root_margin::{normalize_root_margin, root_margin_components}; pub use style::moli_declaration_block::{ - CssDeclarationEntry, CssMutationProjection, CssRemoveResult, CssSetResult, + CssDeclarationEntry, CssMutationProjection, CssRemoveResult, CssSetResult, ParsedCssomProperty, }; pub use stylesheet_imports::{ StylesheetImportPlaceholderLoader, StylesheetImports, diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/cssom_mutation.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/cssom_mutation.rs index ac967420517..2d79b75b3a0 100644 --- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/cssom_mutation.rs +++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/cssom_mutation.rs @@ -162,9 +162,6 @@ pub(in crate::native_bridge::element::styles) fn set_inline_style_property_with_ } else { existing_inline_base_url }; - let Some(affected_names) = style_property_mutation_affected_names_with_pdb(name) else { - return InlineStylePropertyWrite::NeedsCompatibilityEntries; - }; let current = cached .as_deref() .or(initial.as_ref()) @@ -195,25 +192,26 @@ pub(in crate::native_bridge::element::styles) fn set_inline_style_property_with_ url_data: UrlExtraData::from(base), quirks_mode: mode, }); - moli_css_parse::ParsedCssomProperty::parse( - name, - value, - priority, - &context.url_data, - mode, - ) - .filter(moli_css_parse::ParsedCssomProperty::is_ordinary_declaration_list) - .map(|declarations| (declarations, publishable.then_some(context))) + moli_css_parse::parse_cssom_property(name, value, priority, &context.url_data, mode) + .filter(moli_css_parse::ParsedCssomProperty::is_ordinary_declaration_list) + .map(|declarations| (declarations, publishable.then_some(context))) }) .flatten(); if value.is_empty() || parsed.is_some() { - if value.is_empty() - && !affected_names + let removal_names = if value.is_empty() { + let Some(names) = style_property_mutation_affected_names_with_pdb(name) else { + return InlineStylePropertyWrite::NeedsCompatibilityEntries; + }; + if !names .iter() .any(|property| current.block.property_is_declared(property)) - { - return InlineStylePropertyWrite::NoAttributeWrite; - } + { + return InlineStylePropertyWrite::NoAttributeWrite; + } + names + } else { + Vec::new() + }; let cleanup_names = style_property_mutation_cleanup_names_with_pdb(name); let compare_longhand = cleanup_names.is_empty() && !update_inline_style_base; let can_defer = !blocked @@ -228,7 +226,7 @@ pub(in crate::native_bridge::element::styles) fn set_inline_style_property_with_ && runtime.can_defer_cssom_attribute(handle); let mutate = |state: &mut CssInlineStyleDeclarationState| { if let Some((parsed, context)) = parsed { - if !parsed.apply(&mut state.block, compare_longhand) { + if !state.block.apply_parsed_property(parsed, compare_longhand) { return false; } state.parse_context = context; @@ -236,7 +234,7 @@ pub(in crate::native_bridge::element::styles) fn set_inline_style_property_with_ state.block.remove_property(&property); } } else { - for property in &affected_names { + for property in &removal_names { state.block.remove_property(property); } } @@ -274,6 +272,9 @@ pub(in crate::native_bridge::element::styles) fn set_inline_style_property_with_ )); } } + let Some(affected_names) = style_property_mutation_affected_names_with_pdb(name) else { + return InlineStylePropertyWrite::NeedsCompatibilityEntries; + }; let mut state = initial .or_else(|| cached.as_deref().cloned()) .expect("compatibility inline style declaration state"); @@ -337,7 +338,7 @@ pub(in crate::native_bridge::element::styles) fn set_inline_style_property_with_ let uses_preferred_supplemental_entries = cssom_style_property_uses_preferred_pdb_supplemental_entries(name, value, priority); let mut entries = if let Some(native) = native { - native.apply(&mut state.block, false); + state.block.apply_parsed_property(native, false); parsed.entries.clone() } else { let Some(entries) = set_pdb_block_property_collecting_entries( diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs index bcc4de5ba8a..fc77fbf2bd4 100644 --- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs +++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs @@ -690,8 +690,9 @@ fn stylo_pdb_style_property( value: &str, priority: bool, ) -> Option<(ParsedStylePropertyEntries, Option)> { - let affected_names = moli_css_parse::CssDeclarationBlock::affected_names_for_property(name)?; if value.is_empty() { + let affected_names = + moli_css_parse::CssDeclarationBlock::affected_names_for_property(name)?; return Some(( ParsedStylePropertyEntries { entries: Vec::new(), @@ -700,7 +701,7 @@ fn stylo_pdb_style_property( None, )); } - let native = ParsedCssomProperty::parse_without_document(name, value, priority)?; + let native = moli_css_parse::parse_cssom_property_without_document(name, value, priority)?; let unresolved = native.has_unresolved_value() || css_value_uses_unresolved_cssom_storage(value); let block_entries = if unresolved { @@ -712,19 +713,16 @@ fn stylo_pdb_style_property( } else { native.entries()? }; - let stored_names = block_entries - .iter() - .map(|entry| entry.name.clone()) - .collect::>(); let mut entries = Vec::with_capacity(block_entries.len()); for entry in block_entries { let entry_name = canonical_style_property_name(&entry.name); let is_declared_empty_custom_property = entry.value.is_empty() && moli_css_parse::is_cssom_custom_property_name(&entry_name); + let renamed = entry_name != entry.name && entry_name != name; if entry_name.is_empty() || entry.value.is_empty() && !is_declared_empty_custom_property || entry.priority != priority - || entry_name != name && !stored_names.contains(&entry_name) + || renamed && (unresolved || !native.contains_entry_name(&entry_name)) { return None; } @@ -737,7 +735,7 @@ fn stylo_pdb_style_property( Some(( ParsedStylePropertyEntries { entries, - affected_names, + affected_names: native.affected_names(), }, (name != "all" && !unresolved).then_some(native), )) diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs index 82b97aac198..652d99721ca 100644 --- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs +++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs @@ -138,7 +138,7 @@ fn preparsed_pdb_property_preserves_replacement_order_priority_and_shorthand_res moli_css_parse::CssSetResult::ParseError ); let mut actual = moli_css_parse::parse_declaration_block(seed); - native.apply(&mut actual, false); + actual.apply_parsed_property(native, false); assert_eq!( actual.css_text(), expected.css_text(),