From 14349f058a8a8f3ca47e271aeb3bc53eb0047718 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 18 Oct 2021 13:21:23 -0400 Subject: [PATCH] LibWeb: Implement the Element classList attribute And ensure it is updated (if it exists) when the 'class' attribute is changed. --- Userland/Libraries/LibWeb/DOM/Element.cpp | 10 ++++++++++ Userland/Libraries/LibWeb/DOM/Element.h | 3 +++ Userland/Libraries/LibWeb/DOM/Element.idl | 1 + 3 files changed, 14 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 58c476de80..b01bbd8f0a 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -171,6 +172,8 @@ void Element::parse_attribute(const FlyString& name, const String& value) for (auto& new_class : new_classes) { m_classes.unchecked_append(new_class); } + if (m_class_list) + m_class_list->associated_attribute_changed(value); } else if (name == HTML::AttributeNames::style) { auto parsed_style = parse_css_declaration(CSS::ParsingContext(document()), value); if (!parsed_style.is_null()) { @@ -256,6 +259,13 @@ NonnullRefPtr Element::computed_style() return properties; } +RefPtr const& Element::class_list() +{ + if (!m_class_list) + m_class_list = DOMTokenList::create(*this, HTML::AttributeNames::class_); + return m_class_list; +} + // https://dom.spec.whatwg.org/#dom-element-matches DOM::ExceptionOr Element::matches(StringView selectors) const { diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 5ae982baa0..7f2d3f4fb1 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -58,6 +58,8 @@ public: size_t attribute_list_size() const { return m_attributes->length(); } NonnullRefPtr const& attributes() const { return m_attributes; } + RefPtr const& class_list(); + DOM::ExceptionOr matches(StringView selectors) const; int client_top() const; @@ -140,6 +142,7 @@ private: RefPtr m_specified_css_values; HashMap m_custom_properties; + RefPtr m_class_list; Vector m_classes; RefPtr m_shadow_root; diff --git a/Userland/Libraries/LibWeb/DOM/Element.idl b/Userland/Libraries/LibWeb/DOM/Element.idl index aa5ef33882..21a2bfe011 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.idl +++ b/Userland/Libraries/LibWeb/DOM/Element.idl @@ -20,6 +20,7 @@ interface Element : Node { [Reflect] attribute DOMString id; [Reflect=class] attribute DOMString className; + [SameObject, PutForwards=value] readonly attribute DOMTokenList classList; boolean matches(DOMString selectors);