StackOfOpenElements.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Element.h>
  7. #include <LibWeb/HTML/Parser/HTMLParser.h>
  8. #include <LibWeb/HTML/Parser/StackOfOpenElements.h>
  9. #include <LibWeb/Namespace.h>
  10. namespace Web::HTML {
  11. static Vector<FlyString> s_base_list { "applet"_fly_string, "caption"_fly_string, "html"_fly_string, "table"_fly_string, "td"_fly_string, "th"_fly_string, "marquee"_fly_string, "object"_fly_string, "template"_fly_string };
  12. StackOfOpenElements::~StackOfOpenElements() = default;
  13. void StackOfOpenElements::visit_edges(JS::Cell::Visitor& visitor)
  14. {
  15. visitor.visit(m_elements);
  16. }
  17. bool StackOfOpenElements::has_in_scope_impl(FlyString const& tag_name, Vector<FlyString> const& list) const
  18. {
  19. for (auto const& element : m_elements.in_reverse()) {
  20. if (element->local_name() == tag_name)
  21. return true;
  22. if (list.contains_slow(element->local_name()))
  23. return false;
  24. }
  25. VERIFY_NOT_REACHED();
  26. }
  27. bool StackOfOpenElements::has_in_scope(FlyString const& tag_name) const
  28. {
  29. return has_in_scope_impl(tag_name, s_base_list);
  30. }
  31. bool StackOfOpenElements::has_in_scope_impl(const DOM::Element& target_node, Vector<FlyString> const& list) const
  32. {
  33. for (auto& element : m_elements.in_reverse()) {
  34. if (element.ptr() == &target_node)
  35. return true;
  36. if (list.contains_slow(element->local_name()))
  37. return false;
  38. }
  39. VERIFY_NOT_REACHED();
  40. }
  41. bool StackOfOpenElements::has_in_scope(const DOM::Element& target_node) const
  42. {
  43. return has_in_scope_impl(target_node, s_base_list);
  44. }
  45. bool StackOfOpenElements::has_in_button_scope(FlyString const& tag_name) const
  46. {
  47. auto list = s_base_list;
  48. list.append("button"_fly_string);
  49. return has_in_scope_impl(tag_name, list);
  50. }
  51. bool StackOfOpenElements::has_in_table_scope(FlyString const& tag_name) const
  52. {
  53. return has_in_scope_impl(tag_name, { "html"_fly_string, "table"_fly_string, "template"_fly_string });
  54. }
  55. bool StackOfOpenElements::has_in_list_item_scope(FlyString const& tag_name) const
  56. {
  57. auto list = s_base_list;
  58. list.append("ol"_fly_string);
  59. list.append("ul"_fly_string);
  60. return has_in_scope_impl(tag_name, list);
  61. }
  62. // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-select-scope
  63. // The stack of open elements is said to have a particular element in select scope
  64. // when it has that element in the specific scope consisting of all element types except the following:
  65. // - optgroup in the HTML namespace
  66. // - option in the HTML namespace
  67. // NOTE: In this case it's "all element types _except_"
  68. bool StackOfOpenElements::has_in_select_scope(FlyString const& tag_name) const
  69. {
  70. // https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-the-specific-scope
  71. // 1. Initialize node to be the current node (the bottommost node of the stack).
  72. for (auto& node : m_elements.in_reverse()) {
  73. // 2. If node is the target node, terminate in a match state.
  74. if (node->local_name() == tag_name)
  75. return true;
  76. // 3. Otherwise, if node is one of the element types in list, terminate in a failure state.
  77. // NOTE: Here "list" refers to all elements except option and optgroup
  78. if (node->local_name() != HTML::TagNames::option && node->local_name() != HTML::TagNames::optgroup)
  79. return false;
  80. // 4. Otherwise, set node to the previous entry in the stack of open elements and return to step 2.
  81. }
  82. // [4.] (This will never fail, since the loop will always terminate in the previous step if the top of the stack
  83. // — an html element — is reached.)
  84. VERIFY_NOT_REACHED();
  85. }
  86. bool StackOfOpenElements::contains(const DOM::Element& element) const
  87. {
  88. for (auto& element_on_stack : m_elements) {
  89. if (&element == element_on_stack.ptr())
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool StackOfOpenElements::contains_template_element() const
  95. {
  96. for (auto const& element : m_elements) {
  97. if (element->namespace_uri() != Namespace::HTML)
  98. continue;
  99. if (element->local_name() == HTML::TagNames::template_)
  100. return true;
  101. }
  102. return false;
  103. }
  104. void StackOfOpenElements::pop_until_an_element_with_tag_name_has_been_popped(FlyString const& tag_name)
  105. {
  106. while (m_elements.last()->namespace_uri() != Namespace::HTML || m_elements.last()->local_name() != tag_name)
  107. (void)pop();
  108. (void)pop();
  109. }
  110. GC::Ptr<DOM::Element> StackOfOpenElements::topmost_special_node_below(DOM::Element const& formatting_element)
  111. {
  112. GC::Ptr<DOM::Element> found_element = nullptr;
  113. for (auto& element : m_elements.in_reverse()) {
  114. if (element.ptr() == &formatting_element)
  115. break;
  116. if (HTMLParser::is_special_tag(element->local_name(), element->namespace_uri()))
  117. found_element = element.ptr();
  118. }
  119. return found_element.ptr();
  120. }
  121. StackOfOpenElements::LastElementResult StackOfOpenElements::last_element_with_tag_name(FlyString const& tag_name)
  122. {
  123. for (ssize_t i = m_elements.size() - 1; i >= 0; --i) {
  124. auto& element = m_elements[i];
  125. if (element->local_name() == tag_name)
  126. return { element.ptr(), i };
  127. }
  128. return { nullptr, -1 };
  129. }
  130. GC::Ptr<DOM::Element> StackOfOpenElements::element_immediately_above(DOM::Element const& target)
  131. {
  132. bool found_target = false;
  133. for (auto& element : m_elements.in_reverse()) {
  134. if (element.ptr() == &target) {
  135. found_target = true;
  136. } else if (found_target)
  137. return element.ptr();
  138. }
  139. return nullptr;
  140. }
  141. void StackOfOpenElements::remove(DOM::Element const& element)
  142. {
  143. m_elements.remove_first_matching([&element](auto& other) {
  144. return other.ptr() == &element;
  145. });
  146. }
  147. void StackOfOpenElements::replace(DOM::Element const& to_remove, GC::Ref<DOM::Element> to_add)
  148. {
  149. for (size_t i = 0; i < m_elements.size(); i++) {
  150. if (m_elements[i].ptr() == &to_remove) {
  151. m_elements.remove(i);
  152. m_elements.insert(i, to_add);
  153. break;
  154. }
  155. }
  156. }
  157. void StackOfOpenElements::insert_immediately_below(GC::Ref<DOM::Element> element_to_add, DOM::Element const& target)
  158. {
  159. for (size_t i = 0; i < m_elements.size(); i++) {
  160. if (m_elements[i].ptr() == &target) {
  161. m_elements.insert(i + 1, element_to_add);
  162. break;
  163. }
  164. }
  165. }
  166. }