mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-03 07:07:23 +00:00
LibWeb: Speed up CSS namespace checking
CSSStyleSheet now caches the CSSNamespaceRule for the default namespace, which is the only one we currently care about. This saves us from iterating over its list of rules every time we want to know what that default namespace is. The spec dictates that `@namespace` rules are only valid near the start of a stylesheet, so we also take advantage of that to quit searching for namespaces as soon as we see a non-import rule. Also renamed `namespace_filter()` to `default_namespace()` since that's what it actually returns. This makes github.com/serenityos/serenity snappy again. :^)
This commit is contained in:
committed by
Andreas Kling
parent
496db17c2d
commit
0805060e5e
@@ -6,7 +6,6 @@
|
||||
|
||||
#include <LibWeb/Bindings/CSSStyleSheetPrototype.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/CSS/CSSNamespaceRule.h>
|
||||
#include <LibWeb/CSS/CSSStyleSheet.h>
|
||||
#include <LibWeb/CSS/Parser/Parser.h>
|
||||
#include <LibWeb/CSS/StyleComputer.h>
|
||||
@@ -30,6 +29,12 @@ CSSStyleSheet::CSSStyleSheet(JS::Realm& realm, CSSRuleList& rules, MediaList& me
|
||||
|
||||
for (auto& rule : *m_rules)
|
||||
rule->set_parent_style_sheet(this);
|
||||
|
||||
recalculate_namespaces();
|
||||
|
||||
m_rules->on_change = [this]() {
|
||||
recalculate_namespaces();
|
||||
};
|
||||
}
|
||||
|
||||
JS::ThrowCompletionOr<void> CSSStyleSheet::initialize(JS::Realm& realm)
|
||||
@@ -138,17 +143,40 @@ void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList*
|
||||
m_style_sheet_list = list;
|
||||
}
|
||||
|
||||
Optional<StringView> CSSStyleSheet::namespace_filter() const
|
||||
Optional<StringView> CSSStyleSheet::default_namespace() const
|
||||
{
|
||||
for (JS::NonnullGCPtr<CSSRule> rule : *m_rules) {
|
||||
if (rule->type() == CSSRule::Type::Namespace) {
|
||||
auto& namespace_rule = verify_cast<CSSNamespaceRule>(*rule);
|
||||
if (!namespace_rule.namespace_uri().is_empty() && namespace_rule.prefix().is_empty())
|
||||
return namespace_rule.namespace_uri().view();
|
||||
}
|
||||
}
|
||||
if (m_default_namespace_rule)
|
||||
return m_default_namespace_rule->namespace_uri().view();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void CSSStyleSheet::recalculate_namespaces()
|
||||
{
|
||||
for (JS::NonnullGCPtr<CSSRule> 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.
|
||||
// ...
|
||||
// A syntactically invalid @namespace rule (whether malformed or misplaced) must be ignored."
|
||||
// https://drafts.csswg.org/css-namespaces/#syntax
|
||||
switch (rule->type()) {
|
||||
case CSSRule::Type::Import:
|
||||
continue;
|
||||
|
||||
case CSSRule::Type::Namespace:
|
||||
break;
|
||||
|
||||
default:
|
||||
// Any other types mean that further @namespace rules are invalid, so we can stop here.
|
||||
return;
|
||||
}
|
||||
|
||||
auto& namespace_rule = verify_cast<CSSNamespaceRule>(*rule);
|
||||
if (!namespace_rule.namespace_uri().is_empty() && namespace_rule.prefix().is_empty())
|
||||
m_default_namespace_rule = namespace_rule;
|
||||
|
||||
// FIXME: Store qualified namespace rules.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user