Просмотр исходного кода

LibWeb: Cache the "id" attribute value on DOM::Element as FlyString

This will allow us to do O(1) checks against the element ID when
matching selectors, etc.
Andreas Kling 1 год назад
Родитель
Сommit
1c62ee9396

+ 6 - 1
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -478,7 +478,12 @@ void Element::attribute_changed(FlyString const& name, Optional<DeprecatedString
 {
     auto value_or_empty = value.value_or("");
 
-    if (name == HTML::AttributeNames::class_) {
+    if (name == HTML::AttributeNames::id) {
+        if (!value.has_value())
+            m_id = {};
+        else
+            m_id = MUST(FlyString::from_deprecated_fly_string(value_or_empty));
+    } else if (name == HTML::AttributeNames::class_) {
         auto new_classes = value_or_empty.split_view(Infra::is_ascii_whitespace);
         m_classes.clear();
         m_classes.ensure_capacity(new_classes.size());

+ 4 - 0
Userland/Libraries/LibWeb/DOM/Element.h

@@ -375,6 +375,8 @@ public:
     };
     Directionality directionality() const;
 
+    Optional<FlyString> const& id() const { return m_id; }
+
 protected:
     Element(Document&, DOM::QualifiedName);
     virtual void initialize(JS::Realm&) override;
@@ -411,6 +413,8 @@ private:
     Vector<FlyString> m_classes;
     Optional<Dir> m_dir;
 
+    Optional<FlyString> m_id;
+
     Array<JS::GCPtr<Layout::Node>, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)> m_pseudo_element_nodes;
 
     // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reaction-queue