StackOfOpenElements.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2020, 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(JS::make_handle(*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(FlyString const& tag_name) const;
  31. bool has_in_button_scope(FlyString const& tag_name) const;
  32. bool has_in_table_scope(FlyString const& tag_name) const;
  33. bool has_in_list_item_scope(FlyString const& tag_name) const;
  34. bool has_in_select_scope(FlyString const& tag_name) const;
  35. bool has_in_scope(const DOM::Element&) const;
  36. bool contains(const DOM::Element&) const;
  37. bool contains(FlyString const& tag_name) const;
  38. Vector<JS::Handle<DOM::Element>> const& elements() const { return m_elements; }
  39. Vector<JS::Handle<DOM::Element>>& elements() { return m_elements; }
  40. void pop_until_an_element_with_tag_name_has_been_popped(FlyString 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(FlyString const&);
  47. JS::GCPtr<DOM::Element> element_immediately_above(DOM::Element const&);
  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::Handle<DOM::Element>> m_elements;
  52. };
  53. }