From 4b7b726888ca391252a97eb715a710727b8f9f15 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 4 Aug 2023 17:02:29 +0100 Subject: [PATCH] LibWeb: Track all namespace rules in a CSSStyleSheet, and expose them --- Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp | 15 ++++++++++++++- Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index 9e2012f18a7..e3e7b779d87 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -50,6 +50,8 @@ void CSSStyleSheet::visit_edges(Cell::Visitor& visitor) visitor.visit(m_rules); visitor.visit(m_owner_css_rule); visitor.visit(m_default_namespace_rule); + for (auto& [key, namespace_rule] : m_namespace_rules) + visitor.visit(namespace_rule); } // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule @@ -150,8 +152,19 @@ Optional CSSStyleSheet::default_namespace() const return {}; } +Optional CSSStyleSheet::namespace_uri(StringView namespace_prefix) const +{ + return m_namespace_rules.get(namespace_prefix) + .map([](JS::GCPtr namespace_) { + return namespace_->namespace_uri().view(); + }); +} + void CSSStyleSheet::recalculate_namespaces() { + m_default_namespace_rule = nullptr; + m_namespace_rules.clear(); + for (JS::NonnullGCPtr rule : *m_rules) { // "Any @namespace rules must follow all @charset and @import rules and precede all other // non-ignored at-rules and style rules in a style sheet. @@ -174,7 +187,7 @@ void CSSStyleSheet::recalculate_namespaces() if (!namespace_rule.namespace_uri().is_empty() && namespace_rule.prefix().is_empty()) m_default_namespace_rule = namespace_rule; - // FIXME: Store qualified namespace rules. + m_namespace_rules.set(FlyString::from_deprecated_fly_string(namespace_rule.prefix()).release_value_but_fixme_should_propagate_errors(), namespace_rule); } } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index 2f14dc17aca..452a84d7b93 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -50,6 +50,7 @@ public: void set_style_sheet_list(Badge, StyleSheetList*); Optional default_namespace() const; + Optional namespace_uri(StringView namespace_prefix) const; private: CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional location); @@ -61,6 +62,7 @@ private: JS::GCPtr m_rules; JS::GCPtr m_default_namespace_rule; + HashMap> m_namespace_rules; JS::GCPtr m_style_sheet_list; JS::GCPtr m_owner_css_rule;