mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 01:00:05 +00:00
LibWeb: Implement indexed property support for HTML::Storage
We only supported named properties on Storage, and as a result `localStorage[0]` would be disconnected from the Storage's backing map. Fixes at least 20 subtests in WPT in /webstorage.
This commit is contained in:
parent
999c591e83
commit
4c189166f4
Notes:
github-actions[bot]
2024-10-18 21:11:13 +00:00
Author: https://github.com/gmta
Commit: 4c189166f4
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1846
4 changed files with 42 additions and 0 deletions
6
Tests/LibWeb/Text/expected/localStorage.txt
Normal file
6
Tests/LibWeb/Text/expected/localStorage.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
value
|
||||||
|
value
|
||||||
|
other
|
||||||
|
other
|
||||||
|
foo
|
||||||
|
foo
|
17
Tests/LibWeb/Text/input/localStorage.html
Normal file
17
Tests/LibWeb/Text/input/localStorage.html
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<script src="include.js"></script>
|
||||||
|
<script>
|
||||||
|
test(() => {
|
||||||
|
localStorage["test"] = "value";
|
||||||
|
println(localStorage["test"]);
|
||||||
|
println(localStorage.getItem("test"));
|
||||||
|
|
||||||
|
localStorage.setItem("test", "other");
|
||||||
|
println(localStorage["test"]);
|
||||||
|
println(localStorage.getItem("test"));
|
||||||
|
|
||||||
|
// Ensure indexed properties work
|
||||||
|
localStorage[0] = "foo";
|
||||||
|
println(localStorage[0]);
|
||||||
|
println(localStorage.getItem("0"));
|
||||||
|
});
|
||||||
|
</script>
|
|
@ -23,10 +23,13 @@ Storage::Storage(JS::Realm& realm)
|
||||||
: Bindings::PlatformObject(realm)
|
: Bindings::PlatformObject(realm)
|
||||||
{
|
{
|
||||||
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
|
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
|
||||||
|
.supports_indexed_properties = true,
|
||||||
.supports_named_properties = true,
|
.supports_named_properties = true,
|
||||||
|
.has_indexed_property_setter = true,
|
||||||
.has_named_property_setter = true,
|
.has_named_property_setter = true,
|
||||||
.has_named_property_deleter = true,
|
.has_named_property_deleter = true,
|
||||||
.has_legacy_override_built_ins_interface_extended_attribute = true,
|
.has_legacy_override_built_ins_interface_extended_attribute = true,
|
||||||
|
.indexed_property_setter_has_identifier = true,
|
||||||
.named_property_setter_has_identifier = true,
|
.named_property_setter_has_identifier = true,
|
||||||
.named_property_deleter_has_identifier = true,
|
.named_property_deleter_has_identifier = true,
|
||||||
};
|
};
|
||||||
|
@ -168,6 +171,13 @@ Vector<FlyString> Storage::supported_property_names() const
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<JS::Value> Storage::item_value(size_t index) const
|
||||||
|
{
|
||||||
|
// Handle index as a string since that's our key type
|
||||||
|
auto key = String::number(index);
|
||||||
|
return named_item_value(key);
|
||||||
|
}
|
||||||
|
|
||||||
JS::Value Storage::named_item_value(FlyString const& name) const
|
JS::Value Storage::named_item_value(FlyString const& name) const
|
||||||
{
|
{
|
||||||
auto value = get_item(name);
|
auto value = get_item(name);
|
||||||
|
@ -182,6 +192,13 @@ WebIDL::ExceptionOr<Bindings::PlatformObject::DidDeletionFail> Storage::delete_v
|
||||||
return DidDeletionFail::NotRelevant;
|
return DidDeletionFail::NotRelevant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WebIDL::ExceptionOr<void> Storage::set_value_of_indexed_property(u32 index, JS::Value unconverted_value)
|
||||||
|
{
|
||||||
|
// Handle index as a string since that's our key type
|
||||||
|
auto key = String::number(index);
|
||||||
|
return set_value_of_named_property(key, unconverted_value);
|
||||||
|
}
|
||||||
|
|
||||||
WebIDL::ExceptionOr<void> Storage::set_value_of_named_property(String const& key, JS::Value unconverted_value)
|
WebIDL::ExceptionOr<void> Storage::set_value_of_named_property(String const& key, JS::Value unconverted_value)
|
||||||
{
|
{
|
||||||
// NOTE: Since PlatformObject does not know the type of value, we must convert it ourselves.
|
// NOTE: Since PlatformObject does not know the type of value, we must convert it ourselves.
|
||||||
|
|
|
@ -38,9 +38,11 @@ private:
|
||||||
virtual void initialize(JS::Realm&) override;
|
virtual void initialize(JS::Realm&) override;
|
||||||
|
|
||||||
// ^PlatformObject
|
// ^PlatformObject
|
||||||
|
virtual Optional<JS::Value> item_value(size_t index) const override;
|
||||||
virtual JS::Value named_item_value(FlyString const&) const override;
|
virtual JS::Value named_item_value(FlyString const&) const override;
|
||||||
virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) override;
|
virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) override;
|
||||||
virtual Vector<FlyString> supported_property_names() const override;
|
virtual Vector<FlyString> supported_property_names() const override;
|
||||||
|
virtual WebIDL::ExceptionOr<void> set_value_of_indexed_property(u32, JS::Value) override;
|
||||||
virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const& key, JS::Value value) override;
|
virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const& key, JS::Value value) override;
|
||||||
|
|
||||||
void reorder();
|
void reorder();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue