Skip to content

Commit

Permalink
LibWeb: Follow the specification steps to serialize a DOMTokenList
Browse files Browse the repository at this point in the history
This ensures that calling `element.classList.toString()` always
produces the correct value.
  • Loading branch information
tcl3 committed Jul 24, 2024
1 parent f2ebd0e commit 25dfe08
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Tests/LibWeb/Text/expected/DOM/Element-classList.txt
Original file line number Diff line number Diff line change
@@ -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 "
4 changes: 4 additions & 0 deletions Tests/LibWeb/Text/input/DOM/Element-classList.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"`);
});
</script>
</html>
12 changes: 9 additions & 3 deletions Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,20 @@ WebIDL::ExceptionOr<bool> 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)
{
Expand Down Expand Up @@ -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<JS::Value> DOMTokenList::item_value(size_t index) const
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/DOM/DOMTokenList.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class DOMTokenList final : public Bindings::PlatformObject {
WebIDL::ExceptionOr<void> validate_token(StringView token) const;
void run_update_steps();

String serialize_ordered_set() const;

JS::NonnullGCPtr<Element> m_associated_element;
FlyString m_associated_attribute;
Vector<String> m_token_set;
Expand Down

0 comments on commit 25dfe08

Please sign in to comment.