mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-01 08:48:49 +00:00
LibWeb: Implement HTMLDetailsElement's open attribute closer to the spec
The spec now has a "toggle task tracker" to coalesce rapid changes to this attribute. It also now has an explicit ToggleEvent to encapsulate the old and new state of the element. This further handles the attribute being added/removed using a override of Element::attribute_changed(), rather than being the only element to instead override Element::set/remove__attribute().
This commit is contained in:
parent
153ae93f9c
commit
8bb5652835
Notes:
sideshowbarker
2024-07-16 23:57:20 +09:00
Author: https://github.com/trflynn89
Commit: 8bb5652835
Pull-request: https://github.com/SerenityOS/serenity/pull/20965
Reviewed-by: https://github.com/Lubrsi
Reviewed-by: https://github.com/shannonbooth
4 changed files with 92 additions and 40 deletions
|
@ -6,8 +6,10 @@
|
|||
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
#include <LibWeb/HTML/EventLoop/TaskQueue.h>
|
||||
#include <LibWeb/HTML/HTMLDetailsElement.h>
|
||||
#include <LibWeb/HTML/HTMLSummaryElement.h>
|
||||
#include <LibWeb/HTML/ToggleEvent.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
|
@ -18,44 +20,65 @@ HTMLDetailsElement::HTMLDetailsElement(DOM::Document& document, DOM::QualifiedNa
|
|||
|
||||
HTMLDetailsElement::~HTMLDetailsElement() = default;
|
||||
|
||||
WebIDL::ExceptionOr<void> HTMLDetailsElement::set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
|
||||
{
|
||||
auto result = HTMLElement::set_attribute(name, value);
|
||||
if (result.is_exception())
|
||||
return result.exception();
|
||||
|
||||
if (name == HTML::AttributeNames::open)
|
||||
run_details_notification_task_steps();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void HTMLDetailsElement::remove_attribute(DeprecatedFlyString const& name)
|
||||
{
|
||||
HTMLElement::remove_attribute(name);
|
||||
|
||||
if (name == HTML::AttributeNames::open)
|
||||
run_details_notification_task_steps();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/interactive-elements.html#the-details-element:details-notification-task-steps
|
||||
void HTMLDetailsElement::run_details_notification_task_steps()
|
||||
{
|
||||
// Whenever the open attribute is added to or removed from a details element,
|
||||
// the user agent must queue an element task on the DOM manipulation task source given then details element that runs the following steps,
|
||||
// which are known as the details notification task steps, for this details element:
|
||||
queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
|
||||
// 1. FIXME: If another task has been queued to run the details notification task steps for this details element, then return.
|
||||
|
||||
// 2. Fire an event named toggle at the details element.
|
||||
dispatch_event(Web::DOM::Event::create(realm(), HTML::EventNames::toggle));
|
||||
});
|
||||
}
|
||||
|
||||
void HTMLDetailsElement::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLDetailsElementPrototype>(realm, "HTMLDetailsElement"));
|
||||
}
|
||||
|
||||
void HTMLDetailsElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
|
||||
{
|
||||
Base::attribute_changed(name, value);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/interactive-elements.html#details-notification-task-steps
|
||||
if (name == HTML::AttributeNames::open) {
|
||||
// 1. If the open attribute is added, queue a details toggle event task given the details element, "closed", and "open".
|
||||
if (!value.is_null()) {
|
||||
queue_a_details_toggle_event_task("closed"_string, "open"_string);
|
||||
}
|
||||
// 2. Otherwise, queue a details toggle event task given the details element, "open", and "closed".
|
||||
else {
|
||||
queue_a_details_toggle_event_task("open"_string, "closed"_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/interactive-elements.html#queue-a-details-toggle-event-task
|
||||
void HTMLDetailsElement::queue_a_details_toggle_event_task(String old_state, String new_state)
|
||||
{
|
||||
// 1. If element's details toggle task tracker is not null, then:
|
||||
if (m_details_toggle_task_tracker.has_value()) {
|
||||
// 1. Set oldState to element's details toggle task tracker's old state.
|
||||
old_state = move(m_details_toggle_task_tracker->old_state);
|
||||
|
||||
// 2. Remove element's details toggle task tracker's task from its task queue.
|
||||
HTML::main_thread_event_loop().task_queue().remove_tasks_matching([&](auto const& task) {
|
||||
return task.id() == m_details_toggle_task_tracker->task_id;
|
||||
});
|
||||
|
||||
// 3. Set element's details toggle task tracker to null.
|
||||
m_details_toggle_task_tracker->task_id = {};
|
||||
}
|
||||
|
||||
// 2. Queue an element task given the DOM manipulation task source and element to run the following steps:
|
||||
auto task_id = queue_an_element_task(HTML::Task::Source::DOMManipulation, [this, old_state, new_state = move(new_state)]() mutable {
|
||||
// 1. Fire an event named toggle at element, using ToggleEvent, with the oldState attribute initialized to
|
||||
// oldState and the newState attribute initialized to newState.
|
||||
ToggleEventInit event_init {};
|
||||
event_init.old_state = move(old_state);
|
||||
event_init.new_state = move(new_state);
|
||||
|
||||
dispatch_event(ToggleEvent::create(realm(), HTML::EventNames::toggle, move(event_init)));
|
||||
|
||||
// 2. Set element's details toggle task tracker to null.
|
||||
m_details_toggle_task_tracker = {};
|
||||
});
|
||||
|
||||
// 3. Set element's details toggle task tracker to a struct with task set to the just-queued task and old state set to oldState.
|
||||
m_details_toggle_task_tracker = ToggleTaskTracker {
|
||||
.task_id = task_id,
|
||||
.old_state = move(old_state),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue