Skip to content

Truly independent web browser

LibWeb: Reject invalid nested selectors in CSS supports

Andreas Kling7a5eeee62fe8
diff --git a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
index 92bb6a74ad4..194badad158 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
@@ -21,7 +21,7 @@ use crate::css::parser::value_parser::{
parse_ratio_value_with_context, parse_resolution_from_stream,
};
use crate::css::property_metadata::property_id_from_name;
-use crate::css::selector_operations::contains_unknown_webkit;
+use crate::css::selector_operations::contains_unsupported_selector;
use crate::css::selector_parser::{SelectorParsingMode, SelectorType, parse_selector_list};
use crate::css::serialize::{StringUnits, TextSink, serialize_an_identifier, serialize_component_values_to_utf16};
use crate::css::style_compute::{FfiLengthResolutionContext, LENGTH_UNIT_NAMES, px_length_unit};
@@ -1064,8 +1064,6 @@ pub(crate) fn supports_feature_matches(
match kind {
SupportsFeatureKind::Declaration => supports_declaration_matches(context, value),
SupportsFeatureKind::Selector => {
- // NB: Mirrors the selector acceptance of the C++ supports evaluator that this series
- // removed from Libraries/LibWeb/CSS/Parser/RustQueryParsing.cpp.
let Ok(selectors) = parse_selector_list(
value,
declared_namespaces,
@@ -1074,7 +1072,7 @@ pub(crate) fn supports_feature_matches(
) else {
return false;
};
- selectors.len() == 1 && !contains_unknown_webkit(&selectors[0])
+ selectors.len() == 1 && !contains_unsupported_selector(&selectors[0])
}
SupportsFeatureKind::FontTech => font_tech_name_is_supported(value),
SupportsFeatureKind::FontFormat => font_format_is_supported(value),
diff --git a/Libraries/LibWeb/Rust/src/css/selector_operations.rs b/Libraries/LibWeb/Rust/src/css/selector_operations.rs
index f4a917f8adc..d5dbc9a2f96 100644
--- a/Libraries/LibWeb/Rust/src/css/selector_operations.rs
+++ b/Libraries/LibWeb/Rust/src/css/selector_operations.rs
@@ -85,15 +85,22 @@ fn any_simple(selector: &CompiledSelector, predicate: &impl Fn(&SimpleSelector)
.any(|compound| compound.simple_selectors.iter().any(predicate))
}
-pub(crate) fn contains_unknown_webkit(selector: &CompiledSelector) -> bool {
+// https://drafts.csswg.org/css-conditional-4/#support-definition
+// A CSS processor is considered to support a CSS selector if it accepts that all aspects of that selector,
+// 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_unknown_webkit(selector)),
+ .any(|selector| contains_unsupported_selector(selector)),
_ => false,
})
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-L4.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-L4.txt
index fa5438ad2f6..e9666d4ccec 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-L4.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/CSS-supports-L4.txt
@@ -2,18 +2,17 @@ Harness status: OK
Found 18 tests
-14 Pass
-4 Fail
+18 Pass
Pass selector() function accepts a selector
Pass selector() function doesn't accept a selector list
Pass selector() function rejects unknown webkit pseudo-elements.
Pass selector() function accepts known pseudo-elements
Pass selector() with simple combinators
Pass selector() with unknown combinators
-Fail selector() with forgiving :is, 1 arg
-Fail selector() with forgiving :is, multiple args
-Fail selector() with forgiving :where, 1 arg
-Fail selector() with forgiving :where, multiple args
+Pass selector() with forgiving :is, 1 arg
+Pass selector() with forgiving :is, multiple args
+Pass selector() with forgiving :where, 1 arg
+Pass selector() with forgiving :where, multiple args
Pass selector() with forgiving :has, 1 arg
Pass selector() with forgiving :has, multiple args
Pass selector() with unforgiving :nth-child, 1 arg
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.txt
index dd2af96053f..fe227e6e648 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,10 @@ Harness status: OK
Found 5 tests
-2 Pass
-3 Fail
+4 Pass
+1 Fail
Pass Invalid selector can be detected with CSS.supports()
-Fail Invalid selector can be detected with CSS.supports() even if it is dropped by forgiving parsing
+Pass Invalid selector can be detected with CSS.supports() even if it is dropped by forgiving parsing
Pass :is(), :where() or :has() always fails without argument
-Fail :has() always fails inside :has()
+Pass :has() always fails inside :has()
Fail Some pseudo elements always fail inside :is(), :where(), :has()
\ No newline at end of file