StackOfOpenElements.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/DOM/Element.h>
  27. #include <LibWeb/Parser/HTMLDocumentParser.h>
  28. #include <LibWeb/Parser/StackOfOpenElements.h>
  29. namespace Web {
  30. static Vector<FlyString> s_base_list { "applet", "caption", "html", "table", "td", "th", "marquee", "object", "template" };
  31. StackOfOpenElements::~StackOfOpenElements()
  32. {
  33. }
  34. bool StackOfOpenElements::has_in_scope_impl(const FlyString& tag_name, const Vector<FlyString>& list) const
  35. {
  36. for (ssize_t i = m_elements.size() - 1; i >= 0; --i) {
  37. auto& node = m_elements.at(i);
  38. if (node.tag_name() == tag_name)
  39. return true;
  40. if (list.contains_slow(node.tag_name()))
  41. return false;
  42. }
  43. ASSERT_NOT_REACHED();
  44. }
  45. bool StackOfOpenElements::has_in_scope(const FlyString& tag_name) const
  46. {
  47. return has_in_scope_impl(tag_name, s_base_list);
  48. }
  49. bool StackOfOpenElements::has_in_scope_impl(const Element& target_node, const Vector<FlyString>& list) const
  50. {
  51. for (ssize_t i = m_elements.size() - 1; i >= 0; --i) {
  52. auto& node = m_elements.at(i);
  53. if (&node == &target_node)
  54. return true;
  55. if (list.contains_slow(node.tag_name()))
  56. return false;
  57. }
  58. ASSERT_NOT_REACHED();
  59. }
  60. bool StackOfOpenElements::has_in_scope(const Element& target_node) const
  61. {
  62. return has_in_scope_impl(target_node, s_base_list);
  63. }
  64. bool StackOfOpenElements::has_in_button_scope(const FlyString& tag_name) const
  65. {
  66. auto list = s_base_list;
  67. list.append("button");
  68. return has_in_scope_impl(tag_name, list);
  69. }
  70. bool StackOfOpenElements::has_in_table_scope(const FlyString& tag_name) const
  71. {
  72. auto list = s_base_list;
  73. list.append("html");
  74. list.append("table");
  75. list.append("template");
  76. return has_in_scope_impl(tag_name, list);
  77. }
  78. bool StackOfOpenElements::has_in_list_item_scope(const FlyString& tag_name) const
  79. {
  80. auto list = s_base_list;
  81. list.append("ol");
  82. list.append("ul");
  83. return has_in_scope_impl(tag_name, list);
  84. }
  85. bool StackOfOpenElements::has_in_select_scope(const FlyString& tag_name) const
  86. {
  87. auto list = s_base_list;
  88. list.append("option");
  89. list.append("optgroup");
  90. return has_in_scope_impl(tag_name, list);
  91. }
  92. bool StackOfOpenElements::contains(const Element& element) const
  93. {
  94. for (auto& element_on_stack : m_elements) {
  95. if (&element == &element_on_stack)
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool StackOfOpenElements::contains(const FlyString& tag_name) const
  101. {
  102. for (auto& element_on_stack : m_elements) {
  103. if (element_on_stack.tag_name() == tag_name)
  104. return true;
  105. }
  106. return false;
  107. }
  108. void StackOfOpenElements::pop_until_an_element_with_tag_name_has_been_popped(const FlyString& tag_name)
  109. {
  110. while (m_elements.last().tag_name() != tag_name)
  111. pop();
  112. pop();
  113. }
  114. Element* StackOfOpenElements::topmost_special_node_below(const Element& formatting_element)
  115. {
  116. Element* found_element = nullptr;
  117. for (ssize_t i = m_elements.size() - 1; i >= 0; --i) {
  118. auto& element = m_elements[i];
  119. if (&element == &formatting_element)
  120. break;
  121. if (HTMLDocumentParser::is_special_tag(element.tag_name()))
  122. found_element = &element;
  123. }
  124. return found_element;
  125. }
  126. }