aboutsummaryrefslogtreecommitdiff
path: root/ricochet/src/parser/tree_builder.cc
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-10-30 02:24:12 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-10-30 02:24:12 +0800
commit464313d02b76385d3a2e8809a4595afabca88dae (patch)
tree6cbf81416b856ff15fdc4239de5e3624ef10b2cc /ricochet/src/parser/tree_builder.cc
parentcf58b3b4dfa0dbbd0ca492484ca241b320c4ba9e (diff)
parentbdefdb301db843e87aee8c6c35808d295bb66ee5 (diff)
downloadrcc-project-master.tar.gz
rcc-project-master.tar.xz
Merge remote-tracking branch 'ricochet-import/master'HEADmaster
Diffstat (limited to 'ricochet/src/parser/tree_builder.cc')
-rw-r--r--ricochet/src/parser/tree_builder.cc85
1 files changed, 85 insertions, 0 deletions
diff --git a/ricochet/src/parser/tree_builder.cc b/ricochet/src/parser/tree_builder.cc
new file mode 100644
index 0000000..64f841b
--- /dev/null
+++ b/ricochet/src/parser/tree_builder.cc
@@ -0,0 +1,85 @@
+/*
+ * Ricochet - A minimalist CLI web browser for fast, distraction-free navigation.
+ * Copyright (C) 2026 Richard Qin
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "ricochet/parser/tree_builder.hh"
+#include "ricochet/parser/dom_tree.hh"
+#include <utility>
+#include <vector>
+
+namespace ricochet::parser {
+
+namespace {
+struct TokenVisitor
+{
+ std::vector<DomNode>* stack;
+
+ void operator()( const TextToken& token ) const
+ {
+ stack->back().children.emplace_back(
+ DomNode { .tag_name = "", .text_content = token.content, .children = {} } );
+ }
+
+ void operator()( const TagOpenToken& token ) const
+ {
+ const std::size_t space_pos = token.name.find( ' ' );
+ const std::string base_name
+ = ( space_pos == std::string::npos ) ? token.name : token.name.substr( 0, space_pos );
+
+ DomNode new_node { .tag_name = token.name, .text_content = "", .children = {} };
+
+ if ( base_name == "meta" || base_name == "link" || base_name == "img" || base_name == "br" || base_name == "hr"
+ || base_name == "input" || base_name == "base" || base_name == "col" ) {
+
+ stack->back().children.push_back( std::move( new_node ) );
+ } else {
+ stack->push_back( std::move( new_node ) );
+ }
+ }
+
+ void operator()( const TagCloseToken& /*token*/ ) const
+ {
+ if ( stack->size() > 1 ) {
+ DomNode completed_node = std::move( stack->back() );
+ stack->pop_back();
+ stack->back().children.push_back( std::move( completed_node ) );
+ }
+ }
+};
+} // namespace
+
+DomNode TreeBuilder::build( // NOLINT(readability-convert-member-functions-to-static)
+ const std::vector<HtmlToken>& tokens ) const
+{
+ std::vector<DomNode> stack;
+ stack.emplace_back( DomNode { .tag_name = "Document", .text_content = "", .children = {} } );
+
+ TokenVisitor visitor { &stack };
+ for ( const auto& token : tokens ) {
+ std::visit( visitor, token );
+ }
+
+ while ( stack.size() > 1 ) {
+ DomNode completed_node = std::move( stack.back() );
+ stack.pop_back();
+ stack.back().children.push_back( std::move( completed_node ) );
+ }
+
+ return std::move( stack.front() );
+}
+
+} // namespace ricochet::parser \ No newline at end of file