From d160e2f36737d84f1f11c8b15d2e30b38bb8dbdc Mon Sep 17 00:00:00 2001 From: ldm0 Date: Fri, 17 Jul 2026 10:22:35 +0800 Subject: [PATCH] fix(selectors): accept webkit compatibility pseudos --- .../wpt-cross-current/failed-cases.txt | 5 ++ .../wpt-cross-current/passed-cases.txt | 5 -- moli-selector/src/cssom_selector.rs | 86 +++++++++++++++++-- moli-selector/src/lib.rs | 24 ++++++ moli-selector/src/stylo/selector_parse.rs | 40 +++++++-- 5 files changed, 143 insertions(+), 17 deletions(-) diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt index 54e074b229e..b8fe328b4ce 100644 --- a/moli-benchmark/wpt-cross-current/failed-cases.txt +++ b/moli-benchmark/wpt-cross-current/failed-cases.txt @@ -44,6 +44,7 @@ content-security-policy/resource-hints/prefetch-allowed-with-conflicting-permiss content-security-policy/resource-hints/prefetch-generate-directives.html content-security-policy/resource-hints/prefetch-no-csp.html content-security-policy/sandbox/autoplay-disabled-by-csp.html +content-security-policy/script-src/non-nonceable-elements.html content-security-policy/securitypolicyviolation/blockeduri-inline.html content-security-policy/unsafe-eval/eval-blocked-in-about-blank-iframe.html content-security-policy/webrtc/webrtc-allowed-default-src-none.html @@ -2372,6 +2373,7 @@ dom/ranges/tentative/OpaqueRange-range-updates.html dom/ranges/tentative/OpaqueRange-supported-elements.html dom/ranges/tentative/OpaqueRange-unsupported-elements.html dom/ranges/tentative/OpaqueRange-validation.html +domparsing/DOMParser-parseFromString-html.html domparsing/DOMParser-parseFromString-url-base-pushstate.html domparsing/DOMParser-parseFromString-url-base.html domparsing/DOMParser-parseFromString-url-moretests.html @@ -3333,8 +3335,11 @@ trusted-types/block-string-assignment-to-HTMLIFrameElement-srcdoc.html trusted-types/block-string-assignment-to-attribute-via-attribute-node.html trusted-types/eval-function-constructor-untrusted-arguments-and-applying-default-policy.html trusted-types/inheriting-csp-for-local-schemes.html +trusted-types/legacy-trusted-script-urls.html trusted-types/modify-attributes-in-callback.html trusted-types/require-trusted-types-for-TypeError-belongs-to-the-global-object-realm.html +trusted-types/script-enforcement-010.html +trusted-types/script-enforcement-011.html trusted-types/set-attributes-require-trusted-types-default-policy.html trusted-types/set-attributes-require-trusted-types-no-default-policy-error-cases.html trusted-types/set-attributes-require-trusted-types-no-default-policy.html diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt index 6e3d1742101..aec68c715d4 100644 --- a/moli-benchmark/wpt-cross-current/passed-cases.txt +++ b/moli-benchmark/wpt-cross-current/passed-cases.txt @@ -634,7 +634,6 @@ content-security-policy/script-src/hash-always-converted-to-utf-8/iso-8859-9.htm content-security-policy/script-src/hash-always-converted-to-utf-8/utf-8-lone-surrogate.html content-security-policy/script-src/hash-always-converted-to-utf-8/utf-8.html content-security-policy/script-src/javascript-window-open-blocked.html -content-security-policy/script-src/non-nonceable-elements.html content-security-policy/script-src/script-src-1_1.html content-security-policy/script-src/script-src-1_10_1.html content-security-policy/script-src/script-src-1_2.html @@ -4717,7 +4716,6 @@ dom/traversal/TreeWalker.html dom/window-extends-event-target.html dom/xpath-result-single-node-value-nullable.html domparsing/DOMParser-parseFromString-encoding.html -domparsing/DOMParser-parseFromString-html.html domparsing/DOMParser-parseFromString-stylesheets.html domparsing/DOMParser-parseFromString-xml-doctype.html domparsing/DOMParser-parseFromString-xml-parsererror.html @@ -9515,7 +9513,6 @@ trusted-types/eval-no-csp-no-tt.html trusted-types/eval-with-non-trusted-script-object.html trusted-types/eval-with-permissive-csp.html trusted-types/get-trusted-types-compliant-attribute-value.html -trusted-types/legacy-trusted-script-urls.html trusted-types/legacy-trusted-scripts.html trusted-types/navigate-to-javascript-url-001.html trusted-types/navigate-to-javascript-url-002.html @@ -9536,8 +9533,6 @@ trusted-types/script-enforcement-004.html trusted-types/script-enforcement-005.html trusted-types/script-enforcement-006.html trusted-types/script-enforcement-007.html -trusted-types/script-enforcement-010.html -trusted-types/script-enforcement-011.html trusted-types/script-enforcement-012.html trusted-types/script-enforcement-013.html trusted-types/script-enforcement-014.html diff --git a/moli-selector/src/cssom_selector.rs b/moli-selector/src/cssom_selector.rs index fe7e7bc97d7..ac5f9ea878d 100644 --- a/moli-selector/src/cssom_selector.rs +++ b/moli-selector/src/cssom_selector.rs @@ -665,6 +665,43 @@ pub(crate) fn dom_api_selector_list_contains_known_pseudo_element(selector_text: false } +pub(crate) fn webkit_compat_pseudo_element_validation_selector( + selector_text: &str, +) -> Option { + let input = selector_text; + let mut parser = Parser::new(input); + let mut replacements = Vec::new(); + + while !parser.is_exhausted() { + let Ok(token) = parser.next_including_whitespace_and_comments().cloned() else { + return None; + }; + if !matches!(token, Token::Colon) { + continue; + } + let Ok(Token::Colon) = parser.next_including_whitespace_and_comments().cloned() else { + continue; + }; + let name_start = parser.position().byte_index(); + let Ok(Token::Ident(name)) = parser.next_including_whitespace_and_comments().cloned() + else { + continue; + }; + if is_unknown_webkit_pseudo_element_name(&name) { + replacements.push((name_start, parser.position().byte_index())); + } + } + + if replacements.is_empty() { + return None; + } + let mut selector = selector_text.to_owned(); + for (start, end) in replacements.into_iter().rev() { + selector.replace_range(start..end, "part(webkit-compat)"); + } + Some(selector) +} + pub(crate) fn selector_list_has_invalid_terminal_pseudo_element_chain(selector_text: &str) -> bool { let input = selector_text; let mut parser = Parser::new(input); @@ -718,22 +755,23 @@ fn parse_known_pseudo_element_after_colon( parser: &mut Parser<'_>, ) -> Option { let state = parser.state(); - if !matches!( + let has_double_colon = matches!( parser .next_including_whitespace_and_comments() .cloned() .ok()?, Token::Colon - ) { + ); + if !has_double_colon { parser.reset(&state); } let Ok(token) = parser.next_including_whitespace_and_comments().cloned() else { return None; }; match token { - Token::Ident(name) => { - is_known_terminal_pseudo_element_name(&name).then_some(KnownPseudoElementKind::Terminal) - } + Token::Ident(name) => (is_known_terminal_pseudo_element_name(&name) + || (has_double_colon && is_unknown_webkit_pseudo_element_name(&name))) + .then_some(KnownPseudoElementKind::Terminal), Token::Function(name) => { let kind = match name.to_ascii_lowercase().as_str() { "cue" | "highlight" => KnownPseudoElementKind::Terminal, @@ -747,6 +785,12 @@ fn parse_known_pseudo_element_after_colon( } } +fn is_unknown_webkit_pseudo_element_name(name: &str) -> bool { + const WEBKIT_PREFIX: &str = "-webkit-"; + name.get(..WEBKIT_PREFIX.len()) + .is_some_and(|prefix| prefix.eq_ignore_ascii_case(WEBKIT_PREFIX)) +} + fn is_known_terminal_pseudo_element_name(name: &str) -> bool { matches!( name.to_ascii_lowercase().as_str(), @@ -1152,6 +1196,12 @@ mod tests { assert!(dom_api_selector_list_has_only_known_pseudo_elements( "#target::highlight(foo)" )); + assert!(dom_api_selector_list_has_only_known_pseudo_elements( + "span::-WeBkIt-something-invalid" + )); + assert!(!dom_api_selector_list_has_only_known_pseudo_elements( + "input:-webkit-autofill" + )); } #[test] @@ -1173,6 +1223,23 @@ mod tests { )); } + #[test] + fn rewrites_webkit_compat_pseudo_elements_only_for_stylo_validation() { + assert_eq!( + webkit_compat_pseudo_element_validation_selector( + "span::-WeBkIt-something-invalid:active" + ) + .as_deref(), + Some("span::part(webkit-compat):active") + ); + assert_eq!( + webkit_compat_pseudo_element_validation_selector( + r#"[data-value='::-webkit-something-invalid']"# + ), + None + ); + } + #[test] fn detects_invalid_terminal_pseudo_element_chains() { assert!(selector_list_has_invalid_terminal_pseudo_element_chain( @@ -1184,6 +1251,15 @@ mod tests { assert!(selector_list_has_invalid_terminal_pseudo_element_chain( "::highlight(foo)::part(label)" )); + assert!(selector_list_has_invalid_terminal_pseudo_element_chain( + "span::-webkit-something-invalid::before" + )); + assert!(!selector_list_has_invalid_terminal_pseudo_element_chain( + "span::-webkit-something-invalid:active" + )); + assert!(!selector_list_has_invalid_terminal_pseudo_element_chain( + "input:-webkit-autofill::before" + )); assert!(!selector_list_has_invalid_terminal_pseudo_element_chain( "::part(label)::highlight(foo)" )); diff --git a/moli-selector/src/lib.rs b/moli-selector/src/lib.rs index 26e909cf2f3..4efd82d7537 100644 --- a/moli-selector/src/lib.rs +++ b/moli-selector/src/lib.rs @@ -406,6 +406,12 @@ mod tests { assert!(validate_style_rule_selector_list(".one, main > .two").is_ok()); assert!(validate_style_rule_selector_list("::part(mypart):lang(en)").is_ok()); assert!(validate_style_rule_selector_list("::part(mypart):dir(ltr)").is_ok()); + assert!( + validate_style_rule_selector_list("span::-webkit-something-invalid:active").is_ok() + ); + assert!( + validate_style_rule_selector_list("span::-webkit-something-invalid::before").is_err() + ); assert!(validate_style_rule_selector_list("").is_err()); assert!(validate_style_rule_selector_list("div[").is_err()); assert!(validate_style_rule_selector_list(".one,").is_err()); @@ -419,6 +425,7 @@ mod tests { assert!(validate_supports_selector_list("::part(mypart):is(:hover)").is_ok()); assert!(validate_supports_selector_list("::part(mypart):is(:first-child)").is_err()); assert!(validate_supports_selector_list("::part(mypart):where(:first-child)").is_err()); + assert!(validate_supports_selector_list("span::-webkit-something-invalid").is_err()); } #[test] @@ -1312,6 +1319,23 @@ mod tests { .query_selector_host(&host, "invalid# ::before") .is_err() ); + assert_eq!( + engine + .query_selector_host(&host, "span::-webkit-something-invalid") + .unwrap(), + None + ); + assert_eq!( + engine + .query_selector_host(&host, "span::-webkit-something-invalid:active") + .unwrap(), + None + ); + assert!( + engine + .query_selector_host(&host, "span::-webkit-something-invalid::before") + .is_err() + ); } #[test] diff --git a/moli-selector/src/stylo/selector_parse.rs b/moli-selector/src/stylo/selector_parse.rs index 08ca204d851..01ff80a4d92 100644 --- a/moli-selector/src/stylo/selector_parse.rs +++ b/moli-selector/src/stylo/selector_parse.rs @@ -3,6 +3,7 @@ use crate::cssom_selector::{ dom_api_selector_list_has_only_known_pseudo_elements, dom_api_selector_text_with_trailing_attribute_recovery, selector_list_has_invalid_terminal_pseudo_element_chain, + webkit_compat_pseudo_element_validation_selector, }; use cssparser::ToCss; use selectors::parser::ParseRelative; @@ -83,12 +84,20 @@ fn parse_dom_api_selector_list_text_for_url( } pub(crate) fn validate_style_rule_selector_list(selector: &str) -> Result<(), SelectorError> { - parse_style_rule_selector_list_for_url( - selector, - Url::parse("about:blank").expect("about:blank is valid"), - false, - ) - .map(|_| ()) + if selector_list_has_invalid_terminal_pseudo_element_chain(selector) { + return Err(SelectorError::syntax( + "terminal pseudo-elements cannot be chained", + )); + } + let url = Url::parse("about:blank").expect("about:blank is valid"); + parse_style_rule_selector_list_for_url(selector, url.clone(), false) + .or_else(|error| { + let Some(selector) = webkit_compat_pseudo_element_validation_selector(selector) else { + return Err(error); + }; + parse_style_rule_selector_list_for_url(&selector, url, false) + }) + .map(|_| ()) } pub(crate) fn validate_supports_selector_list(selector: &str) -> Result<(), SelectorError> { @@ -121,12 +130,29 @@ pub(crate) fn validate_style_rule_selector_list_with_namespaces( selector: &str, namespace_context: &StyleRuleNamespaceContext, ) -> Result<(), SelectorError> { + if selector_list_has_invalid_terminal_pseudo_element_chain(selector) { + return Err(SelectorError::syntax( + "terminal pseudo-elements cannot be chained", + )); + } + let url = Url::parse("about:blank").expect("about:blank is valid"); parse_style_rule_selector_list_for_url_and_namespaces( selector, - Url::parse("about:blank").expect("about:blank is valid"), + url.clone(), namespace_context, false, ) + .or_else(|error| { + let Some(selector) = webkit_compat_pseudo_element_validation_selector(selector) else { + return Err(error); + }; + parse_style_rule_selector_list_for_url_and_namespaces( + &selector, + url, + namespace_context, + false, + ) + }) .map(|_| ()) }