HTMLDetailsElement.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/DOM/ElementFactory.h>
  9. #include <LibWeb/DOM/Event.h>
  10. #include <LibWeb/DOM/ShadowRoot.h>
  11. #include <LibWeb/DOM/Text.h>
  12. #include <LibWeb/HTML/EventLoop/TaskQueue.h>
  13. #include <LibWeb/HTML/HTMLDetailsElement.h>
  14. #include <LibWeb/HTML/HTMLSlotElement.h>
  15. #include <LibWeb/HTML/HTMLSummaryElement.h>
  16. #include <LibWeb/HTML/ToggleEvent.h>
  17. #include <LibWeb/Namespace.h>
  18. namespace Web::HTML {
  19. JS_DEFINE_ALLOCATOR(HTMLDetailsElement);
  20. HTMLDetailsElement::HTMLDetailsElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : HTMLElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLDetailsElement::~HTMLDetailsElement() = default;
  25. void HTMLDetailsElement::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(m_summary_slot);
  29. visitor.visit(m_descendants_slot);
  30. }
  31. void HTMLDetailsElement::initialize(JS::Realm& realm)
  32. {
  33. Base::initialize(realm);
  34. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLDetailsElementPrototype>(realm, "HTMLDetailsElement"));
  35. create_shadow_tree(realm).release_value_but_fixme_should_propagate_errors();
  36. }
  37. void HTMLDetailsElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  38. {
  39. Base::attribute_changed(name, value);
  40. // https://html.spec.whatwg.org/multipage/interactive-elements.html#details-notification-task-steps
  41. if (name == HTML::AttributeNames::open) {
  42. // 1. If the open attribute is added, queue a details toggle event task given the details element, "closed", and "open".
  43. if (value.has_value()) {
  44. queue_a_details_toggle_event_task("closed"_string, "open"_string);
  45. }
  46. // 2. Otherwise, queue a details toggle event task given the details element, "open", and "closed".
  47. else {
  48. queue_a_details_toggle_event_task("open"_string, "closed"_string);
  49. }
  50. update_shadow_tree_style();
  51. }
  52. }
  53. void HTMLDetailsElement::children_changed()
  54. {
  55. Base::children_changed();
  56. update_shadow_tree_slots();
  57. }
  58. // https://html.spec.whatwg.org/multipage/interactive-elements.html#queue-a-details-toggle-event-task
  59. void HTMLDetailsElement::queue_a_details_toggle_event_task(String old_state, String new_state)
  60. {
  61. // 1. If element's details toggle task tracker is not null, then:
  62. if (m_details_toggle_task_tracker.has_value()) {
  63. // 1. Set oldState to element's details toggle task tracker's old state.
  64. old_state = move(m_details_toggle_task_tracker->old_state);
  65. // 2. Remove element's details toggle task tracker's task from its task queue.
  66. HTML::main_thread_event_loop().task_queue().remove_tasks_matching([&](auto const& task) {
  67. return task.id() == m_details_toggle_task_tracker->task_id;
  68. });
  69. // 3. Set element's details toggle task tracker to null.
  70. m_details_toggle_task_tracker->task_id = {};
  71. }
  72. // 2. Queue an element task given the DOM manipulation task source and element to run the following steps:
  73. auto task_id = queue_an_element_task(HTML::Task::Source::DOMManipulation, [this, old_state, new_state = move(new_state)]() mutable {
  74. // 1. Fire an event named toggle at element, using ToggleEvent, with the oldState attribute initialized to
  75. // oldState and the newState attribute initialized to newState.
  76. ToggleEventInit event_init {};
  77. event_init.old_state = move(old_state);
  78. event_init.new_state = move(new_state);
  79. dispatch_event(ToggleEvent::create(realm(), HTML::EventNames::toggle, move(event_init)));
  80. // 2. Set element's details toggle task tracker to null.
  81. m_details_toggle_task_tracker = {};
  82. });
  83. // 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.
  84. m_details_toggle_task_tracker = ToggleTaskTracker {
  85. .task_id = task_id,
  86. .old_state = move(old_state),
  87. };
  88. }
  89. // https://html.spec.whatwg.org/#the-details-and-summary-elements
  90. WebIDL::ExceptionOr<void> HTMLDetailsElement::create_shadow_tree(JS::Realm& realm)
  91. {
  92. // The element is also expected to have an internal shadow tree with two slots.
  93. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm, document(), *this, Bindings::ShadowRootMode::Closed);
  94. shadow_root->set_slot_assignment(Bindings::SlotAssignmentMode::Manual);
  95. // The first slot is expected to take the details element's first summary element child, if any.
  96. auto summary_slot = TRY(DOM::create_element(document(), HTML::TagNames::slot, Namespace::HTML));
  97. MUST(shadow_root->append_child(summary_slot));
  98. // The second slot is expected to take the details element's remaining descendants, if any.
  99. auto descendants_slot = TRY(DOM::create_element(document(), HTML::TagNames::slot, Namespace::HTML));
  100. MUST(shadow_root->append_child(descendants_slot));
  101. m_summary_slot = static_cast<HTML::HTMLSlotElement&>(*summary_slot);
  102. m_descendants_slot = static_cast<HTML::HTMLSlotElement&>(*descendants_slot);
  103. set_shadow_root(shadow_root);
  104. return {};
  105. }
  106. void HTMLDetailsElement::update_shadow_tree_slots()
  107. {
  108. Vector<HTMLSlotElement::SlottableHandle> summary_assignment;
  109. Vector<HTMLSlotElement::SlottableHandle> descendants_assignment;
  110. auto* summary = first_child_of_type<HTMLSummaryElement>();
  111. if (summary != nullptr)
  112. summary_assignment.append(JS::make_handle(static_cast<DOM::Element&>(*summary)));
  113. for_each_in_subtree([&](auto& child) {
  114. if (&child == summary)
  115. return IterationDecision::Continue;
  116. if (!child.is_slottable())
  117. return IterationDecision::Continue;
  118. child.as_slottable().visit([&](auto& node) {
  119. descendants_assignment.append(JS::make_handle(node));
  120. });
  121. return IterationDecision::Continue;
  122. });
  123. m_summary_slot->assign(move(summary_assignment));
  124. m_descendants_slot->assign(move(descendants_assignment));
  125. update_shadow_tree_style();
  126. }
  127. // https://html.spec.whatwg.org/#the-details-and-summary-elements:the-details-element-6
  128. void HTMLDetailsElement::update_shadow_tree_style()
  129. {
  130. if (has_attribute(HTML::AttributeNames::open)) {
  131. MUST(m_descendants_slot->set_attribute(HTML::AttributeNames::style, R"~~~(
  132. display: block;
  133. )~~~"_string));
  134. } else {
  135. // FIXME: Should be `display: block` but we do not support `content-visibility: hidden`.
  136. MUST(m_descendants_slot->set_attribute(HTML::AttributeNames::style, R"~~~(
  137. display: none;
  138. content-visibility: hidden;
  139. )~~~"_string));
  140. }
  141. }
  142. }