2020-05-24 19:24:36 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-05-24 19:24:36 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-24 19:24:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
namespace Web::HTML {
|
2020-05-24 19:24:36 +02:00
|
|
|
|
2022-02-14 22:30:50 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/parsing.html#stack-of-open-elements
|
2020-05-24 19:24:36 +02:00
|
|
|
class StackOfOpenElements {
|
|
|
|
public:
|
2022-02-14 22:30:50 +01:00
|
|
|
// Initially, the stack of open elements is empty.
|
|
|
|
// The stack grows downwards; the topmost node on the stack is the first one added to the stack,
|
|
|
|
// and the bottommost node of the stack is the most recently added node in the stack
|
|
|
|
// (notwithstanding when the stack is manipulated in a random access fashion as part of the handling for misnested tags).
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
StackOfOpenElements() = default;
|
2020-05-24 19:24:36 +02:00
|
|
|
~StackOfOpenElements();
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
DOM::Element& first() { return *m_elements.first(); }
|
|
|
|
DOM::Element& last() { return *m_elements.last(); }
|
2020-05-28 00:27:46 +02:00
|
|
|
|
2020-05-24 19:24:36 +02:00
|
|
|
bool is_empty() const { return m_elements.is_empty(); }
|
2024-11-15 04:01:23 +13:00
|
|
|
void push(GC::Ref<DOM::Element> element) { m_elements.append(element); }
|
|
|
|
GC::Ref<DOM::Element> pop() { return *m_elements.take_last(); }
|
2022-03-20 02:11:42 +01:00
|
|
|
void remove(DOM::Element const& element);
|
2024-11-15 04:01:23 +13:00
|
|
|
void replace(DOM::Element const& to_remove, GC::Ref<DOM::Element> to_add);
|
|
|
|
void insert_immediately_below(GC::Ref<DOM::Element> element_to_add, DOM::Element const& target);
|
2020-05-24 19:24:36 +02:00
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
const DOM::Element& current_node() const { return *m_elements.last(); }
|
|
|
|
DOM::Element& current_node() { return *m_elements.last(); }
|
2020-05-24 19:24:36 +02:00
|
|
|
|
2023-10-01 20:07:44 +13:00
|
|
|
bool has_in_scope(FlyString const& tag_name) const;
|
|
|
|
bool has_in_button_scope(FlyString const& tag_name) const;
|
|
|
|
bool has_in_table_scope(FlyString const& tag_name) const;
|
|
|
|
bool has_in_list_item_scope(FlyString const& tag_name) const;
|
|
|
|
bool has_in_select_scope(FlyString const& tag_name) const;
|
2020-05-24 19:24:36 +02:00
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
bool has_in_scope(const DOM::Element&) const;
|
2020-05-27 23:22:42 +02:00
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
bool contains(const DOM::Element&) const;
|
2024-11-04 17:14:25 +01:00
|
|
|
[[nodiscard]] bool contains_template_element() const;
|
2020-05-24 22:39:59 +02:00
|
|
|
|
2022-10-17 10:46:11 +02:00
|
|
|
auto const& elements() const { return m_elements; }
|
|
|
|
auto& elements() { return m_elements; }
|
2020-05-24 22:39:59 +02:00
|
|
|
|
2024-11-04 17:14:25 +01:00
|
|
|
void pop_until_an_element_with_tag_name_has_been_popped(FlyString const& local_name);
|
2020-05-28 18:09:31 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<DOM::Element> topmost_special_node_below(DOM::Element const&);
|
2020-05-29 22:06:05 +02:00
|
|
|
|
2020-08-19 22:30:33 +01:00
|
|
|
struct LastElementResult {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<DOM::Element> element;
|
2020-08-19 22:30:33 +01:00
|
|
|
ssize_t index;
|
|
|
|
};
|
2023-10-01 20:07:44 +13:00
|
|
|
LastElementResult last_element_with_tag_name(FlyString const&);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<DOM::Element> element_immediately_above(DOM::Element const&);
|
2020-06-21 17:00:55 +02:00
|
|
|
|
2022-10-17 10:46:11 +02:00
|
|
|
void visit_edges(JS::Cell::Visitor&);
|
|
|
|
|
2020-05-24 19:24:36 +02:00
|
|
|
private:
|
2023-10-01 20:07:44 +13:00
|
|
|
bool has_in_scope_impl(FlyString const& tag_name, Vector<FlyString> const&) const;
|
|
|
|
bool has_in_scope_impl(const DOM::Element& target_node, Vector<FlyString> const&) const;
|
2020-05-24 22:21:25 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
Vector<GC::Ref<DOM::Element>> m_elements;
|
2020-05-24 19:24:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|