Files
ladybird/Libraries/LibWeb/HTML/Parser/SpeculativeHTMLParser.h
Aliaksandr Kalenik 70ac025eff LibWeb: Implement the speculative HTML parser
When the HTML parser blocks on a synchronous external script, run a
separate tokenizer over the unparsed input and issue speculative fetches
for the resources it finds (script src, link rel=stylesheet|preload, img
src), with <base href> tracking and template/foreign-content skipping.

Also fills in the previously-stubbed "consume a preloaded resource"
algorithm and the document's "map of preloaded resources", so that
<link rel="preload"> followed by a matching consumer deduplicates to
a single fetch.
2026-04-26 18:48:29 +02:00

50 lines
1.3 KiB
C++

/*
* Copyright (c) 2026, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <LibGC/Ptr.h>
#include <LibJS/Heap/Cell.h>
#include <LibURL/URL.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Parser/HTMLTokenizer.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/parsing.html#speculative-html-parser
class SpeculativeHTMLParser final : public JS::Cell {
GC_CELL(SpeculativeHTMLParser, JS::Cell);
GC_DECLARE_ALLOCATOR(SpeculativeHTMLParser);
public:
static GC::Ref<SpeculativeHTMLParser> create(JS::Realm&, GC::Ref<DOM::Document>, String pending_input, URL::URL base_url);
virtual ~SpeculativeHTMLParser() override;
void run();
void stop();
private:
SpeculativeHTMLParser(GC::Ref<DOM::Document>, String pending_input, URL::URL base_url);
virtual void visit_edges(JS::Cell::Visitor&) override;
void process_start_tag(HTMLToken const&);
void process_end_tag(HTMLToken const&);
GC::Ref<DOM::Document> m_document;
// m_input must precede m_tokenizer so that m_input.bytes_as_string_view() is valid when the
// tokenizer's constructor runs in our initializer list.
String m_input;
HTMLTokenizer m_tokenizer;
URL::URL m_base_url;
u32 m_template_depth { 0 };
u32 m_foreign_depth { 0 };
};
}