1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:27:45 +00:00

LibWeb: Port DOMTokenList from DeprecatedString to String

This commit is contained in:
Shannon Booth 2023-08-12 21:30:21 +12:00 committed by Andreas Kling
parent d706f9f241
commit b0eea51335
4 changed files with 41 additions and 41 deletions

View file

@ -15,20 +15,20 @@
namespace { namespace {
// https://infra.spec.whatwg.org/#set-append // https://infra.spec.whatwg.org/#set-append
inline void append_to_ordered_set(Vector<DeprecatedString>& set, DeprecatedString item) inline void append_to_ordered_set(Vector<String>& set, String item)
{ {
if (!set.contains_slow(item)) if (!set.contains_slow(item))
set.append(move(item)); set.append(move(item));
} }
// https://infra.spec.whatwg.org/#list-remove // https://infra.spec.whatwg.org/#list-remove
inline void remove_from_ordered_set(Vector<DeprecatedString>& set, StringView item) inline void remove_from_ordered_set(Vector<String>& set, StringView item)
{ {
set.remove_first_matching([&](auto const& value) { return value == item; }); set.remove_first_matching([&](auto const& value) { return value == item; });
} }
// https://infra.spec.whatwg.org/#set-replace // https://infra.spec.whatwg.org/#set-replace
inline void replace_in_ordered_set(Vector<DeprecatedString>& set, StringView item, DeprecatedString replacement) inline void replace_in_ordered_set(Vector<String>& set, String const& item, String replacement)
{ {
auto item_index = set.find_first_index(item); auto item_index = set.find_first_index(item);
VERIFY(item_index.has_value()); VERIFY(item_index.has_value());
@ -52,19 +52,19 @@ inline void replace_in_ordered_set(Vector<DeprecatedString>& set, StringView ite
namespace Web::DOM { namespace Web::DOM {
JS::NonnullGCPtr<DOMTokenList> DOMTokenList::create(Element& associated_element, DeprecatedFlyString associated_attribute) JS::NonnullGCPtr<DOMTokenList> DOMTokenList::create(Element& associated_element, FlyString associated_attribute)
{ {
auto& realm = associated_element.realm(); auto& realm = associated_element.realm();
return realm.heap().allocate<DOMTokenList>(realm, associated_element, move(associated_attribute)); return realm.heap().allocate<DOMTokenList>(realm, associated_element, move(associated_attribute));
} }
// https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A2 // https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A2
DOMTokenList::DOMTokenList(Element& associated_element, DeprecatedFlyString associated_attribute) DOMTokenList::DOMTokenList(Element& associated_element, FlyString associated_attribute)
: Bindings::LegacyPlatformObject(associated_element.realm()) : Bindings::LegacyPlatformObject(associated_element.realm())
, m_associated_element(associated_element) , m_associated_element(associated_element)
, m_associated_attribute(move(associated_attribute)) , m_associated_attribute(move(associated_attribute))
{ {
auto value = associated_element.get_attribute(m_associated_attribute); auto value = associated_element.get_attribute(m_associated_attribute.to_deprecated_fly_string());
associated_attribute_changed(value); associated_attribute_changed(value);
} }
@ -90,7 +90,7 @@ void DOMTokenList::associated_attribute_changed(StringView value)
auto split_values = value.split_view_if(Infra::is_ascii_whitespace); auto split_values = value.split_view_if(Infra::is_ascii_whitespace);
for (auto const& split_value : split_values) for (auto const& split_value : split_values)
append_to_ordered_set(m_token_set, split_value); append_to_ordered_set(m_token_set, String::from_utf8(split_value).release_value_but_fixme_should_propagate_errors());
} }
// https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A3 // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A3
@ -100,26 +100,24 @@ bool DOMTokenList::is_supported_property_index(u32 index) const
} }
// https://dom.spec.whatwg.org/#dom-domtokenlist-item // https://dom.spec.whatwg.org/#dom-domtokenlist-item
DeprecatedString const& DOMTokenList::item(size_t index) const Optional<String> DOMTokenList::item(size_t index) const
{ {
static const DeprecatedString null_string {};
// 1. If index is equal to or greater than thiss token sets size, then return null. // 1. If index is equal to or greater than thiss token sets size, then return null.
if (index >= m_token_set.size()) if (index >= m_token_set.size())
return null_string; return {};
// 2. Return thiss token set[index]. // 2. Return thiss token set[index].
return m_token_set[index]; return m_token_set[index];
} }
// https://dom.spec.whatwg.org/#dom-domtokenlist-contains // https://dom.spec.whatwg.org/#dom-domtokenlist-contains
bool DOMTokenList::contains(StringView token) bool DOMTokenList::contains(String const& token)
{ {
return m_token_set.contains_slow(token); return m_token_set.contains_slow(token);
} }
// https://dom.spec.whatwg.org/#dom-domtokenlist-add // https://dom.spec.whatwg.org/#dom-domtokenlist-add
WebIDL::ExceptionOr<void> DOMTokenList::add(Vector<DeprecatedString> const& tokens) WebIDL::ExceptionOr<void> DOMTokenList::add(Vector<String> const& tokens)
{ {
// 1. For each token in tokens: // 1. For each token in tokens:
for (auto const& token : tokens) { for (auto const& token : tokens) {
@ -137,7 +135,7 @@ WebIDL::ExceptionOr<void> DOMTokenList::add(Vector<DeprecatedString> const& toke
} }
// https://dom.spec.whatwg.org/#dom-domtokenlist-remove // https://dom.spec.whatwg.org/#dom-domtokenlist-remove
WebIDL::ExceptionOr<void> DOMTokenList::remove(Vector<DeprecatedString> const& tokens) WebIDL::ExceptionOr<void> DOMTokenList::remove(Vector<String> const& tokens)
{ {
// 1. For each token in tokens: // 1. For each token in tokens:
for (auto const& token : tokens) { for (auto const& token : tokens) {
@ -155,7 +153,7 @@ WebIDL::ExceptionOr<void> DOMTokenList::remove(Vector<DeprecatedString> const& t
} }
// https://dom.spec.whatwg.org/#dom-domtokenlist-toggle // https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
WebIDL::ExceptionOr<bool> DOMTokenList::toggle(DeprecatedString const& token, Optional<bool> force) WebIDL::ExceptionOr<bool> DOMTokenList::toggle(String const& token, Optional<bool> force)
{ {
// 1. If token is the empty string, then throw a "SyntaxError" DOMException. // 1. If token is the empty string, then throw a "SyntaxError" DOMException.
// 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException. // 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
@ -186,7 +184,7 @@ WebIDL::ExceptionOr<bool> DOMTokenList::toggle(DeprecatedString const& token, Op
} }
// https://dom.spec.whatwg.org/#dom-domtokenlist-replace // https://dom.spec.whatwg.org/#dom-domtokenlist-replace
WebIDL::ExceptionOr<bool> DOMTokenList::replace(DeprecatedString const& token, DeprecatedString const& new_token) WebIDL::ExceptionOr<bool> DOMTokenList::replace(String const& token, String const& new_token)
{ {
// 1. If either token or newToken is the empty string, then throw a "SyntaxError" DOMException. // 1. If either token or newToken is the empty string, then throw a "SyntaxError" DOMException.
// 2. If either token or newToken contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException. // 2. If either token or newToken contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
@ -222,21 +220,21 @@ WebIDL::ExceptionOr<bool> DOMTokenList::supports([[maybe_unused]] StringView tok
} }
// https://dom.spec.whatwg.org/#dom-domtokenlist-value // https://dom.spec.whatwg.org/#dom-domtokenlist-value
DeprecatedString DOMTokenList::value() const String DOMTokenList::value() const
{ {
StringBuilder builder; StringBuilder builder;
builder.join(' ', m_token_set); builder.join(' ', m_token_set);
return builder.to_deprecated_string(); return MUST(builder.to_string());
} }
// https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2 // https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2
void DOMTokenList::set_value(DeprecatedString value) void DOMTokenList::set_value(String const& value)
{ {
JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr(); JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
if (!associated_element) if (!associated_element)
return; return;
MUST(associated_element->set_attribute(m_associated_attribute, move(value))); MUST(associated_element->set_attribute(m_associated_attribute.to_deprecated_fly_string(), value.to_deprecated_string()));
} }
WebIDL::ExceptionOr<void> DOMTokenList::validate_token(StringView token) const WebIDL::ExceptionOr<void> DOMTokenList::validate_token(StringView token) const
@ -255,20 +253,22 @@ void DOMTokenList::run_update_steps()
if (!associated_element) if (!associated_element)
return; return;
auto deprecated_attribute = m_associated_attribute.to_deprecated_fly_string();
// 1. If the associated element does not have an associated attribute and token set is empty, then return. // 1. If the associated element does not have an associated attribute and token set is empty, then return.
if (!associated_element->has_attribute(m_associated_attribute) && m_token_set.is_empty()) if (!associated_element->has_attribute(deprecated_attribute) && m_token_set.is_empty())
return; return;
// 2. Set an attribute value for the associated element using associated attributes local name and the result of running the ordered set serializer for token set. // 2. Set an attribute value for the associated element using associated attributes 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(deprecated_attribute, value().to_deprecated_string()));
} }
WebIDL::ExceptionOr<JS::Value> DOMTokenList::item_value(size_t index) const WebIDL::ExceptionOr<JS::Value> DOMTokenList::item_value(size_t index) const
{ {
auto const& string = item(index); auto string = item(index);
if (string.is_null()) if (!string.has_value())
return JS::js_undefined(); return JS::js_undefined();
return JS::PrimitiveString::create(vm(), string); return JS::PrimitiveString::create(vm(), string.release_value());
} }
} }

View file

@ -8,9 +8,9 @@
#pragma once #pragma once
#include <AK/DeprecatedFlyString.h> #include <AK/FlyString.h>
#include <AK/DeprecatedString.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibWeb/Bindings/LegacyPlatformObject.h> #include <LibWeb/Bindings/LegacyPlatformObject.h>
@ -24,7 +24,7 @@ class DOMTokenList final : public Bindings::LegacyPlatformObject {
WEB_PLATFORM_OBJECT(DOMTokenList, Bindings::LegacyPlatformObject); WEB_PLATFORM_OBJECT(DOMTokenList, Bindings::LegacyPlatformObject);
public: public:
[[nodiscard]] static JS::NonnullGCPtr<DOMTokenList> create(Element& associated_element, DeprecatedFlyString associated_attribute); [[nodiscard]] static JS::NonnullGCPtr<DOMTokenList> create(Element& associated_element, FlyString associated_attribute);
~DOMTokenList() = default; ~DOMTokenList() = default;
void associated_attribute_changed(StringView value); void associated_attribute_changed(StringView value);
@ -33,18 +33,18 @@ public:
virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override; virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
size_t length() const { return m_token_set.size(); } size_t length() const { return m_token_set.size(); }
DeprecatedString const& item(size_t index) const; Optional<String> item(size_t index) const;
bool contains(StringView token); bool contains(String const& token);
WebIDL::ExceptionOr<void> add(Vector<DeprecatedString> const& tokens); WebIDL::ExceptionOr<void> add(Vector<String> const& tokens);
WebIDL::ExceptionOr<void> remove(Vector<DeprecatedString> const& tokens); WebIDL::ExceptionOr<void> remove(Vector<String> const& tokens);
WebIDL::ExceptionOr<bool> toggle(DeprecatedString const& token, Optional<bool> force); WebIDL::ExceptionOr<bool> toggle(String const& token, Optional<bool> force);
WebIDL::ExceptionOr<bool> replace(DeprecatedString const& token, DeprecatedString const& new_token); WebIDL::ExceptionOr<bool> replace(String const& token, String const& new_token);
WebIDL::ExceptionOr<bool> supports(StringView token); WebIDL::ExceptionOr<bool> supports(StringView token);
DeprecatedString value() const; String value() const;
void set_value(DeprecatedString value); void set_value(String const& value);
private: private:
DOMTokenList(Element& associated_element, DeprecatedFlyString associated_attribute); DOMTokenList(Element& associated_element, FlyString associated_attribute);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override; virtual void visit_edges(Cell::Visitor&) override;
@ -66,8 +66,8 @@ private:
void run_update_steps(); void run_update_steps();
JS::NonnullGCPtr<Element> m_associated_element; JS::NonnullGCPtr<Element> m_associated_element;
DeprecatedFlyString m_associated_attribute; FlyString m_associated_attribute;
Vector<DeprecatedString> m_token_set; Vector<String> m_token_set;
}; };
} }

View file

@ -1,5 +1,5 @@
// https://dom.spec.whatwg.org/#interface-domtokenlist // https://dom.spec.whatwg.org/#interface-domtokenlist
[Exposed=Window] [Exposed=Window, UseNewAKString]
interface DOMTokenList { interface DOMTokenList {
readonly attribute unsigned long length; readonly attribute unsigned long length;
getter DOMString? item(unsigned long index); getter DOMString? item(unsigned long index);

View file

@ -507,7 +507,7 @@ NonnullRefPtr<CSS::StyleProperties> Element::resolved_css_values()
DOMTokenList* Element::class_list() DOMTokenList* Element::class_list()
{ {
if (!m_class_list) if (!m_class_list)
m_class_list = DOMTokenList::create(*this, HTML::AttributeNames::class_); m_class_list = DOMTokenList::create(*this, FlyString::from_deprecated_fly_string(HTML::AttributeNames::class_).release_value());
return m_class_list; return m_class_list;
} }