StackOfOpenElements.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtrVector.h>
  8. #include <LibWeb/DOM/Element.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::HTML {
  11. // https://html.spec.whatwg.org/multipage/parsing.html#stack-of-open-elements
  12. class StackOfOpenElements {
  13. public:
  14. // Initially, the stack of open elements is empty.
  15. // The stack grows downwards; the topmost node on the stack is the first one added to the stack,
  16. // and the bottommost node of the stack is the most recently added node in the stack
  17. // (notwithstanding when the stack is manipulated in a random access fashion as part of the handling for misnested tags).
  18. StackOfOpenElements() = default;
  19. ~StackOfOpenElements();
  20. DOM::Element& first() { return *m_elements.first(); }
  21. DOM::Element& last() { return *m_elements.last(); }
  22. bool is_empty() const { return m_elements.is_empty(); }
  23. void push(JS::NonnullGCPtr<DOM::Element> element) { m_elements.append(element); }
  24. JS::NonnullGCPtr<DOM::Element> pop() { return *m_elements.take_last(); }
  25. void remove(DOM::Element const& element);
  26. void replace(DOM::Element const& to_remove, JS::NonnullGCPtr<DOM::Element> to_add);
  27. void insert_immediately_below(JS::NonnullGCPtr<DOM::Element> element_to_add, DOM::Element const& target);
  28. const DOM::Element& current_node() const { return *m_elements.last(); }
  29. DOM::Element& current_node() { return *m_elements.last(); }
  30. bool has_in_scope(DeprecatedFlyString const& tag_name) const;
  31. bool has_in_button_scope(DeprecatedFlyString const& tag_name) const;
  32. bool has_in_table_scope(DeprecatedFlyString const& tag_name) const;
  33. bool has_in_list_item_scope(DeprecatedFlyString const& tag_name) const;
  34. bool has_in_select_scope(DeprecatedFlyString const& tag_name) const;
  35. bool has_in_scope(const DOM::Element&) const;
  36. bool contains(const DOM::Element&) const;
  37. bool contains(DeprecatedFlyString const& tag_name) const;
  38. auto const& elements() const { return m_elements; }
  39. auto& elements() { return m_elements; }
  40. void pop_until_an_element_with_tag_name_has_been_popped(DeprecatedFlyString const&);
  41. JS::GCPtr<DOM::Element> topmost_special_node_below(DOM::Element const&);
  42. struct LastElementResult {
  43. JS::GCPtr<DOM::Element> element;
  44. ssize_t index;
  45. };
  46. LastElementResult last_element_with_tag_name(DeprecatedFlyString const&);
  47. JS::GCPtr<DOM::Element> element_immediately_above(DOM::Element const&);
  48. void visit_edges(JS::Cell::Visitor&);
  49. private:
  50. bool has_in_scope_impl(DeprecatedFlyString const& tag_name, Vector<DeprecatedFlyString> const&) const;
  51. bool has_in_scope_impl(const DOM::Element& target_node, Vector<DeprecatedFlyString> const&) const;
  52. Vector<JS::NonnullGCPtr<DOM::Element>> m_elements;
  53. };
  54. }