HTMLDetailsElement.cpp 6.6 KB

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