mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:47:45 +00:00
LibWeb: Factor out the "stack of open elements" into its own class
This will allow us to write more expressive parsing code. :^)
This commit is contained in:
parent
96cc1138c0
commit
53d2f4df70
5 changed files with 93 additions and 28 deletions
|
@ -88,6 +88,7 @@ set(SOURCES
|
||||||
Parser/HTMLParser.cpp
|
Parser/HTMLParser.cpp
|
||||||
Parser/HTMLToken.cpp
|
Parser/HTMLToken.cpp
|
||||||
Parser/HTMLTokenizer.cpp
|
Parser/HTMLTokenizer.cpp
|
||||||
|
Parser/StackOfOpenElements.cpp
|
||||||
ResourceLoader.cpp
|
ResourceLoader.cpp
|
||||||
StylePropertiesModel.cpp
|
StylePropertiesModel.cpp
|
||||||
URLEncoder.cpp
|
URLEncoder.cpp
|
||||||
|
|
|
@ -109,21 +109,21 @@ void HTMLDocumentParser::handle_before_html(HTMLToken& token)
|
||||||
if (token.is_start_tag() && token.tag_name() == "html") {
|
if (token.is_start_tag() && token.tag_name() == "html") {
|
||||||
auto element = create_element_for(token);
|
auto element = create_element_for(token);
|
||||||
document().append_child(element);
|
document().append_child(element);
|
||||||
m_stack_of_open_elements.append(element);
|
m_stack_of_open_elements.push(move(element));
|
||||||
m_insertion_mode = InsertionMode::BeforeHead;
|
m_insertion_mode = InsertionMode::BeforeHead;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullRefPtr<Node> HTMLDocumentParser::current_node()
|
Element& HTMLDocumentParser::current_node()
|
||||||
{
|
{
|
||||||
return m_stack_of_open_elements.last();
|
return m_stack_of_open_elements.current_node();
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Node> HTMLDocumentParser::find_appropriate_place_for_inserting_node()
|
RefPtr<Node> HTMLDocumentParser::find_appropriate_place_for_inserting_node()
|
||||||
{
|
{
|
||||||
auto target = current_node();
|
auto& target = current_node();
|
||||||
if (m_foster_parenting) {
|
if (m_foster_parenting) {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ RefPtr<Element> HTMLDocumentParser::insert_html_element(HTMLToken& token)
|
||||||
auto element = create_element_for(token);
|
auto element = create_element_for(token);
|
||||||
// FIXME: Check if it's possible to insert `element` at `adjusted_insertion_location`
|
// FIXME: Check if it's possible to insert `element` at `adjusted_insertion_location`
|
||||||
adjusted_insertion_location->append_child(element);
|
adjusted_insertion_location->append_child(element);
|
||||||
m_stack_of_open_elements.append(element);
|
m_stack_of_open_elements.push(element);
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,14 +164,14 @@ void HTMLDocumentParser::handle_in_head(HTMLToken& token)
|
||||||
{
|
{
|
||||||
if (token.is_start_tag() && token.tag_name() == "meta") {
|
if (token.is_start_tag() && token.tag_name() == "meta") {
|
||||||
auto element = insert_html_element(token);
|
auto element = insert_html_element(token);
|
||||||
m_stack_of_open_elements.take_last();
|
m_stack_of_open_elements.pop();
|
||||||
if (token.is_self_closing()) {
|
if (token.is_self_closing()) {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (token.is_end_tag() && token.tag_name() == "head") {
|
if (token.is_end_tag() && token.tag_name() == "head") {
|
||||||
m_stack_of_open_elements.take_last();
|
m_stack_of_open_elements.pop();
|
||||||
m_insertion_mode = InsertionMode::AfterHead;
|
m_insertion_mode = InsertionMode::AfterHead;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -243,21 +243,8 @@ AnythingElse:
|
||||||
void HTMLDocumentParser::generate_implied_end_tags()
|
void HTMLDocumentParser::generate_implied_end_tags()
|
||||||
{
|
{
|
||||||
Vector<String> names { "dd", "dt", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc" };
|
Vector<String> names { "dd", "dt", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc" };
|
||||||
while (names.contains_slow(current_node()->tag_name()))
|
while (names.contains_slow(current_node().tag_name()))
|
||||||
m_stack_of_open_elements.take_last();
|
m_stack_of_open_elements.pop();
|
||||||
}
|
|
||||||
|
|
||||||
bool HTMLDocumentParser::stack_of_open_elements_has_element_with_tag_name_in_scope(const FlyString& tag_name)
|
|
||||||
{
|
|
||||||
Vector<String> list { "applet", "caption", "html", "table", "td", "th", "marquee", "object", "template" };
|
|
||||||
for (ssize_t i = m_stack_of_open_elements.size() - 1; i >= 0; --i) {
|
|
||||||
auto& node = m_stack_of_open_elements.at(i);
|
|
||||||
if (node.tag_name() == tag_name)
|
|
||||||
return true;
|
|
||||||
if (list.contains_slow(node.tag_name()))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ASSERT_NOT_REACHED();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTMLDocumentParser::handle_after_body(HTMLToken& token)
|
void HTMLDocumentParser::handle_after_body(HTMLToken& token)
|
||||||
|
@ -284,7 +271,7 @@ void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
|
||||||
void HTMLDocumentParser::handle_in_body(HTMLToken& token)
|
void HTMLDocumentParser::handle_in_body(HTMLToken& token)
|
||||||
{
|
{
|
||||||
if (token.is_end_tag() && token.tag_name() == "body") {
|
if (token.is_end_tag() && token.tag_name() == "body") {
|
||||||
if (!stack_of_open_elements_has_element_with_tag_name_in_scope("body")) {
|
if (!m_stack_of_open_elements.has_in_scope("body")) {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,17 +297,17 @@ void HTMLDocumentParser::handle_in_body(HTMLToken& token)
|
||||||
if (token.is_end_tag() && names.contains_slow(token.tag_name())) {
|
if (token.is_end_tag() && names.contains_slow(token.tag_name())) {
|
||||||
// FIXME: If the stack of open elements has a p element in button scope, then close a p element.
|
// FIXME: If the stack of open elements has a p element in button scope, then close a p element.
|
||||||
|
|
||||||
if (!stack_of_open_elements_has_element_with_tag_name_in_scope(token.tag_name())) {
|
if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_implied_end_tags();
|
generate_implied_end_tags();
|
||||||
|
|
||||||
if (current_node()->tag_name() != token.tag_name()) {
|
if (current_node().tag_name() != token.tag_name()) {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_stack_of_open_elements.take_last();
|
m_stack_of_open_elements.pop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <AK/NonnullRefPtrVector.h>
|
#include <AK/NonnullRefPtrVector.h>
|
||||||
#include <LibWeb/DOM/Node.h>
|
#include <LibWeb/DOM/Node.h>
|
||||||
#include <LibWeb/Parser/HTMLTokenizer.h>
|
#include <LibWeb/Parser/HTMLTokenizer.h>
|
||||||
|
#include <LibWeb/Parser/StackOfOpenElements.h>
|
||||||
|
|
||||||
#define ENUMERATE_INSERTION_MODES \
|
#define ENUMERATE_INSERTION_MODES \
|
||||||
__ENUMERATE_INSERTION_MODE(Initial) \
|
__ENUMERATE_INSERTION_MODE(Initial) \
|
||||||
|
@ -93,10 +94,10 @@ private:
|
||||||
NonnullRefPtr<Element> create_element_for(HTMLToken&);
|
NonnullRefPtr<Element> create_element_for(HTMLToken&);
|
||||||
RefPtr<Node> find_appropriate_place_for_inserting_node();
|
RefPtr<Node> find_appropriate_place_for_inserting_node();
|
||||||
RefPtr<Element> insert_html_element(HTMLToken&);
|
RefPtr<Element> insert_html_element(HTMLToken&);
|
||||||
NonnullRefPtr<Node> current_node();
|
Element& current_node();
|
||||||
|
|
||||||
InsertionMode m_insertion_mode { InsertionMode::Initial };
|
InsertionMode m_insertion_mode { InsertionMode::Initial };
|
||||||
NonnullRefPtrVector<Node> m_stack_of_open_elements;
|
StackOfOpenElements m_stack_of_open_elements;
|
||||||
|
|
||||||
HTMLTokenizer m_tokenizer;
|
HTMLTokenizer m_tokenizer;
|
||||||
|
|
||||||
|
|
23
Libraries/LibWeb/Parser/StackOfOpenElements.cpp
Normal file
23
Libraries/LibWeb/Parser/StackOfOpenElements.cpp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#include <LibWeb/DOM/Element.h>
|
||||||
|
#include <LibWeb/Parser/StackOfOpenElements.h>
|
||||||
|
|
||||||
|
namespace Web {
|
||||||
|
|
||||||
|
StackOfOpenElements::~StackOfOpenElements()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StackOfOpenElements::has_in_scope(const FlyString& tag_name) const
|
||||||
|
{
|
||||||
|
static Vector<FlyString> list { "applet", "caption", "html", "table", "td", "th", "marquee", "object", "template" };
|
||||||
|
for (ssize_t i = m_elements.size() - 1; i >= 0; --i) {
|
||||||
|
auto& node = m_elements.at(i);
|
||||||
|
if (node.tag_name() == tag_name)
|
||||||
|
return true;
|
||||||
|
if (list.contains_slow(node.tag_name()))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
53
Libraries/LibWeb/Parser/StackOfOpenElements.h
Normal file
53
Libraries/LibWeb/Parser/StackOfOpenElements.h
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/NonnullRefPtrVector.h>
|
||||||
|
#include <LibWeb/DOM/Element.h>
|
||||||
|
#include <LibWeb/Forward.h>
|
||||||
|
|
||||||
|
namespace Web {
|
||||||
|
|
||||||
|
class StackOfOpenElements {
|
||||||
|
public:
|
||||||
|
StackOfOpenElements() { }
|
||||||
|
~StackOfOpenElements();
|
||||||
|
|
||||||
|
bool is_empty() const { return m_elements.is_empty(); }
|
||||||
|
void push(NonnullRefPtr<Element> element) { m_elements.append(move(element)); }
|
||||||
|
NonnullRefPtr<Element> pop() { return m_elements.take_last(); }
|
||||||
|
|
||||||
|
const Element& current_node() const { return m_elements.last(); }
|
||||||
|
Element& current_node() { return m_elements.last(); }
|
||||||
|
|
||||||
|
bool has_in_scope(const FlyString& tag_name) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
NonnullRefPtrVector<Element> m_elements;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue