Truly independent web browser
LibWeb: Reject nested pseudo-elements in selector support checks
Andreas Kling24bc78e77b7b
diff --git a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rsindex 194badad158..bfd18ef5855 100644--- a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs@@ -3270,6 +3270,79 @@ mod tests {));}+ #[test]+ fn supports_rejects_contextual_pseudo_elements() {+ let value_context = FfiValueParsingContext {+ kind: FfiValueParsingContextKind::Property,+ value: 0,+ secondary_value: 0,+ name: Default::default(),+ };+ let context = media_value_parse_context(&value_context);+ let selectors = [+ ":is(::before)",+ ":where(.ok, ::after)",+ ":is(.ok, :unknown)",+ ":not(::before)",+ ":not(:is(::before))",+ ":has(::before)",+ ":has(:is(::before))",+ ":nth-child(2n of ::before)",+ ":nth-last-child(2n of :is(::before))",+ ":host(::before)",+ ":host(:is(::before))",+ "::slotted(::before)",+ "::slotted(:where(.ok, ::before))",+ ":is(::slotted(.ok))",+ "::-webkit-unknown",+ ];+ let wrongly_supported: Vec<_> = selectors+ .into_iter()+ .filter(|source| {+ let units: Vec<_> = source.encode_utf16().collect();+ supports_feature_matches(&context, &[], SupportsFeatureKind::Selector, &units)+ })+ .collect();+ assert!(+ wrongly_supported.is_empty(),+ "incorrectly supported: {wrongly_supported:?}"+ );+ }++ #[test]+ fn supports_valid_pseudo_elements_and_real_nested_selectors() {+ let value_context = FfiValueParsingContext {+ kind: FfiValueParsingContextKind::Property,+ value: 0,+ secondary_value: 0,+ name: Default::default(),+ };+ let context = media_value_parse_context(&value_context);+ for source in [+ "::before",+ "div::after",+ "::part(foo)",+ "::slotted(.ok)",+ "::slotted(:is(.ok, :hover))",+ ":is(.ok, :hover)",+ ":where(:not(.bad), :has(.child))",+ ":nth-child(2n of :is(.ok, .other))",+ ":host(:is(.ok, .other))",+ ] {+ let units: Vec<_> = source.encode_utf16().collect();+ assert!(+ supports_feature_matches(&context, &[], SupportsFeatureKind::Selector, &units),+ "{source}"+ );+ }+ }++ #[test]+ fn supports_validation_does_not_change_forgiving_stylesheet_parsing() {+ let units: Vec<_> = ":is(.ok, ::before)".encode_utf16().collect();+ assert!(parse_selector_list(&units, &[], SelectorType::Standalone, SelectorParsingMode::Standard).is_ok());+ }+#[test]fn parses_container_size_and_style_queries() {let values = components_from_source(b"--".as_slice()).unwrap();diff --git a/Libraries/LibWeb/Rust/src/css/selector_operations.rs b/Libraries/LibWeb/Rust/src/css/selector_operations.rsindex d5dbc9a2f96..555185345cc 100644--- a/Libraries/LibWeb/Rust/src/css/selector_operations.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_operations.rs@@ -90,19 +90,23 @@ fn any_simple(selector: &CompiledSelector, predicate: &impl Fn(&SimpleSelector)// recursively, (rather than considering any of its syntax to be unknown or invalid) and that selector doesn’t// contain unknown -webkit- pseudo-elements.pub(crate) fn contains_unsupported_selector(selector: &CompiledSelector) -> bool {- any_simple(selector, &|simple| match simple {- SimpleSelector::Invalid(..) => true,- SimpleSelector::PseudoElement(pseudo_element) => {- pseudo_element.pseudo_element == PseudoElementType::UnknownWebKit- || matches!(&pseudo_element.value, PseudoElementValue::CompoundSelector(selector)- if contains_unsupported_selector(selector))- }- SimpleSelector::PseudoClass(pseudo_class) => pseudo_class- .argument_selector_list- .iter()- .any(|selector| contains_unsupported_selector(selector)),- _ => false,- })+ fn visit(selector: &CompiledSelector, allow_pseudo_elements: bool) -> bool {+ any_simple(selector, &|simple| match simple {+ SimpleSelector::Invalid(..) => true,+ SimpleSelector::PseudoElement(pseudo_element) => {+ !allow_pseudo_elements+ || pseudo_element.pseudo_element == PseudoElementType::UnknownWebKit+ || matches!(&pseudo_element.value, PseudoElementValue::CompoundSelector(selector)+ if visit(selector, false))+ }+ SimpleSelector::PseudoClass(pseudo_class) => pseudo_class+ .argument_selector_list+ .iter()+ .any(|selector| visit(selector, false)),+ _ => false,+ })+ }+ visit(selector, true)}fn contains_named_namespace(selector: &CompiledSelector) -> bool {diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-selector-detecting-invalid-in-logical-combinations.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-selector-detecting-invalid-in-logical-combinations.txtindex fe227e6e648..db4d08fca3d 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-selector-detecting-invalid-in-logical-combinations.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-selector-detecting-invalid-in-logical-combinations.txt@@ -2,10 +2,9 @@ Harness status: OKFound 5 tests-4 Pass-1 Fail+5 PassPass Invalid selector can be detected with CSS.supports()Pass Invalid selector can be detected with CSS.supports() even if it is dropped by forgiving parsingPass :is(), :where() or :has() always fails without argumentPass :has() always fails inside :has()-Fail Some pseudo elements always fail inside :is(), :where(), :has()\ No newline at end of file+Pass Some pseudo elements always fail inside :is(), :where(), :has()\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/parsing/parse-is-where.txt b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/parsing/parse-is-where.txtindex 23a1ff96983..8a8be863af6 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/parsing/parse-is-where.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/parsing/parse-is-where.txt@@ -2,8 +2,8 @@ Harness status: OKFound 33 tests-26 Pass-7 Fail+28 Pass+5 FailPass ":is(div )" should be a valid selectorPass ":where(div )" should be a valid selectorPass ":is(div + bar, div ~ .baz)" should be a valid selector@@ -24,8 +24,8 @@ Pass ":is(div):hover" should be a valid selectorPass ":where(div):hover" should be a valid selectorPass ":is(div)::before" should be a valid selectorPass ":where(div)::before" should be a valid selector-Fail ":is(::before)" should be a valid selector-Fail ":where(::before)" should be a valid selector+Pass ":is(::before)" should be a valid selector+Pass ":where(::before)" should be a valid selectorPass ":is(div) + bar" should be a valid selectorPass ":where(div) + bar" should be a valid selectorPass "::part(foo):is(:hover)" should be a valid selector