Truly independent web browser
Utilities: Let dump-html-tree parse its input as a fragment
Andreas Kling6f6f4b6372eb
diff --git a/Utilities/dump-html-tree.cpp b/Utilities/dump-html-tree.cppindex 307f0e3d95e..3099f5a18cf 100644--- a/Utilities/dump-html-tree.cpp+++ b/Utilities/dump-html-tree.cpp@@ -21,6 +21,7 @@#include <LibWeb/DOM/DocumentFragment.h>#include <LibWeb/DOM/DocumentType.h>#include <LibWeb/DOM/Element.h>+#include <LibWeb/DOM/ElementFactory.h>#include <LibWeb/DOM/ShadowRoot.h>#include <LibWeb/HTML/CustomElements/CustomElementRegistry.h>#include <LibWeb/HTML/HTMLTemplateElement.h>@@ -297,10 +298,18 @@ static GC::Ref<Web::DOM::Document> parse_html(Web::Page& page, Web::DOM::Documenreturn document;}+static GC::Ref<Web::DOM::DocumentFragment> parse_html_fragment(Web::DOM::Element& context, Utf16View input, Web::HTML::FragmentFastPath fast_path)+{+ return MUST(Web::HTML::HTMLParser::parse_html_fragment(GC::Ref { context }, input,+ Web::HTML::HTMLParser::AllowDeclarativeShadowRoots::No, Web::HTML::ParserScriptingMode::Inert, fast_path));+}+ErrorOr<int> ladybird_main(Main::Arguments arguments){StringView file_path;+ StringView fragment_context;bool silent = false;+ bool skip_fragment_fast_path = false;int iterations = 1;Core::ArgsParser args_parser;@@ -309,6 +318,8 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)"Use --silent for perf work on the parser itself.");args_parser.add_positional_argument(file_path, "Path to HTML file (or - for stdin)", "file", Core::ArgsParser::Required::No);args_parser.add_option(silent, "Don't print anything (for benchmarking)", "silent", 'q');+ args_parser.add_option(fragment_context, "Parse the input as a fragment in an element with this tag name", "fragment", 'f', "tag");+ args_parser.add_option(skip_fragment_fast_path, "Parse fragments with the tree builder only", "skip-fragment-fast-path");args_parser.add_option(iterations, "Run the parser N times, reporting timing unless --silent is used", "iterations", 'n', "count");args_parser.parse(arguments);@@ -346,19 +357,31 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)auto timer = Core::ElapsedTimer::start_new();- for (int i = 0; i < iterations; i++) {- auto document = parse_html(page, origin_document, input);- if (!silent && i == 0)- dump_tree(document);+ if (fragment_context.is_empty()) {+ for (int i = 0; i < iterations; i++) {+ auto document = parse_html(page, origin_document, input);+ if (!silent && i == 0)+ dump_tree(document);+ }+ } else {+ auto document = parse_html(page, origin_document, "<!DOCTYPE html>"sv);+ auto context = MUST(Web::DOM::create_element(document, Utf16FlyString::from_utf8(fragment_context), Web::Namespace::HTML));+ auto fragment_input = Utf16String::from_utf8_with_replacement_character(input);+ auto fast_path = skip_fragment_fast_path ? Web::HTML::FragmentFastPath::Skip : Web::HTML::FragmentFastPath::Allow;+ for (int i = 0; i < iterations; i++) {+ auto fragment = parse_html_fragment(context, fragment_input, fast_path);+ if (!silent && i == 0)+ dump_tree(fragment);+ }}if (iterations > 1 && !silent) {- auto elapsed_ms = timer.elapsed_milliseconds();- warnln("input={}B iterations={} total={}ms ({:.3f}ms/iter)",+ auto elapsed_us = timer.elapsed_time().to_microseconds();+ warnln("input={}B iterations={} total={}ms ({:.4f}ms/iter)",input_data.size(),iterations,- elapsed_ms,- static_cast<double>(elapsed_ms) / iterations);+ elapsed_us / 1000,+ static_cast<double>(elapsed_us) / 1000 / iterations);}return 0;