StackOfOpenElements.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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::contains(const Element& element) const
  86. {
  87. for (auto& element_on_stack : m_elements) {
  88. if (&element == &element_on_stack)
  89. return true;
  90. }
  91. return false;
  92. }
  93. bool StackOfOpenElements::contains(const FlyString& tag_name) const
  94. {
  95. for (auto& element_on_stack : m_elements) {
  96. if (element_on_stack.tag_name() == tag_name)
  97. return true;
  98. }
  99. return false;
  100. }
  101. void StackOfOpenElements::pop_until_an_element_with_tag_name_has_been_popped(const FlyString& tag_name)
  102. {
  103. while (m_elements.last().tag_name() != tag_name)
  104. pop();
  105. pop();
  106. }
  107. Element* StackOfOpenElements::topmost_special_node_below(const Element& formatting_element)
  108. {
  109. Element* found_element = nullptr;
  110. for (ssize_t i = m_elements.size() - 1; i >= 0; ++i) {
  111. auto& element = m_elements[i];
  112. if (&element == &formatting_element)
  113. break;
  114. if (HTMLDocumentParser::is_special_tag(element.tag_name()))
  115. found_element = &element;
  116. }
  117. return found_element;
  118. }
  119. }