Truly independent web browser
LibWeb: Set the script text of scripts parsed in the fragment case
Andreas Klingc361274b1e9f
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cppindex 6f21a06334a..8b6d23f6504 100644--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp@@ -201,6 +201,7 @@ extern "C" size_t ladybird_html_parser_template_content(size_t);extern "C" size_t ladybird_html_parser_attach_declarative_shadow_root(size_t, RustFfiHtmlShadowRootMode, RustFfiHtmlSlotAssignmentMode, bool, bool, bool, bool);extern "C" void ladybird_html_parser_set_template_content(size_t, size_t);extern "C" bool ladybird_html_parser_is_shadow_host(size_t);+extern "C" void ladybird_html_parser_set_script_text_from_parser(size_t);HTMLParser::HTMLParser(DOM::Document& document, ParserScriptingMode scripting_mode, StringView input, StringView encoding, EncodingConfidence encoding_confidence): m_tokenizer(decode_html_parser_input(input, encoding))@@ -3130,4 +3131,12 @@ extern "C" bool ladybird_html_parser_is_shadow_host(size_t node)return element && element->is_shadow_host();}+// https://w3c.github.io/trusted-types/dist/spec/#setting-slot-values-from-parser+extern "C" void ladybird_html_parser_set_script_text_from_parser(size_t element)+{+ // Set script's script text value to its child text content.+ auto& script = as<HTMLScriptElement>(node_from_html_parser_ffi(element));+ script.set_string_text(script.child_text_content());+}+}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rsindex 9d8e86a5105..a5d299f03fd 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs@@ -169,6 +169,7 @@ unsafe extern "C" {) -> usize;fn ladybird_html_parser_set_template_content(element: usize, content: usize);fn ladybird_html_parser_is_shadow_host(node: usize) -> bool;+ fn ladybird_html_parser_set_script_text_from_parser(element: usize);}/// Opaque handle for the Rust HTML parser, passed across the FFI boundary.@@ -2823,6 +2824,10 @@ impl TreeBuilder {// The Fragment scripting mode treats parser-inserted scripts as if they were not parser-inserted, allowing,// for example, executing scripts when applying a fragment created by createContextualFragment().if self.parsing_fragment {+ // https://w3c.github.io/trusted-types/dist/spec/#setting-slot-values-from-parser+ // NB: The parser does not prepare scripts in the fragment case, but their script text value still+ // has to be set to their child text content, so that they can run once inserted into a document.+ unsafe { ladybird_html_parser_set_script_text_from_parser(script) };return;}self.pending_script = Some(script);diff --git a/Tests/LibWeb/Text/expected/HTML/createContextualFragment-script-with-trusted-types.txt b/Tests/LibWeb/Text/expected/HTML/createContextualFragment-script-with-trusted-types.txtnew file mode 100644index 00000000000..b730908e7e8--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/createContextualFragment-script-with-trusted-types.txt@@ -0,0 +1 @@+Script ran: truediff --git a/Tests/LibWeb/Text/input/HTML/createContextualFragment-script-with-trusted-types.html b/Tests/LibWeb/Text/input/HTML/createContextualFragment-script-with-trusted-types.htmlnew file mode 100644index 00000000000..de282bc8f5f--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/createContextualFragment-script-with-trusted-types.html@@ -0,0 +1,16 @@+<!DOCTYPE html>+<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">+<script src="../include.js"></script>+<div id="target"></div>+<script>+ test(() => {+ const policy = trustedTypes.createPolicy("policy", { createHTML: s => s });+ window.didRun = false;+ const target = document.getElementById("target");+ const range = document.createRange();+ range.selectNodeContents(target);+ const fragment = range.createContextualFragment(policy.createHTML("<script>window.didRun = true;<\/script>"));+ target.appendChild(fragment);+ println(`Script ran: ${window.didRun}`);+ });+</script>