Skip to content

Truly independent web browser

LibWeb: Keep the document source as parkable source code

Andreas Kling3ea811e54dab
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index d4058070fea..37b89bd0052 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -3750,6 +3750,15 @@ GC::Ref<CSS::HighlightRegistry> Document::highlight_registry()
return *m_highlight_registry;
}
+void Document::set_source(Utf16String source)
+{
+ if (source.is_empty()) {
+ m_source = nullptr;
+ return;
+ }
+ m_source = JS::SourceCode::create({}, move(source));
+}
+
// https://html.spec.whatwg.org/multipage/obsolete.html#dom-document-clear
void Document::clear()
{
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 348eb3d9326..a49c0619ded 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -32,6 +32,7 @@
#include <LibGfx/Point.h>
#include <LibHTTP/Cache/CacheMode.h>
#include <LibJS/Forward.h>
+#include <LibJS/SourceCode.h>
#include <LibURL/Origin.h>
#include <LibURL/URL.h>
#include <LibUnicode/Forward.h>
@@ -733,8 +734,10 @@ public:
void capture_events();
void release_events();
- Utf16String const& source() const { return m_source; }
- void set_source(Utf16String source) { m_source = move(source); }
+ // NB: The source is kept as source code, which LibJS compresses while nothing reads it.
+ Utf16String source() const { return m_source ? m_source->code() : Utf16String {}; }
+ size_t source_length_in_code_units() const { return m_source ? m_source->length_in_code_units() : 0; }
+ void set_source(Utf16String);
HTML::EnvironmentSettingsObject& relevant_settings_object() const;
@@ -2063,7 +2066,7 @@ private:
bool m_has_been_destroyed { false };
bool m_observers_consider_document_fully_active { false };
- Utf16String m_source;
+ RefPtr<JS::SourceCode const> m_source;
GC::Ptr<Element> m_pending_parsing_blocking_script;
diff --git a/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cpp b/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cpp
index 163d16f6f7a..62af6a504e7 100644
--- a/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cpp
@@ -392,7 +392,8 @@ ErrorOr<bool> IncrementalDocumentParser::change_encoding(StringView new_encoding
builder.append(TRY(decoder->decode({}, TextCodec::Flush::Yes)));
auto redecoded_source = builder.to_string();
- auto current_source = m_processed_implied_eof ? m_document->source().utf16_view() : m_source.view();
+ auto document_source = m_processed_implied_eof ? m_document->source() : Utf16String {};
+ auto current_source = m_processed_implied_eof ? document_source.utf16_view() : m_source.view();
if (!redecoded_source.starts_with(current_source)
|| (m_processed_implied_eof && redecoded_source.length_in_code_units() != current_source.length_in_code_units())) {
// FIXME: A mismatch requires restarting the navigation with the new encoding, as described by step 6 of the
diff --git a/Libraries/LibWeb/HTML/Scripting/Script.cpp b/Libraries/LibWeb/HTML/Scripting/Script.cpp
index ffb0194c941..3a66bbfaca8 100644
--- a/Libraries/LibWeb/HTML/Scripting/Script.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Script.cpp
@@ -24,7 +24,7 @@ void register_javascript_source(Script& script, NonnullRefPtr<JS::SourceCode con
}
auto source_length = is_inline_source == ScriptRegistry::IsInlineSource::Yes
- ? document->source().length_in_code_units()
+ ? document->source_length_in_code_units()
: source_code->length_in_code_units();
auto const& registered_script = document->script_registry().register_javascript_source(
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index fd615fd61e4..1b017c6791e 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -2874,7 +2874,8 @@ Optional<Web::HTML::ScriptRegistry::Content> PageClient::devtools_source_content
if (!document)
return {};
- return document->script_registry().script_content(source_id.script_id, document->source().utf16_view());
+ auto document_source = document->source();
+ return document->script_registry().script_content(source_id.script_id, document_source.utf16_view());
}
Vector<WebView::DebuggerSourcePosition> PageClient::devtools_source_breakpoint_positions(Web::HTML::ScriptRegistry::Identifier const& source_id) const