From e25ec22e5f9ef144788b09bc76a93b703d53ba18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Sat, 19 Sep 2026 03:00:00 +0200 Subject: [PATCH] Match CSS namespace type selectors --- .../native/html_parser/src/lib.rs | 79 +++++++++++++++++- .../native/webscene_css_compound.h | 5 ++ .../native/webscene_css_rule_payload.h | 18 +++-- .../native/webscene_css_rule_preparation.h | 7 +- .../native/webscene_css_selectors.h | 61 ++++++++++++-- .../native/webscene_css_state.h | 4 + .../native/webscene_css_stylesheet.h | 10 ++- .../native/webscene_css_stylesheet_sink.h | 61 +++++++++++++- .../native/webscene_selector_parser.cpp | 65 +++++++++++++-- .../native/webscene_selector_parser.h | 12 +++ .../native/webscene_selector_parser_ffi.h | 11 +++ .../webscene_v8_runtime_css_parsing.inc | 45 +++++++---- .../tests/selector_parser_tests.cpp | 34 ++++++++ tests/NativeWeb/css_service.cpp | 5 +- .../cssom-namespace-selector-matching.html | 80 +++++++++++++++++++ ...m-namespace-selector-matching-profile.json | 22 +++++ 16 files changed, 471 insertions(+), 48 deletions(-) create mode 100644 tests/WebPlatformSubset/contracts/cssom-namespace-selector-matching.html create mode 100644 tests/WebPlatformSubset/webscene-cssom-namespace-selector-matching-profile.json diff --git a/experiments/WebScene.NativeEngine.Probe/native/html_parser/src/lib.rs b/experiments/WebScene.NativeEngine.Probe/native/html_parser/src/lib.rs index 4e036db82..8bcf3185f 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/html_parser/src/lib.rs +++ b/experiments/WebScene.NativeEngine.Probe/native/html_parser/src/lib.rs @@ -1299,6 +1299,7 @@ mod selector_syntax { }; use selectors::{Parser as SelectorParser, SelectorImpl, SelectorList}; use std::borrow::Borrow; + use std::collections::HashMap; use std::fmt::{self, Write}; #[derive(Clone, Debug, Default, Eq, Hash, PartialEq)] @@ -1433,7 +1434,10 @@ mod selector_syntax { } #[derive(Default)] - struct WebSceneSelectorParser; + struct WebSceneSelectorParser { + default_namespace: Option, + namespaces: HashMap, + } fn is_supported_pseudo_class(name: &str) -> bool { matches!( @@ -1514,6 +1518,14 @@ mod selector_syntax { true } + fn default_namespace(&self) -> Option { + self.default_namespace.clone() + } + + fn namespace_for_prefix(&self, prefix: &Atom) -> Option { + self.namespaces.get(prefix).cloned() + } + fn parse_non_ts_pseudo_class( &self, location: SourceLocation, @@ -1692,6 +1704,13 @@ mod selector_syntax { pub combinator_count: usize, } + #[repr(C)] + #[derive(Clone, Copy, Default)] + pub struct SelectorNamespace { + pub prefix: ByteSlice, + pub namespace_url: ByteSlice, + } + fn selector_error(status: u32) -> SelectorParseResult { SelectorParseResult { status, @@ -1704,17 +1723,48 @@ mod selector_syntax { ABI_VERSION } - #[no_mangle] - pub extern "C" fn webscene_selector_parse(input: ByteSlice) -> SelectorParseResult { + fn parse_selector( + input: ByteSlice, + default_namespace: Option, + namespaces: *const SelectorNamespace, + namespace_count: usize, + ) -> SelectorParseResult { let Some(input) = read_slice(input).and_then(|bytes| std::str::from_utf8(bytes).ok()) else { return selector_error(STATUS_INVALID_ARGUMENT); }; + if namespace_count > 256 || (namespace_count != 0 && namespaces.is_null()) { + return selector_error(STATUS_INVALID_ARGUMENT); + } + let default_namespace = match default_namespace { + Some(value) => match read_slice(value).and_then(|bytes| std::str::from_utf8(bytes).ok()) { + Some(value) => Some(Atom::from(value)), + None => return selector_error(STATUS_INVALID_ARGUMENT), + }, + None => None, + }; + let mut namespace_map = HashMap::with_capacity(namespace_count); + if namespace_count != 0 { + let entries = unsafe { std::slice::from_raw_parts(namespaces, namespace_count) }; + for entry in entries { + let Some(prefix) = read_slice(entry.prefix) + .and_then(|bytes| std::str::from_utf8(bytes).ok()) + else { return selector_error(STATUS_INVALID_ARGUMENT); }; + let Some(url) = read_slice(entry.namespace_url) + .and_then(|bytes| std::str::from_utf8(bytes).ok()) + else { return selector_error(STATUS_INVALID_ARGUMENT); }; + namespace_map.insert(Atom::from(prefix), Atom::from(url)); + } + } reset_allocation_metrics(); let parsed = catch_unwind(AssertUnwindSafe(|| { let mut parser_input = ParserInput::new(input); let mut parser = CssParser::new(&mut parser_input); - let list = SelectorList::parse(&WebSceneSelectorParser, &mut parser, ParseRelative::No) + let selector_parser = WebSceneSelectorParser { + default_namespace, + namespaces: namespace_map, + }; + let list = SelectorList::parse(&selector_parser, &mut parser, ParseRelative::No) .map_err(|_| ())?; parser.expect_exhausted().map_err(|_| ())?; Ok::<_, ()>(SelectorOutput { @@ -1738,6 +1788,27 @@ mod selector_syntax { } } + #[no_mangle] + pub extern "C" fn webscene_selector_parse(input: ByteSlice) -> SelectorParseResult { + parse_selector(input, None, std::ptr::null(), 0) + } + + #[no_mangle] + pub extern "C" fn webscene_selector_parse_with_namespaces( + input: ByteSlice, + default_namespace: ByteSlice, + has_default_namespace: u8, + namespaces: *const SelectorNamespace, + namespace_count: usize, + ) -> SelectorParseResult { + parse_selector( + input, + (has_default_namespace != 0).then_some(default_namespace), + namespaces, + namespace_count, + ) + } + #[no_mangle] pub unsafe extern "C" fn webscene_selector_at( handle: *const c_void, diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_compound.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_compound.h index 6d3d2be7a..0fc0fcfeb 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_compound.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_compound.h @@ -42,6 +42,11 @@ inline bool compound_matches(const Host& host,const dom_node& node, return false; } + if (selector.namespace_uri.has_value() + && node.namespace_uri() != *selector.namespace_uri) { + return false; + } + if (!selector.tag.empty()) { auto wanted_tag = selector.tag; if (!node.xml_mode) wanted_tag = ascii_lower(wanted_tag); diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_payload.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_payload.h index c3e2fa376..7123168a7 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_payload.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_payload.h @@ -43,7 +43,8 @@ inline uint64_t rule_payload_hash( std::string_view selector, const std::vector& declarations, const std::vector& media_queries, - uint32_t cascade_layer_index = 0U) + uint32_t cascade_layer_index = 0U, + std::string_view selector_namespace_key = {}) { auto hash = uint64_t{1469598103934665603ULL}; const auto append = [&](std::string_view value) { @@ -55,6 +56,7 @@ inline uint64_t rule_payload_hash( hash *= 1099511628211ULL; }; append(selector); + append(selector_namespace_key); for (const auto& declaration : declarations) { append(declaration.name); append(declaration.value); @@ -72,9 +74,11 @@ inline bool rule_payload_matches( std::string_view selector, const std::vector& declarations, const std::vector& media_queries, - uint32_t cascade_layer_index = 0U) + uint32_t cascade_layer_index = 0U, + std::string_view selector_namespace_key = {}) { if (payload.selector != selector + || payload.selector_namespace_key != selector_namespace_key || payload.declarations.size() != declarations.size() || payload.media_queries != media_queries || payload.cascade_layer_index != cascade_layer_index) { @@ -96,13 +100,15 @@ std::shared_ptr intern_rule_payload( std::mutex& mutex, rule_payload_cache& payloads, Compile&& compile, std::string selector, const std::vector& declarations, const std::vector& media_queries, - uint32_t cascade_layer_index = 0U) + uint32_t cascade_layer_index = 0U, + std::string selector_namespace_key = {}) { const auto hash = rule_payload_hash( selector, declarations, media_queries, - cascade_layer_index); + cascade_layer_index, + selector_namespace_key); std::lock_guard lock(mutex); auto& candidates = payloads[hash]; for (auto iterator = candidates.begin(); iterator != candidates.end();) { @@ -116,13 +122,15 @@ std::shared_ptr intern_rule_payload( selector, declarations, media_queries, - cascade_layer_index)) { + cascade_layer_index, + selector_namespace_key)) { return candidate; } ++iterator; } auto payload = std::make_shared(); payload->selector = std::move(selector); + payload->selector_namespace_key = std::move(selector_namespace_key); payload->compiled_selector = compile(payload->selector); std::string pseudo_origin; payload->pseudo_kind = static_cast( diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_preparation.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_preparation.h index b813f3add..a083e3398 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_preparation.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_rule_preparation.h @@ -9,7 +9,8 @@ template void prepare_style_rule(std::string_view prelude, std::vector declarations, const std::vector& media, const std::string& address, - Observe&& observe, Append&& append) + Observe&& observe, Append&& append, + const selector_namespace_context* namespaces = nullptr) { for(auto& declaration:declarations) { declaration.value=resolve_resource_urls(std::move(declaration.value),address); @@ -22,7 +23,9 @@ void prepare_style_rule(std::string_view prelude, append(std::string(":host"),declarations,media); return; } - const auto selectors=parse_selector_syntax(prelude); + const auto selectors = namespaces == nullptr + ? parse_selector_syntax(prelude) + : parse_selector_syntax(prelude, *namespaces); if(!selectors) return; for(const auto& selector:selectors.selectors) append(selector.serialized,declarations,media); diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_selectors.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_selectors.h index 6917c0a78..b518b2bac 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_selectors.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_selectors.h @@ -202,7 +202,8 @@ inline size_t find_css_attribute_close( } inline compiled_css_compound compile_css_compound_selector( - std::string_view selector) + std::string_view selector, + const selector_namespace_context* namespaces = nullptr) { compiled_css_compound result; selector = trim_css_view(selector); @@ -211,11 +212,50 @@ inline compiled_css_compound compile_css_compound_selector( size_t cursor = 0U; if (selector.empty()) { // A bare structural pseudo-class has an implicit universal selector. + } else if (selector[cursor] == '|') { + result.namespace_uri = std::string{}; + ++cursor; + if (cursor < selector.size() && selector[cursor] == '*') { + ++cursor; + } else { + result.tag = read_css_identifier(selector, cursor); + if (result.tag.empty()) return result; + } } else if (selector[cursor] == '*') { ++cursor; + if (cursor < selector.size() && selector[cursor] == '|') { + ++cursor; + if (cursor < selector.size() && selector[cursor] == '*') { + ++cursor; + } else { + result.tag = read_css_identifier(selector, cursor); + if (result.tag.empty()) return result; + } + } else if (namespaces != nullptr && namespaces->has_default_namespace) { + result.namespace_uri = namespaces->default_namespace; + } } else if (std::isalpha(static_cast(selector[cursor])) + || selector[cursor] == '_' || selector[cursor] == '-' || selector[cursor] == '\\') { - result.tag = read_css_identifier(selector, cursor); + auto first = read_css_identifier(selector, cursor); + if (cursor < selector.size() && selector[cursor] == '|') { + if (namespaces == nullptr) return result; + const auto found = namespaces->prefixes.find(first); + if (found == namespaces->prefixes.end()) return result; + result.namespace_uri = found->second; + ++cursor; + if (cursor < selector.size() && selector[cursor] == '*') { + ++cursor; + } else { + result.tag = read_css_identifier(selector, cursor); + if (result.tag.empty()) return result; + } + } else { + result.tag = std::move(first); + if (namespaces != nullptr && namespaces->has_default_namespace) { + result.namespace_uri = namespaces->default_namespace; + } + } } while (cursor < selector.size()) { const auto marker = selector[cursor++]; @@ -280,9 +320,13 @@ inline compiled_css_compound compile_css_compound_selector( // Reuse Servo parsing/specificity and the runtime's native compound preparation. -inline compiled_css_selector_list compile_selector_list(std::string_view text) { +inline compiled_css_selector_list compile_selector_list( + std::string_view text, + const selector_namespace_context* namespaces = nullptr) { compiled_css_selector_list result; - const auto parsed=parse_selector_syntax(text); + const auto parsed = namespaces == nullptr + ? parse_selector_syntax(text) + : parse_selector_syntax(text, *namespaces); if(!parsed) return result; for(const auto& source:parsed.selectors) { compiled_css_selector selector; @@ -290,7 +334,8 @@ inline compiled_css_selector_list compile_selector_list(std::string_view text) { selector.combinators=source.combinators; selector.specificity=source.specificity; for(const auto& part:selector.compounds) - selector.compiled_compounds.push_back(compile_css_compound_selector(part)); + selector.compiled_compounds.push_back( + compile_css_compound_selector(part, namespaces)); selector.ancestor_requirements.resize(selector.compiled_compounds.size()); for(size_t i=1;i #include +#include #include #include #include @@ -38,6 +39,8 @@ struct compiled_css_pseudo final { struct compiled_css_compound final { std::string tag; + // Missing means any namespace. An empty value means explicitly no namespace. + std::optional namespace_uri; std::vector> identities; std::vector attributes; std::vector pseudos; @@ -179,6 +182,7 @@ struct compiled_css_selector final { struct css_rule_payload final { std::string selector; + std::string selector_namespace_key; compiled_css_selector compiled_selector; compiled_css_selector compiled_pseudo_origin; // Immutable dependency plans, aligned with the originating selector's diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet.h index a7e2c90fc..205f2b17b 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet.h @@ -45,14 +45,18 @@ std::optional prepare_stylesheet(std::string_view text, void append_parsed_css_style_rule(std::string selector, std::vector declarations, const std::vector& media,const std::string& source, - uint32_t cascade_layer) { + uint32_t cascade_layer, + const selector_namespace_context& namespaces) { const auto previous_count=output.rules.size(); prepare_style_rule(selector,std::move(declarations),media,source, [](const css_declaration&) {}, [&](const std::string& prepared,const auto& values,const auto& conditions) { output.rules.push_back(intern_rule_payload(cache_mutex,cache, - compile_selector,prepared,values,conditions,cascade_layer)); - }); + [&namespaces](std::string_view value) { + return compile_selector(value, &namespaces); + }, prepared, values, conditions, cascade_layer, + namespaces.cache_key())); + }, &namespaces); if(previous_count==output.rules.size()) output.diagnostics.push_back({"selector:"+selector,"unsupported", "selector parser rejected this rule"}); diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet_sink.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet_sink.h index 960d22a66..a18669cb9 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet_sink.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_css_stylesheet_sink.h @@ -147,6 +147,26 @@ class stylesheet_sink final : public css_syntax_sink { "css", "at-rule:@container", "supported", "named and unnamed size conditions are evaluated against the nearest eligible ancestor", "stylesheet-parser"); + } else if (name == "namespace" && stack_.empty() && !has_block) { + const auto parsed = parse_namespace(prelude); + if (parsed.has_value()) { + const auto& [prefix, uri] = *parsed; + if (prefix.has_value()) { + namespaces_.prefixes.insert_or_assign(*prefix, uri); + } else { + namespaces_.default_namespace = uri; + namespaces_.has_default_namespace = true; + } + owner_.record_feature( + "css", "at-rule:@namespace", "supported", + "namespace declarations constrain type and universal selectors", + "stylesheet-parser"); + } else { + owner_.record_feature( + "css", "at-rule:@namespace", "unsupported", + "invalid namespace declaration", "stylesheet-parser"); + } + current.children_active = false; } else { owner_.record_feature( "css", "at-rule:@" + name, "unsupported", {}, @@ -224,7 +244,8 @@ class stylesheet_sink final : public css_syntax_sink { std::move(rule.declarations), rule.media_queries, stylesheet_address_, - rule.cascade_layer); + rule.cascade_layer, + namespaces_); } } else if (current.keyframes) { completed_keyframes_.emplace_back( @@ -242,6 +263,43 @@ class stylesheet_sink final : public css_syntax_sink { } private: + static std::optional, std::string>> + parse_namespace(std::string_view prelude) + { + auto source = trim_css_view(prelude); + if (source.empty()) return std::nullopt; + std::optional prefix; + if (source.front() != '\'' && source.front() != '"' + && !ascii_lower(source).starts_with("url(")) { + size_t cursor = 0U; + auto value = read_css_identifier(source, cursor); + if (value.empty() || cursor == source.size() + || !std::isspace(static_cast(source[cursor]))) { + return std::nullopt; + } + prefix = std::move(value); + source = trim_css_view(source.substr(cursor)); + } + std::string uri; + if (!source.empty() && (source.front() == '\'' || source.front() == '"')) { + const auto quote = source.front(); + if (source.size() < 2U || source.back() != quote) return std::nullopt; + uri.assign(source.substr(1U, source.size() - 2U)); + } else if (ascii_lower(source).starts_with("url(")) { + if (source.back() != ')') return std::nullopt; + source = trim_css_view(source.substr(4U, source.size() - 5U)); + if (source.size() >= 2U + && (source.front() == '\'' || source.front() == '"') + && source.back() == source.front()) { + source = source.substr(1U, source.size() - 2U); + } + uri.assign(source); + } else { + return std::nullopt; + } + return std::pair{std::move(prefix), std::move(uri)}; + } + struct completed_style_rule final { std::string selector; std::vector declarations; @@ -370,6 +428,7 @@ class stylesheet_sink final : public css_syntax_sink { Host& owner_; const std::string& stylesheet_address_; std::vector stack_; + selector_namespace_context namespaces_; std::vector> completed_keyframes_; size_t next_rule_index_{0U}; }; diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.cpp b/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.cpp index 1a8f4768f..1064f274c 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.cpp +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.cpp @@ -355,11 +355,34 @@ uint64_t selector_syntax_persistent_cache_hits() noexcept uint64_t selector_syntax_compilation_count() noexcept { return selector_compilations.load(std::memory_order_relaxed); } -selector_syntax_output parse_selector_syntax(std::string_view input) +std::string selector_namespace_context::cache_key() const +{ + std::vector> ordered( + prefixes.begin(), prefixes.end()); + std::sort(ordered.begin(), ordered.end()); + std::string result = has_default_namespace + ? "d:" + default_namespace : "d-"; + for (const auto& [prefix, uri] : ordered) { + result += '\x1f'; + result += prefix; + result += '='; + result += uri; + } + return result; +} + +namespace { +selector_syntax_output parse_selector_syntax_impl( + std::string_view input, + const selector_namespace_context* namespaces) { - if (auto cached = find_selector_process_cache(input)) return std::move(*cached); - if (auto cached = read_selector_persistent_cache(input)) { - remember_selector(std::string(input), *cached); + auto cache_input = std::string(input); + if (namespaces != nullptr) { + cache_input.insert(0, namespaces->cache_key() + '\x1e'); + } + if (auto cached = find_selector_process_cache(cache_input)) return std::move(*cached); + if (auto cached = read_selector_persistent_cache(cache_input)) { + remember_selector(cache_input, *cached); return std::move(*cached); } @@ -373,7 +396,22 @@ selector_syntax_output parse_selector_syntax(std::string_view input) const auto normalized = encode_wtf8_surrogates(input, transformed_wtf8); const auto parser_input = transformed_wtf8 ? std::string_view(normalized) : input; const auto started = std::chrono::steady_clock::now(); - auto parsed = webscene_selector_parse(borrow(parser_input)); + webscene_selector_parse_result parsed{}; + if (namespaces == nullptr) { + parsed = webscene_selector_parse(borrow(parser_input)); + } else { + std::vector mappings; + mappings.reserve(namespaces->prefixes.size()); + for (const auto& [prefix, uri] : namespaces->prefixes) { + mappings.push_back({borrow(prefix), borrow(uri)}); + } + parsed = webscene_selector_parse_with_namespaces( + borrow(parser_input), + borrow(namespaces->default_namespace), + namespaces->has_default_namespace ? 1U : 0U, + mappings.data(), + mappings.size()); + } const auto finished = std::chrono::steady_clock::now(); output.metrics.duration_ns = static_cast( std::chrono::duration_cast(finished - started).count()); @@ -451,9 +489,22 @@ selector_syntax_output parse_selector_syntax(std::string_view input) } output.selectors.push_back(std::move(selector)); } - remember_selector(std::string(input), output); - write_selector_persistent_cache(input, output); + remember_selector(cache_input, output); + write_selector_persistent_cache(cache_input, output); return output; } +} // namespace + +selector_syntax_output parse_selector_syntax(std::string_view input) +{ + return parse_selector_syntax_impl(input, nullptr); +} + +selector_syntax_output parse_selector_syntax( + std::string_view input, + const selector_namespace_context& namespaces) +{ + return parse_selector_syntax_impl(input, &namespaces); +} } // namespace webscene_native diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.h index 308ea95a1..511f0789b 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser.h @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace webscene_native { @@ -30,6 +31,14 @@ struct selector_syntax_output final { explicit operator bool() const noexcept { return error.empty(); } }; +struct selector_namespace_context final { + std::string default_namespace; + std::unordered_map prefixes; + bool has_default_namespace{false}; + + std::string cache_key() const; +}; + void set_selector_syntax_compilation_cache_directory(std::string directory); void clear_selector_syntax_process_cache(); uint64_t selector_syntax_process_cache_hits() noexcept; @@ -37,5 +46,8 @@ uint64_t selector_syntax_persistent_cache_hits() noexcept; uint64_t selector_syntax_compilation_count() noexcept; selector_syntax_output parse_selector_syntax(std::string_view input); +selector_syntax_output parse_selector_syntax( + std::string_view input, + const selector_namespace_context& namespaces); } // namespace webscene_native diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser_ffi.h b/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser_ffi.h index 5a64104a4..a8af71ea4 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser_ffi.h +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_selector_parser_ffi.h @@ -26,8 +26,19 @@ struct webscene_selector_view final { size_t combinator_count; }; +struct webscene_selector_namespace final { + webscene_selector_byte_slice prefix; + webscene_selector_byte_slice namespace_url; +}; + uint32_t webscene_selector_parser_abi_version(); webscene_selector_parse_result webscene_selector_parse(webscene_selector_byte_slice); +webscene_selector_parse_result webscene_selector_parse_with_namespaces( + webscene_selector_byte_slice, + webscene_selector_byte_slice, + uint8_t, + const webscene_selector_namespace*, + size_t); uint8_t webscene_selector_at(const void*, size_t, webscene_selector_view*); uint8_t webscene_selector_compound_at( const void*, size_t, size_t, webscene_selector_byte_slice*); diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_css_parsing.inc b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_css_parsing.inc index e520b7f59..f30ae4bf5 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_css_parsing.inc +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_css_parsing.inc @@ -541,7 +541,8 @@ static std::shared_ptr intern_css_rule_payload( std::string selector, const std::vector& declarations, - const std::vector& media_queries + const std::vector& media_queries, + const selector_namespace_context* namespaces #if defined(WEBSCENE_NATIVE_ENGINE_SERVO_SELECTORS) , const selector_syntax_selector* parsed_selector = nullptr #endif @@ -552,8 +553,11 @@ #endif return css::intern_rule_payload( shared_css_rule_payload_mutex,shared_css_rule_payloads, - [](std::string_view value) { return compile_css_selector(std::string(value)); }, - std::move(selector),declarations,media_queries); + [namespaces](std::string_view value) { + return compile_css_selector(value, namespaces); + }, + std::move(selector), declarations, media_queries, 0U, + namespaces == nullptr ? std::string{} : namespaces->cache_key()); } uint32_t register_cascade_layer(const std::string& name) @@ -582,7 +586,8 @@ std::string selector, const std::vector& declarations, const std::vector& media_queries, - uint32_t cascade_layer_order = 0U + uint32_t cascade_layer_order, + const selector_namespace_context* namespaces #if defined(WEBSCENE_NATIVE_ENGINE_SERVO_SELECTORS) , const selector_syntax_selector* parsed_selector = nullptr #endif @@ -595,7 +600,8 @@ rule.payload = intern_css_rule_payload( std::move(selector), declarations, - media_queries + media_queries, + namespaces #if defined(WEBSCENE_NATIVE_ENGINE_SERVO_SELECTORS) , parsed_selector #endif @@ -1163,7 +1169,8 @@ std::vector declarations, const std::vector& inherited_media, const std::string& stylesheet_address, - uint32_t cascade_layer_order = 0U) + uint32_t cascade_layer_order = 0U, + const selector_namespace_context& namespaces = {}) { #if defined(WEBSCENE_NATIVE_ENGINE_SERVO_SELECTORS) css::prepare_style_rule(prelude,std::move(declarations),inherited_media,stylesheet_address, @@ -1177,11 +1184,11 @@ start_resource_prefetch(*url,{},WEBSCENE_RESOURCE_IMAGE); } }, - [this, cascade_layer_order](const std::string& selector,const std::vector& values, + [this, cascade_layer_order, &namespaces](const std::string& selector,const std::vector& values, const std::vector& media) { inventory_css_selector(selector); - append_css_rule(selector,values,media,cascade_layer_order); - }); + append_css_rule(selector, values, media, cascade_layer_order, &namespaces); + }, &namespaces); #else for (auto& declaration : declarations) { declaration.value = resolve_css_resource_urls( @@ -1205,7 +1212,8 @@ // selector matching or leaks into the document tree. if (trim_css_view(prelude) == ":host") { inventory_css_selector(":host"); - append_css_rule(":host", declarations, inherited_media, cascade_layer_order); + append_css_rule( + ":host", declarations, inherited_media, cascade_layer_order, &namespaces); return; } size_t selector_cursor = 0U; @@ -1235,7 +1243,7 @@ inventory_css_selector(selector); append_css_rule( std::move(selector), declarations, inherited_media, - cascade_layer_order); + cascade_layer_order, &namespaces); } selector_cursor = index + 1U; } @@ -1999,10 +2007,12 @@ return last; } - static compiled_css_selector compile_css_selector(std::string_view selector) + static compiled_css_selector compile_css_selector( + std::string_view selector, + const selector_namespace_context* namespaces = nullptr) { #if defined(WEBSCENE_NATIVE_ENGINE_SERVO_SELECTORS) - return css::compile_selector(selector); + return css::compile_selector(selector, namespaces); #else std::vector reversed_compounds; std::vector reversed_combinators; @@ -2026,18 +2036,19 @@ result.compiled_compounds.reserve(result.compounds.size()); for (const auto& compound : result.compounds) { result.compiled_compounds.push_back( - compile_css_compound_selector(compound)); + compile_css_compound_selector(compound, namespaces)); } return result; #endif } static compiled_css_selector_list compile_css_selector_list( - std::string_view selector) + std::string_view selector, + const selector_namespace_context* namespaces = nullptr) { compiled_css_selector_list result; #if defined(WEBSCENE_NATIVE_ENGINE_SERVO_SELECTORS) - return css::compile_selector_list(selector); + return css::compile_selector_list(selector, namespaces); #else size_t start = 0; int bracket_depth = 0; @@ -2067,7 +2078,7 @@ else if (!at_end && character == ')') --parenthesis_depth; else if (character == ',' && bracket_depth == 0 && parenthesis_depth == 0) { result.selectors.push_back(compile_css_selector( - selector.substr(start, index - start))); + selector.substr(start, index - start), namespaces)); start = index + 1U; } } diff --git a/experiments/WebScene.NativeEngine.Probe/tests/selector_parser_tests.cpp b/experiments/WebScene.NativeEngine.Probe/tests/selector_parser_tests.cpp index dcb2e408e..b9f8be6f9 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/selector_parser_tests.cpp +++ b/experiments/WebScene.NativeEngine.Probe/tests/selector_parser_tests.cpp @@ -115,6 +115,39 @@ void require_nested_has_selector_list_tokenization() "nested function or quoted attribute comma split an outer :has() arm"); } +void require_namespace_resolution() +{ + using namespace webscene_native; + using namespace webscene_native::css; + selector_namespace_context namespaces; + namespaces.prefixes.emplace( + "svg", "http://www.w3.org/2000/svg"); + namespaces.default_namespace = "urn:webscene:default"; + namespaces.has_default_namespace = true; + + const auto prefixed = compile_selector("svg|circle", &namespaces); + require(prefixed.compiled_compounds.size() == 1U + && prefixed.compiled_compounds[0].tag == "circle" + && prefixed.compiled_compounds[0].namespace_uri + == "http://www.w3.org/2000/svg", + "declared namespace prefix was not retained by compiled matching"); + const auto unprefixed = compile_selector("circle", &namespaces); + require(unprefixed.compiled_compounds.size() == 1U + && unprefixed.compiled_compounds[0].namespace_uri + == "urn:webscene:default", + "default namespace was not applied to an unprefixed type selector"); + const auto any = compile_selector("*|circle", &namespaces); + require(any.compiled_compounds.size() == 1U + && !any.compiled_compounds[0].namespace_uri.has_value(), + "explicit any namespace must not constrain matching"); + const auto empty = compile_selector("|circle", &namespaces); + require(empty.compiled_compounds.size() == 1U + && empty.compiled_compounds[0].namespace_uri == std::string{}, + "explicit empty namespace was not retained"); + require(compile_selector("missing|circle", &namespaces).compounds.empty(), + "undeclared namespace prefix must reject the selector"); +} + void test_compiled_css_invalidation_plans() { using namespace webscene_native::css; @@ -224,6 +257,7 @@ int main() require_validation(); require_wtf8_domstring_round_trip(); require_nested_has_selector_list_tokenization(); + require_namespace_resolution(); test_compiled_css_invalidation_plans(); std::cout << "selector parser tests passed\n"; return 0; diff --git a/tests/NativeWeb/css_service.cpp b/tests/NativeWeb/css_service.cpp index 1aa082199..2ec814d52 100644 --- a/tests/NativeWeb/css_service.cpp +++ b/tests/NativeWeb/css_service.cpp @@ -64,12 +64,13 @@ struct stylesheet_test_host { void append_parsed_css_style_rule(std::string selector, std::vector declarations, const std::vector& media,const std::string& address, - uint32_t = 0U) { + uint32_t = 0U, + const webscene_native::selector_namespace_context& namespaces = {}) { webscene_native::css::prepare_style_rule(selector,std::move(declarations),media,address, [](const auto&) {}, [&](const auto& prepared_selector,const auto& values,const auto& conditions) { rules.push_back({prepared_selector,values,conditions,address}); - }); + }, &namespaces); } }; diff --git a/tests/WebPlatformSubset/contracts/cssom-namespace-selector-matching.html b/tests/WebPlatformSubset/contracts/cssom-namespace-selector-matching.html new file mode 100644 index 000000000..d628ff0fe --- /dev/null +++ b/tests/WebPlatformSubset/contracts/cssom-namespace-selector-matching.html @@ -0,0 +1,80 @@ + + +Dynamic CSS namespace selector matching + + diff --git a/tests/WebPlatformSubset/webscene-cssom-namespace-selector-matching-profile.json b/tests/WebPlatformSubset/webscene-cssom-namespace-selector-matching-profile.json new file mode 100644 index 000000000..c2724f993 --- /dev/null +++ b/tests/WebPlatformSubset/webscene-cssom-namespace-selector-matching-profile.json @@ -0,0 +1,22 @@ +{ + "profile": "webscene-cssom-namespace-selector-matching-1", + "wptRevision": "2c705104a295c48053eeddf7fe0170d790a4e853", + "runtime": "v8", + "viewport": { "width": 800, "height": 600, "deviceScaleFactor": 1 }, + "required": [ + { + "path": "contracts/cssom-namespace-selector-matching.html", + "type": "contract", + "capabilities": [ + "cssom-namespace-prefixed-type-selector", + "cssom-namespace-universal-type-selector", + "cssom-namespace-default-type-selector", + "cssom-namespace-dynamic-recascade" + ], + "evidence": ["cssom-namespace-selector-matching-239"], + "reason": "CSS Namespaces and CSSOM regression for dynamic prefixed, any, empty, and default namespace type-selector matching across owner-backed and adopted stylesheets." + } + ], + "harnessBlocked": [], + "excluded": [] +}