aboutsummaryrefslogtreecommitdiff
path: root/ricochet/src/core
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-04-11 03:26:17 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-04-11 03:26:17 +0800
commit168504eab3727bd028db9c8b9e377571ae601ac5 (patch)
tree3dd1645b3a1f1449765c61f174fe33137a49b60b /ricochet/src/core
parent79f11cd16452b0ba2d7d2be27240ce5fa79d0002 (diff)
downloadrcc-project-168504eab3727bd028db9c8b9e377571ae601ac5.tar.gz
rcc-project-168504eab3727bd028db9c8b9e377571ae601ac5.tar.xz
feat: implement DOM tree builder to transform HTML tokens into a hierarchical structure
Diffstat (limited to 'ricochet/src/core')
-rw-r--r--ricochet/src/core/browser.cc35
1 files changed, 19 insertions, 16 deletions
diff --git a/ricochet/src/core/browser.cc b/ricochet/src/core/browser.cc
index 1e9d044..7acc915 100644
--- a/ricochet/src/core/browser.cc
+++ b/ricochet/src/core/browser.cc
@@ -19,20 +19,23 @@
#include "ricochet/core/browser.hh"
#include "ricochet/net/http_client.hh"
#include "ricochet/parser/html_lexer.hh"
+#include "ricochet/parser/tree_builder.hh"
#include <iostream>
namespace {
-struct TokenPrinter {
- void operator()(const ricochet::parser::TextToken& t) const {
- std::cout << "[TEXT] " << t.content << "\n";
- }
- void operator()(const ricochet::parser::TagOpenToken& t) const {
- std::cout << "[OPEN] <" << t.name << ">\n";
- }
- void operator()(const ricochet::parser::TagCloseToken& t) const {
- std::cout << "[CLOSE] </" << t.name << ">\n";
+void print_dom_tree(const ricochet::parser::DomNode& node, std::size_t depth = 0) // NOLINT(misc-no-recursion)
+{
+ const std::string indent(depth * 2, ' ');
+
+ if (node.tag_name.empty()) {
+ std::cout << indent << "\"" << node.text_content << "\"\n";
+ } else {
+ std::cout << indent << "<" << node.tag_name << ">\n";
+ for (const auto& child : node.children) {
+ print_dom_tree(child, depth + 1);
+ }
}
-};
+}
} // namespace
namespace ricochet::core {
@@ -49,15 +52,15 @@ int Browser::run( std::string_view initial_url )
}
const auto& response = response_result.value();
- std::cout << "[=== HTTP Status: " << response.status_code << " ===]\n";
- std::cout << "\n[=== Tokenizing HTML ===]\n";
const parser::HtmlLexer lexer;
- auto tokens = lexer.tokenize(response.body);
+ const auto tokens = lexer.tokenize(response.body);
- for (const auto& token : tokens) {
- std::visit(TokenPrinter{}, token);
- }
+ const parser::TreeBuilder builder;
+ const auto dom_root = builder.build(tokens);
+
+ std::cout << "\n[=== Abstract Syntax Tree (DOM) ===]\n";
+ print_dom_tree(dom_root);
return 0;
}