StackOfOpenElements.h 2.7 KB

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