summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-10-18 13:21:23 -0400
committerAndreas Kling <kling@serenityos.org>2021-10-18 23:33:56 +0200
commit14349f058a8a8f3ca47e271aeb3bc53eb0047718 (patch)
tree232aa7e6e35e42f0ad1b00e268d46fbe10d82ff8 /Userland/Libraries
parentd24ae8063b154c997c91ff004d668ed16d1b4a7f (diff)
downloadserenity-14349f058a8a8f3ca47e271aeb3bc53eb0047718.zip
LibWeb: Implement the Element classList attribute
And ensure it is updated (if it exists) when the 'class' attribute is changed.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.cpp10
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.h3
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.idl1
3 files changed, 14 insertions, 0 deletions
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 <LibWeb/CSS/SelectorEngine.h>
#include <LibWeb/CSS/StyleInvalidator.h>
#include <LibWeb/DOM/DOMException.h>
+#include <LibWeb/DOM/DOMTokenList.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/ExceptionOr.h>
@@ -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<CSS::StyleProperties> Element::computed_style()
return properties;
}
+RefPtr<DOMTokenList> 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<bool> 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<NamedNodeMap> const& attributes() const { return m_attributes; }
+ RefPtr<DOMTokenList> const& class_list();
+
DOM::ExceptionOr<bool> matches(StringView selectors) const;
int client_top() const;
@@ -140,6 +142,7 @@ private:
RefPtr<CSS::StyleProperties> m_specified_css_values;
HashMap<String, CSS::StyleComputer::CustomPropertyResolutionTuple> m_custom_properties;
+ RefPtr<DOMTokenList> m_class_list;
Vector<FlyString> m_classes;
RefPtr<ShadowRoot> 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);