diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-04-11 03:26:17 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-04-11 03:26:17 +0800 |
| commit | 828b10cc337784444b18e4c178a75f87c771412a (patch) | |
| tree | 47b2b403853fc6bbf623832015c13af2fa724125 /src/core/browser.cc | |
| parent | 085bdfaee339d471fd8d49c0b349995d69bf3565 (diff) | |
| download | rwebbrowser-828b10cc337784444b18e4c178a75f87c771412a.tar.gz rwebbrowser-828b10cc337784444b18e4c178a75f87c771412a.tar.xz | |
feat: implement DOM tree builder to transform HTML tokens into a hierarchical structure
Diffstat (limited to 'src/core/browser.cc')
| -rw-r--r-- | src/core/browser.cc | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/core/browser.cc b/src/core/browser.cc index 1e9d044..7acc915 100644 --- a/src/core/browser.cc +++ b/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; } |
