From 25dfe0832d2531639748eb90f3708cec389b8819 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Tue, 23 Jul 2024 13:09:11 +0100 Subject: [PATCH] LibWeb: Follow the specification steps to serialize a DOMTokenList This ensures that calling `element.classList.toString()` always produces the correct value. --- Tests/LibWeb/Text/expected/DOM/Element-classList.txt | 2 ++ Tests/LibWeb/Text/input/DOM/Element-classList.html | 4 ++++ Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp | 12 +++++++++--- Userland/Libraries/LibWeb/DOM/DOMTokenList.h | 2 ++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Tests/LibWeb/Text/expected/DOM/Element-classList.txt b/Tests/LibWeb/Text/expected/DOM/Element-classList.txt index c3a080681e6c..c9b7f958521a 100644 --- a/Tests/LibWeb/Text/expected/DOM/Element-classList.txt +++ b/Tests/LibWeb/Text/expected/DOM/Element-classList.txt @@ -1,3 +1,5 @@ element.classList initial value: "" element.classList after setting classList to "a": "a" element.classList after setting className to "": "" +element.classList after setting to className to "a a b c": "a a b c" +element.classList after setting to className to " a a b c ": " a a b c " diff --git a/Tests/LibWeb/Text/input/DOM/Element-classList.html b/Tests/LibWeb/Text/input/DOM/Element-classList.html index 22b76d884272..72d7f53c2657 100644 --- a/Tests/LibWeb/Text/input/DOM/Element-classList.html +++ b/Tests/LibWeb/Text/input/DOM/Element-classList.html @@ -8,6 +8,10 @@ println(`element.classList after setting classList to "a": "${element.classList.toString()}"`); element.className = ""; println(`element.classList after setting className to "": "${element.classList.toString()}"`); + element.className = "a a b c"; + println(`element.classList after setting to className to "a a b c": "${element.classList.toString()}"`); + element.className = " a a b c "; + println(`element.classList after setting to className to " a a b c ": "${element.classList.toString()}"`); }); diff --git a/Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp b/Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp index 90a759059b92..dbbd7b57c2ec 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp +++ b/Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp @@ -239,14 +239,20 @@ WebIDL::ExceptionOr DOMTokenList::supports(StringView token) return false; } -// https://dom.spec.whatwg.org/#dom-domtokenlist-value -String DOMTokenList::value() const +// https://dom.spec.whatwg.org/#concept-ordered-set-serializer +String DOMTokenList::serialize_ordered_set() const { StringBuilder builder; builder.join(' ', m_token_set); return MUST(builder.to_string()); } +// https://dom.spec.whatwg.org/#dom-domtokenlist-value +String DOMTokenList::value() const +{ + return m_associated_element->get_attribute_value(m_associated_attribute); +} + // https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2 void DOMTokenList::set_value(String const& value) { @@ -278,7 +284,7 @@ void DOMTokenList::run_update_steps() return; // 2. Set an attribute value for the associated element using associated attribute’s local name and the result of running the ordered set serializer for token set. - MUST(associated_element->set_attribute(m_associated_attribute, value())); + MUST(associated_element->set_attribute(m_associated_attribute, serialize_ordered_set())); } WebIDL::ExceptionOr DOMTokenList::item_value(size_t index) const diff --git a/Userland/Libraries/LibWeb/DOM/DOMTokenList.h b/Userland/Libraries/LibWeb/DOM/DOMTokenList.h index 97e8611df8c5..a496aaa9e2eb 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMTokenList.h +++ b/Userland/Libraries/LibWeb/DOM/DOMTokenList.h @@ -53,6 +53,8 @@ class DOMTokenList final : public Bindings::PlatformObject { WebIDL::ExceptionOr validate_token(StringView token) const; void run_update_steps(); + String serialize_ordered_set() const; + JS::NonnullGCPtr m_associated_element; FlyString m_associated_attribute; Vector m_token_set;