1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:28:11 +00:00

LibWeb: Port LegacyPlatformObject from DeprecatedString to String

This commit is contained in:
Shannon Booth 2023-11-21 12:15:56 +13:00 committed by Tim Flynn
parent 56d10bf198
commit f43313d099
6 changed files with 32 additions and 30 deletions

View file

@ -123,11 +123,11 @@ WebIDL::ExceptionOr<void> LegacyPlatformObject::invoke_indexed_property_setter(J
} }
// https://webidl.spec.whatwg.org/#invoke-named-setter // https://webidl.spec.whatwg.org/#invoke-named-setter
WebIDL::ExceptionOr<void> LegacyPlatformObject::invoke_named_property_setter(DeprecatedString const& property_name, JS::Value value) WebIDL::ExceptionOr<void> LegacyPlatformObject::invoke_named_property_setter(String const& property_name, JS::Value value)
{ {
// 1. Let creating be true if P is not a supported property name, and false otherwise. // 1. Let creating be true if P is not a supported property name, and false otherwise.
Vector<String> supported_property_names = this->supported_property_names(); Vector<String> supported_property_names = this->supported_property_names();
bool creating = !supported_property_names.contains_slow(MUST(String::from_deprecated_string(property_name))); bool creating = !supported_property_names.contains_slow(property_name);
// FIXME: We do not have this information at this point, so converting the value is left as an exercise to the inheritor of LegacyPlatformObject. // FIXME: We do not have this information at this point, so converting the value is left as an exercise to the inheritor of LegacyPlatformObject.
// 2. Let operation be the operation used to declare the indexed property setter. // 2. Let operation be the operation used to declare the indexed property setter.
@ -174,7 +174,7 @@ JS::ThrowCompletionOr<bool> LegacyPlatformObject::internal_set(JS::PropertyKey c
// 2. If O implements an interface with a named property setter and Type(P) is String, then: // 2. If O implements an interface with a named property setter and Type(P) is String, then:
if (has_named_property_setter() && property_name.is_string()) { if (has_named_property_setter() && property_name.is_string()) {
// 1. Invoke the named property setter on O with P and V. // 1. Invoke the named property setter on O with P and V.
TRY(throw_dom_exception_if_needed(vm, [&] { return invoke_named_property_setter(property_name.as_string(), value); })); TRY(throw_dom_exception_if_needed(vm, [&] { return invoke_named_property_setter(MUST(String::from_deprecated_string(property_name.as_string())), value); }));
// 2. Return true. // 2. Return true.
return true; return true;
@ -236,7 +236,7 @@ JS::ThrowCompletionOr<bool> LegacyPlatformObject::internal_define_own_property(J
return false; return false;
// 2. Invoke the named property setter on O with P and Desc.[[Value]]. // 2. Invoke the named property setter on O with P and Desc.[[Value]].
TRY(throw_dom_exception_if_needed(vm, [&] { return invoke_named_property_setter(property_name_as_string, property_descriptor.value.value()); })); TRY(throw_dom_exception_if_needed(vm, [&] { return invoke_named_property_setter(MUST(String::from_deprecated_string(property_name_as_string)), property_descriptor.value.value()); }));
// 3. Return true. // 3. Return true.
return true; return true;
@ -291,7 +291,7 @@ JS::ThrowCompletionOr<bool> LegacyPlatformObject::internal_delete(JS::PropertyKe
// 4. Otherwise, operation was defined with an identifier: // 4. Otherwise, operation was defined with an identifier:
// 1. Perform method steps of operation with O as this and « P » as the argument values. // 1. Perform method steps of operation with O as this and « P » as the argument values.
// 2. If operation was declared with a return type of boolean and the steps returned false, then return false. // 2. If operation was declared with a return type of boolean and the steps returned false, then return false.
auto did_deletion_fail = TRY(throw_dom_exception_if_needed(vm, [&] { return delete_value(property_name_string); })); auto did_deletion_fail = TRY(throw_dom_exception_if_needed(vm, [&] { return delete_value(MUST(String::from_deprecated_string(property_name_string))); }));
if (!named_property_deleter_has_identifier()) if (!named_property_deleter_has_identifier())
VERIFY(did_deletion_fail != DidDeletionFail::NotRelevant); VERIFY(did_deletion_fail != DidDeletionFail::NotRelevant);
@ -339,7 +339,7 @@ JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> LegacyPlatformObject::interna
if (supports_indexed_properties()) { if (supports_indexed_properties()) {
for (u64 index = 0; index <= NumericLimits<u32>::max(); ++index) { for (u64 index = 0; index <= NumericLimits<u32>::max(); ++index) {
if (is_supported_property_index(index)) if (is_supported_property_index(index))
keys.append(JS::PrimitiveString::create(vm, DeprecatedString::number(index))); keys.append(JS::PrimitiveString::create(vm, MUST(String::number(index))));
else else
break; break;
} }

View file

@ -42,12 +42,12 @@ public:
// NOTE: These will crash if you make has_named_property_setter return true but do not override these methods. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
// NOTE: This is only used if named_property_setter_has_identifier returns false, otherwise set_value_of_named_property is used instead. // NOTE: This is only used if named_property_setter_has_identifier returns false, otherwise set_value_of_named_property is used instead.
virtual WebIDL::ExceptionOr<void> set_value_of_new_named_property(DeprecatedString const&, JS::Value) { VERIFY_NOT_REACHED(); } virtual WebIDL::ExceptionOr<void> set_value_of_new_named_property(String const&, JS::Value) { VERIFY_NOT_REACHED(); }
virtual WebIDL::ExceptionOr<void> set_value_of_existing_named_property(DeprecatedString const&, JS::Value) { VERIFY_NOT_REACHED(); } virtual WebIDL::ExceptionOr<void> set_value_of_existing_named_property(String const&, JS::Value) { VERIFY_NOT_REACHED(); }
// NOTE: These will crash if you make has_named_property_setter return true but do not override these methods. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
// NOTE: This is only used if you make named_property_setter_has_identifier return true, otherwise set_value_of_{new,existing}_named_property is used instead. // NOTE: This is only used if you make named_property_setter_has_identifier return true, otherwise set_value_of_{new,existing}_named_property is used instead.
virtual WebIDL::ExceptionOr<void> set_value_of_named_property(DeprecatedString const&, JS::Value) { VERIFY_NOT_REACHED(); } virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const&, JS::Value) { VERIFY_NOT_REACHED(); }
// NOTE: These will crash if you make has_indexed_property_setter return true but do not override these methods. // NOTE: These will crash if you make has_indexed_property_setter return true but do not override these methods.
// NOTE: This is only used if indexed_property_setter_has_identifier returns false, otherwise set_value_of_indexed_property is used instead. // NOTE: This is only used if indexed_property_setter_has_identifier returns false, otherwise set_value_of_indexed_property is used instead.
@ -67,7 +67,7 @@ public:
}; };
// NOTE: This will crash if you make has_named_property_deleter return true but do not override this method. // NOTE: This will crash if you make has_named_property_deleter return true but do not override this method.
virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(DeprecatedString const&) { VERIFY_NOT_REACHED(); } virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) { VERIFY_NOT_REACHED(); }
protected: protected:
explicit LegacyPlatformObject(JS::Realm& realm); explicit LegacyPlatformObject(JS::Realm& realm);
@ -90,7 +90,7 @@ protected:
private: private:
WebIDL::ExceptionOr<void> invoke_indexed_property_setter(JS::PropertyKey const&, JS::Value); WebIDL::ExceptionOr<void> invoke_indexed_property_setter(JS::PropertyKey const&, JS::Value);
WebIDL::ExceptionOr<void> invoke_named_property_setter(DeprecatedString const&, JS::Value); WebIDL::ExceptionOr<void> invoke_named_property_setter(String const&, JS::Value);
}; };
} }

View file

@ -119,24 +119,26 @@ DeprecatedString DOMStringMap::determine_value_of_named_property(DeprecatedStrin
} }
// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_new_named_property(DeprecatedString const& name, JS::Value unconverted_value) WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_new_named_property(String const& name, JS::Value unconverted_value)
{ {
// NOTE: Since LegacyPlatformObject does not know the type of value, we must convert it ourselves. // NOTE: Since LegacyPlatformObject does not know the type of value, we must convert it ourselves.
// The type of `value` is `DOMString`. // The type of `value` is `DOMString`.
auto value = TRY(unconverted_value.to_string(vm())); auto value = TRY(unconverted_value.to_string(vm()));
AK::StringBuilder builder; StringBuilder builder;
// 3. Insert the string data- at the front of name. // 3. Insert the string data- at the front of name.
// NOTE: This is done out of order because StringBuilder doesn't have prepend. // NOTE: This is done out of order because StringBuilder doesn't have prepend.
builder.append("data-"sv); builder.append("data-"sv);
for (size_t character_index = 0; character_index < name.length(); ++character_index) { auto name_view = name.bytes_as_string_view();
// 1. If name contains a U+002D HYPHEN-MINUS character (-) followed by an ASCII lower alpha, then throw a "SyntaxError" DOMException.
auto current_character = name[character_index];
if (current_character == '-' && character_index + 1 < name.length()) { for (size_t character_index = 0; character_index < name_view.length(); ++character_index) {
auto next_character = name[character_index + 1]; // 1. If name contains a U+002D HYPHEN-MINUS character (-) followed by an ASCII lower alpha, then throw a "SyntaxError" DOMException.
auto current_character = name_view[character_index];
if (current_character == '-' && character_index + 1 < name_view.length()) {
auto next_character = name_view[character_index + 1];
if (is_ascii_lower_alpha(next_character)) if (is_ascii_lower_alpha(next_character))
return WebIDL::SyntaxError::create(realm(), "Name cannot contain a '-' followed by a lowercase character."_fly_string); return WebIDL::SyntaxError::create(realm(), "Name cannot contain a '-' followed by a lowercase character."_fly_string);
} }
@ -162,21 +164,21 @@ WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_new_named_property(Deprecat
} }
// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_existing_named_property(DeprecatedString const& name, JS::Value value) WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_existing_named_property(String const& name, JS::Value value)
{ {
return set_value_of_new_named_property(name, value); return set_value_of_new_named_property(name, value);
} }
// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem
WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> DOMStringMap::delete_value(DeprecatedString const& name) WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> DOMStringMap::delete_value(String const& name)
{ {
AK::StringBuilder builder; StringBuilder builder;
// 2. Insert the string data- at the front of name. // 2. Insert the string data- at the front of name.
// NOTE: This is done out of order because StringBuilder doesn't have prepend. // NOTE: This is done out of order because StringBuilder doesn't have prepend.
builder.append("data-"sv); builder.append("data-"sv);
for (auto character : name) { for (auto character : name.bytes_as_string_view()) {
// 1. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase. // 1. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase.
if (is_ascii_upper_alpha(character)) { if (is_ascii_upper_alpha(character)) {
builder.append('-'); builder.append('-');

View file

@ -24,10 +24,10 @@ public:
DeprecatedString determine_value_of_named_property(DeprecatedString const&) const; DeprecatedString determine_value_of_named_property(DeprecatedString const&) const;
virtual WebIDL::ExceptionOr<void> set_value_of_new_named_property(DeprecatedString const&, JS::Value) override; virtual WebIDL::ExceptionOr<void> set_value_of_new_named_property(String const&, JS::Value) override;
virtual WebIDL::ExceptionOr<void> set_value_of_existing_named_property(DeprecatedString const&, JS::Value) override; virtual WebIDL::ExceptionOr<void> set_value_of_existing_named_property(String const&, JS::Value) override;
virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(DeprecatedString const&) override; virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) override;
private: private:
explicit DOMStringMap(DOM::Element&); explicit DOMStringMap(DOM::Element&);

View file

@ -167,18 +167,18 @@ WebIDL::ExceptionOr<JS::Value> Storage::named_item_value(FlyString const& name)
return JS::PrimitiveString::create(vm(), value.release_value()); return JS::PrimitiveString::create(vm(), value.release_value());
} }
WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> Storage::delete_value(DeprecatedString const& name) WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> Storage::delete_value(String const& name)
{ {
remove_item(name); remove_item(name);
return DidDeletionFail::NotRelevant; return DidDeletionFail::NotRelevant;
} }
WebIDL::ExceptionOr<void> Storage::set_value_of_named_property(DeprecatedString const& key, JS::Value unconverted_value) WebIDL::ExceptionOr<void> Storage::set_value_of_named_property(String const& key, JS::Value unconverted_value)
{ {
// NOTE: Since LegacyPlatformObject does not know the type of value, we must convert it ourselves. // NOTE: Since LegacyPlatformObject does not know the type of value, we must convert it ourselves.
// The type of `value` is `DOMString`. // The type of `value` is `DOMString`.
auto value = TRY(unconverted_value.to_string(vm())); auto value = TRY(unconverted_value.to_string(vm()));
return set_item(String::from_deprecated_string(key).release_value(), value); return set_item(key, value);
} }
void Storage::dump() const void Storage::dump() const

View file

@ -39,9 +39,9 @@ private:
// ^LegacyPlatformObject // ^LegacyPlatformObject
virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const&) const override; virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const&) const override;
virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(DeprecatedString const&) override; virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) override;
virtual Vector<String> supported_property_names() const override; virtual Vector<String> supported_property_names() const override;
virtual WebIDL::ExceptionOr<void> set_value_of_named_property(DeprecatedString const& key, JS::Value value) override; virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const& key, JS::Value value) override;
virtual bool supports_indexed_properties() const override { return false; } virtual bool supports_indexed_properties() const override { return false; }
virtual bool supports_named_properties() const override { return true; } virtual bool supports_named_properties() const override { return true; }