srctree

Robin Linden parent 00d04fd8 95413565
html: Move the parser actions to a separate file

inlinesplit
html/parser.h added: 75, removed: 59, total 16
@@ -5,6 +5,7 @@
#ifndef HTML_PARSER_H_
#define HTML_PARSER_H_
 
#include "html/parser_actions.h"
#include "html/parser_states.h"
 
#include "dom/dom.h"
 
filename was Deleted added: 75, removed: 59, total 16
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#ifndef HTML_PARSER_ACTIONS_H_
#define HTML_PARSER_ACTIONS_H_
 
#include "html/parser_states.h"
 
#include "dom/dom.h"
#include "html2/tokenizer.h"
 
#include <algorithm>
#include <stack>
#include <utility>
#include <variant>
#include <vector>
 
namespace html {
 
class Actions {
public:
Actions(dom::Document &document,
html2::Tokenizer &tokenizer,
bool scripting,
std::stack<dom::Element *> &open_elements)
: document_{document}, tokenizer_{tokenizer}, scripting_{scripting}, open_elements_{open_elements} {}
 
dom::Document &document() { return document_; }
std::stack<dom::Element *> &open_elements() { return open_elements_; }
bool scripting() const { return scripting_; }
 
void insert_element_for(html2::StartTagToken const &token) {
auto into_dom_attributes = [](std::vector<html2::Attribute> const &attributes) -> dom::AttrMap {
dom::AttrMap attrs{};
for (auto const &[name, value] : attributes) {
attrs[name] = value;
}
 
return attrs;
};
 
insert({token.tag_name, into_dom_attributes(token.attributes)});
}
 
void insert(dom::Element element) {
dom::Node &node = open_elements_.top()->children.emplace_back(std::move(element));
open_elements_.push(&std::get<dom::Element>(node));
}
 
void set_tokenizer_state(html2::State state) { tokenizer_.set_state(state); }
 
void store_original_insertion_mode(InsertionMode mode) { original_insertion_mode_ = std::move(mode); }
InsertionMode original_insertion_mode() { return std::move(original_insertion_mode_); }
 
private:
dom::Document &document_;
html2::Tokenizer &tokenizer_;
bool scripting_;
InsertionMode original_insertion_mode_;
std::stack<dom::Element *> &open_elements_;
};
 
} // namespace html
 
#endif
 
html/parser_states.cpp added: 75, removed: 59, total 16
@@ -4,6 +4,8 @@
 
#include "html/parser_states.h"
 
#include "html/parser_actions.h"
 
#include "dom/dom.h"
#include "html2/tokenizer.h"
#include "util/string.h"
 
html/parser_states.h added: 75, removed: 59, total 16
@@ -5,19 +5,8 @@
#ifndef HTML_PARSER_STATES_H_
#define HTML_PARSER_STATES_H_
 
#include "dom/dom.h"
#include "html2/tokenizer.h"
#include "util/string.h"
 
#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <sstream>
#include <stack>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
 
namespace html {
@@ -138,49 +127,6 @@ struct Text {
std::optional<InsertionMode> process(Actions &, html2::Token const &);
};
 
class Actions {
public:
Actions(dom::Document &document,
html2::Tokenizer &tokenizer,
bool scripting,
std::stack<dom::Element *> &open_elements)
: document_{document}, tokenizer_{tokenizer}, scripting_{scripting}, open_elements_{open_elements} {}
 
dom::Document &document() { return document_; }
std::stack<dom::Element *> &open_elements() { return open_elements_; }
bool scripting() const { return scripting_; }
 
void insert_element_for(html2::StartTagToken const &token) {
auto into_dom_attributes = [](std::vector<html2::Attribute> const &attributes) -> dom::AttrMap {
dom::AttrMap attrs{};
for (auto const &[name, value] : attributes) {
attrs[name] = value;
}
 
return attrs;
};
 
insert({token.tag_name, into_dom_attributes(token.attributes)});
}
 
void insert(dom::Element element) {
dom::Node &node = open_elements_.top()->children.emplace_back(std::move(element));
open_elements_.push(&std::get<dom::Element>(node));
}
 
void set_tokenizer_state(html2::State state) { tokenizer_.set_state(state); }
 
void store_original_insertion_mode(InsertionMode mode) { original_insertion_mode_ = std::move(mode); }
InsertionMode original_insertion_mode() { return std::move(original_insertion_mode_); }
 
private:
dom::Document &document_;
html2::Tokenizer &tokenizer_;
bool scripting_;
InsertionMode original_insertion_mode_;
std::stack<dom::Element *> &open_elements_;
};
 
} // namespace html
 
#endif
 
html/parser_states_test.cpp added: 75, removed: 59, total 16
@@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "html/parser_actions.h"
#include "html/parser_states.h"
 
#include "etest/etest.h"