StackOfOpenElements.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.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(GC::Ref<DOM::Element> element) { m_elements.append(element); }
  23. GC::Ref<DOM::Element> pop() { return *m_elements.take_last(); }
  24. void remove(DOM::Element const& element);
  25. void replace(DOM::Element const& to_remove, GC::Ref<DOM::Element> to_add);
  26. void insert_immediately_below(GC::Ref<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_scope(const DOM::Element&) const;
  34. bool contains(const DOM::Element&) const;
  35. [[nodiscard]] bool contains_template_element() const;
  36. auto const& elements() const { return m_elements; }
  37. auto& elements() { return m_elements; }
  38. void pop_until_an_element_with_tag_name_has_been_popped(FlyString const& local_name);
  39. GC::Ptr<DOM::Element> topmost_special_node_below(DOM::Element const&);
  40. struct LastElementResult {
  41. GC::Ptr<DOM::Element> element;
  42. ssize_t index;
  43. };
  44. LastElementResult last_element_with_tag_name(FlyString const&);
  45. GC::Ptr<DOM::Element> element_immediately_above(DOM::Element const&);
  46. void visit_edges(JS::Cell::Visitor&);
  47. private:
  48. bool has_in_scope_impl(FlyString const& tag_name, Vector<FlyString> const&) const;
  49. bool has_in_scope_impl(const DOM::Element& target_node, Vector<FlyString> const&) const;
  50. Vector<GC::Ref<DOM::Element>> m_elements;
  51. };
  52. }