2020-08-01 03:04:26 +01:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-08-01 03:04:26 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 03:04:26 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-30 17:16:16 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2023-06-02 21:44:03 +02:00
|
|
|
#include <LibWeb/DOM/Event.h>
|
2023-08-31 13:53:17 -04:00
|
|
|
#include <LibWeb/HTML/EventLoop/TaskQueue.h>
|
2020-08-01 03:04:26 +01:00
|
|
|
#include <LibWeb/HTML/HTMLDetailsElement.h>
|
2023-06-02 21:44:03 +02:00
|
|
|
#include <LibWeb/HTML/HTMLSummaryElement.h>
|
2023-08-31 13:53:17 -04:00
|
|
|
#include <LibWeb/HTML/ToggleEvent.h>
|
2020-08-01 03:04:26 +01:00
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
HTMLDetailsElement::HTMLDetailsElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-08-01 03:04:26 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
HTMLDetailsElement::~HTMLDetailsElement() = default;
|
2023-01-10 06:28:20 -05:00
|
|
|
|
2023-08-31 13:53:17 -04:00
|
|
|
void HTMLDetailsElement::initialize(JS::Realm& realm)
|
2023-06-02 21:44:03 +02:00
|
|
|
{
|
2023-08-31 13:53:17 -04:00
|
|
|
Base::initialize(realm);
|
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLDetailsElementPrototype>(realm, "HTMLDetailsElement"));
|
2023-06-02 21:44:03 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 13:53:17 -04:00
|
|
|
void HTMLDetailsElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
|
2023-06-02 21:44:03 +02:00
|
|
|
{
|
2023-08-31 13:53:17 -04:00
|
|
|
Base::attribute_changed(name, value);
|
2023-06-02 21:44:03 +02:00
|
|
|
|
2023-08-31 13:53:17 -04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2023-06-02 21:44:03 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 13:53:17 -04:00
|
|
|
// 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)
|
2023-06-02 21:44:03 +02:00
|
|
|
{
|
2023-08-31 13:53:17 -04:00
|
|
|
// 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 = {};
|
2023-06-02 21:44:03 +02:00
|
|
|
});
|
|
|
|
|
2023-08-31 13:53:17 -04:00
|
|
|
// 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),
|
|
|
|
};
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2020-08-01 03:04:26 +01:00
|
|
|
}
|