Skip to content

Commit

Permalink
LibWeb: Port DOMTokenList from DeprecatedString to String
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonbooth authored and awesomekling committed Aug 27, 2023
1 parent d706f9f commit b0eea51
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
50 changes: 25 additions & 25 deletions Userland/Libraries/LibWeb/DOM/DOMTokenList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
namespace {

// 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))
set.append(move(item));
}

// 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; });
}

// 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);
VERIFY(item_index.has_value());
Expand All @@ -52,19 +52,19 @@ inline void replace_in_ordered_set(Vector<DeprecatedString>& set, StringView ite

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();
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
DOMTokenList::DOMTokenList(Element& associated_element, DeprecatedFlyString associated_attribute)
DOMTokenList::DOMTokenList(Element& associated_element, FlyString associated_attribute)
: Bindings::LegacyPlatformObject(associated_element.realm())
, m_associated_element(associated_element)
, 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);
}

Expand All @@ -90,7 +90,7 @@ void DOMTokenList::associated_attribute_changed(StringView value)

auto split_values = value.split_view_if(Infra::is_ascii_whitespace);
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
Expand All @@ -100,26 +100,24 @@ bool DOMTokenList::is_supported_property_index(u32 index) const
}

// 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 this’s token set’s size, then return null.
if (index >= m_token_set.size())
return null_string;
return {};

// 2. Return this’s token set[index].
return m_token_set[index];
}

// 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);
}

// 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:
for (auto const& token : tokens) {
Expand All @@ -137,7 +135,7 @@ WebIDL::ExceptionOr<void> DOMTokenList::add(Vector<DeprecatedString> const& toke
}

// 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:
for (auto const& token : tokens) {
Expand All @@ -155,7 +153,7 @@ WebIDL::ExceptionOr<void> DOMTokenList::remove(Vector<DeprecatedString> const& t
}

// 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.
// 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
Expand Down Expand Up @@ -186,7 +184,7 @@ WebIDL::ExceptionOr<bool> DOMTokenList::toggle(DeprecatedString const& token, Op
}

// 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.
// 2. If either token or newToken contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
Expand Down Expand Up @@ -222,21 +220,21 @@ WebIDL::ExceptionOr<bool> DOMTokenList::supports([[maybe_unused]] StringView tok
}

// https://dom.spec.whatwg.org/#dom-domtokenlist-value
DeprecatedString DOMTokenList::value() const
String DOMTokenList::value() const
{
StringBuilder builder;
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
void DOMTokenList::set_value(DeprecatedString value)
void DOMTokenList::set_value(String const& value)
{
JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
if (!associated_element)
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
Expand All @@ -255,20 +253,22 @@ void DOMTokenList::run_update_steps()
if (!associated_element)
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.
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;

// 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(deprecated_attribute, value().to_deprecated_string()));
}

WebIDL::ExceptionOr<JS::Value> DOMTokenList::item_value(size_t index) const
{
auto const& string = item(index);
if (string.is_null())
auto string = item(index);
if (!string.has_value())
return JS::js_undefined();
return JS::PrimitiveString::create(vm(), string);
return JS::PrimitiveString::create(vm(), string.release_value());
}

}
28 changes: 14 additions & 14 deletions Userland/Libraries/LibWeb/DOM/DOMTokenList.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#pragma once

#include <AK/DeprecatedFlyString.h>
#include <AK/DeprecatedString.h>
#include <AK/FlyString.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibWeb/Bindings/LegacyPlatformObject.h>
Expand All @@ -24,7 +24,7 @@ class DOMTokenList final : public Bindings::LegacyPlatformObject {
WEB_PLATFORM_OBJECT(DOMTokenList, Bindings::LegacyPlatformObject);

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;

void associated_attribute_changed(StringView value);
Expand All @@ -33,18 +33,18 @@ class DOMTokenList final : public Bindings::LegacyPlatformObject {
virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;

size_t length() const { return m_token_set.size(); }
DeprecatedString const& item(size_t index) const;
bool contains(StringView token);
WebIDL::ExceptionOr<void> add(Vector<DeprecatedString> const& tokens);
WebIDL::ExceptionOr<void> remove(Vector<DeprecatedString> const& tokens);
WebIDL::ExceptionOr<bool> toggle(DeprecatedString const& token, Optional<bool> force);
WebIDL::ExceptionOr<bool> replace(DeprecatedString const& token, DeprecatedString const& new_token);
Optional<String> item(size_t index) const;
bool contains(String const& token);
WebIDL::ExceptionOr<void> add(Vector<String> const& tokens);
WebIDL::ExceptionOr<void> remove(Vector<String> const& tokens);
WebIDL::ExceptionOr<bool> toggle(String const& token, Optional<bool> force);
WebIDL::ExceptionOr<bool> replace(String const& token, String const& new_token);
WebIDL::ExceptionOr<bool> supports(StringView token);
DeprecatedString value() const;
void set_value(DeprecatedString value);
String value() const;
void set_value(String const& value);

private:
DOMTokenList(Element& associated_element, DeprecatedFlyString associated_attribute);
DOMTokenList(Element& associated_element, FlyString associated_attribute);

virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Expand All @@ -66,8 +66,8 @@ class DOMTokenList final : public Bindings::LegacyPlatformObject {
void run_update_steps();

JS::NonnullGCPtr<Element> m_associated_element;
DeprecatedFlyString m_associated_attribute;
Vector<DeprecatedString> m_token_set;
FlyString m_associated_attribute;
Vector<String> m_token_set;
};

}
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/DOM/DOMTokenList.idl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://dom.spec.whatwg.org/#interface-domtokenlist
[Exposed=Window]
[Exposed=Window, UseNewAKString]
interface DOMTokenList {
readonly attribute unsigned long length;
getter DOMString? item(unsigned long index);
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/DOM/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ NonnullRefPtr<CSS::StyleProperties> Element::resolved_css_values()
DOMTokenList* Element::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;
}

Expand Down

0 comments on commit b0eea51

Please sign in to comment.