From 432536f0b3c248b45b290379052f31296f92b183 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 19 Mar 2024 10:34:32 +0100 Subject: [PATCH] LibWeb: Make SelectorEngine::matches_namespace() a standalone function This will allow me to reuse it in the next commit. --- .../Libraries/LibWeb/CSS/SelectorEngine.cpp | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp index 720b06269ad..f9836e63353 100644 --- a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp +++ b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp @@ -547,6 +547,38 @@ static inline bool matches_pseudo_class(CSS::Selector::SimpleSelector::PseudoCla return false; } +static ALWAYS_INLINE bool matches_namespace( + CSS::Selector::SimpleSelector::QualifiedName const& qualified_name, + DOM::Element const& element, + Optional style_sheet_for_rule) +{ + switch (qualified_name.namespace_type) { + case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Default: + // "if no default namespace has been declared for selectors, this is equivalent to *|E." + if (!style_sheet_for_rule.has_value() || !style_sheet_for_rule->default_namespace_rule()) + return true; + // "Otherwise it is equivalent to ns|E where ns is the default namespace." + return element.namespace_uri() == style_sheet_for_rule->default_namespace_rule()->namespace_uri(); + case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::None: + // "elements with name E without a namespace" + return !element.namespace_uri().has_value(); + case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Any: + // "elements with name E in any namespace, including those without a namespace" + return true; + case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Named: + // "elements with name E in namespace ns" + // Unrecognized namespace prefixes are invalid, so don't match. + // (We can't detect this at parse time, since a namespace rule may be inserted later.) + // So, if we don't have a context to look up namespaces from, we fail to match. + if (!style_sheet_for_rule.has_value()) + return false; + + auto selector_namespace = style_sheet_for_rule->namespace_uri(qualified_name.namespace_); + return selector_namespace.has_value() && selector_namespace.value() == element.namespace_uri(); + } + VERIFY_NOT_REACHED(); +} + static inline bool matches(CSS::Selector::SimpleSelector const& component, Optional style_sheet_for_rule, DOM::Element const& element, JS::GCPtr scope) { switch (component.type) { @@ -565,32 +597,7 @@ static inline bool matches(CSS::Selector::SimpleSelector const& component, Optio } } - // Match the namespace - switch (qualified_name.namespace_type) { - case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Default: - // "if no default namespace has been declared for selectors, this is equivalent to *|E." - if (!style_sheet_for_rule.has_value() || !style_sheet_for_rule->default_namespace_rule()) - return true; - // "Otherwise it is equivalent to ns|E where ns is the default namespace." - return element.namespace_uri() == style_sheet_for_rule->default_namespace_rule()->namespace_uri(); - case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::None: - // "elements with name E without a namespace" - return !element.namespace_uri().has_value(); - case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Any: - // "elements with name E in any namespace, including those without a namespace" - return true; - case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Named: - // "elements with name E in namespace ns" - // Unrecognized namespace prefixes are invalid, so don't match. - // (We can't detect this at parse time, since a namespace rule may be inserted later.) - // So, if we don't have a context to look up namespaces from, we fail to match. - if (!style_sheet_for_rule.has_value()) - return false; - - auto selector_namespace = style_sheet_for_rule->namespace_uri(qualified_name.namespace_); - return selector_namespace.has_value() && selector_namespace.value() == element.namespace_uri(); - } - VERIFY_NOT_REACHED(); + return matches_namespace(qualified_name, element, style_sheet_for_rule); } case CSS::Selector::SimpleSelector::Type::Id: return component.name() == element.id();