StackOfOpenElements.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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() { }
  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(NonnullRefPtr<DOM::Element> element) { m_elements.append(move(element)); }
  24. NonnullRefPtr<DOM::Element> pop() { return m_elements.take_last(); }
  25. const DOM::Element& current_node() const { return m_elements.last(); }
  26. DOM::Element& current_node() { return m_elements.last(); }
  27. bool has_in_scope(const FlyString& tag_name) const;
  28. bool has_in_button_scope(const FlyString& tag_name) const;
  29. bool has_in_table_scope(const FlyString& tag_name) const;
  30. bool has_in_list_item_scope(const FlyString& tag_name) const;
  31. bool has_in_select_scope(const FlyString& tag_name) const;
  32. bool has_in_scope(const DOM::Element&) const;
  33. bool contains(const DOM::Element&) const;
  34. bool contains(const FlyString& tag_name) const;
  35. const NonnullRefPtrVector<DOM::Element>& elements() const { return m_elements; }
  36. NonnullRefPtrVector<DOM::Element>& elements() { return m_elements; }
  37. void pop_until_an_element_with_tag_name_has_been_popped(const FlyString&);
  38. DOM::Element* topmost_special_node_below(const DOM::Element&);
  39. struct LastElementResult {
  40. DOM::Element* element;
  41. ssize_t index;
  42. };
  43. LastElementResult last_element_with_tag_name(const FlyString&);
  44. DOM::Element* element_immediately_above(DOM::Element const&);
  45. private:
  46. bool has_in_scope_impl(const FlyString& tag_name, const Vector<FlyString>&) const;
  47. bool has_in_scope_impl(const DOM::Element& target_node, const Vector<FlyString>&) const;
  48. NonnullRefPtrVector<DOM::Element> m_elements;
  49. };
  50. }