mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-26 01:35:08 +02:00
LibWeb: Implement caching of reflected element array attributes
For attributes like Element.ariaControlsElements, which are a reflection of FrozenArray<Element>, we must return the same JS::Array object every time the attribute is invoked - until its contents have changed. This patch implements caching of the reflected array in accordance with the spec.
This commit is contained in:
Notes:
github-actions[bot]
2025-04-26 21:30:33 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/ac1c2a956a2 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4477
@@ -528,4 +528,25 @@ template JS::ThrowCompletionOr<UnsignedLong> convert_to_int(JS::VM& vm, JS::Valu
|
||||
template JS::ThrowCompletionOr<LongLong> convert_to_int(JS::VM& vm, JS::Value, EnforceRange, Clamp);
|
||||
template JS::ThrowCompletionOr<UnsignedLongLong> convert_to_int(JS::VM& vm, JS::Value, EnforceRange, Clamp);
|
||||
|
||||
// AD-HOC: For same-object caching purposes, this can be used to compare a cached JS array of DOM::Elements with another
|
||||
// list. Either list can be null, in which case they are considered the same only if they are both null.
|
||||
bool lists_contain_same_elements(GC::Ptr<JS::Array> array, Optional<GC::RootVector<GC::Ref<DOM::Element>>> const& elements)
|
||||
{
|
||||
if (!array || !elements.has_value())
|
||||
return !array && !elements.has_value();
|
||||
|
||||
bool is_equivalent = array->indexed_properties().array_like_size() == elements->size();
|
||||
|
||||
for (size_t i = 0; is_equivalent && i < elements->size(); ++i) {
|
||||
auto cached_value = array->get_without_side_effects(JS::PropertyKey { i });
|
||||
auto const& cached_element = as<DOM::Element>(cached_value.as_object());
|
||||
|
||||
auto it = elements->find_if([&](auto const& element) { return element.ptr() == &cached_element; });
|
||||
if (it == elements->end())
|
||||
is_equivalent = false;
|
||||
}
|
||||
|
||||
return is_equivalent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user