LibWeb: Extend set_text of HTMLScriptElement to accommodate TrustedTypes

This commit is contained in:
Tete17 2025-08-05 21:58:13 +02:00 committed by Jelle Raaijmakers
parent b4df857a57
commit 664a3b536a
Notes: github-actions[bot] 2025-09-16 08:59:04 +00:00
7 changed files with 46 additions and 3 deletions

View file

@ -24,6 +24,8 @@
#include <LibWeb/Infra/CharacterTypes.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
namespace Web::HTML {
@ -639,6 +641,26 @@ void HTMLScriptElement::unmark_as_parser_inserted(Badge<DOM::Range>)
m_parser_document = nullptr;
}
// https://www.w3.org/TR/trusted-types/#the-text-idl-attribute
WebIDL::ExceptionOr<void> HTMLScriptElement::set_text(TrustedTypes::TrustedScriptOrString text)
{
// 1. Let value be the result of calling Get Trusted Type compliant string with
// TrustedScript, thiss relevant global object, the given value, HTMLScriptElement text, and script.
auto const value = TRY(get_trusted_type_compliant_string(
TrustedTypes::TrustedTypeName::TrustedScript,
HTML::relevant_global_object(*this),
text,
TrustedTypes::InjectionSink::HTMLScriptElementtext,
TrustedTypes::Script.to_string()));
// 2. Set thiss script text value to the given value.
m_script_text = value;
// 3. String replace all with the given value within this.
string_replace_all(value);
return {};
}
// https://html.spec.whatwg.org/multipage/scripting.html#dom-script-async
bool HTMLScriptElement::async() const
{

View file

@ -13,6 +13,7 @@
#include <LibWeb/HTML/Scripting/ImportMapParseResult.h>
#include <LibWeb/HTML/Scripting/Script.h>
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
namespace Web::HTML {
@ -57,8 +58,8 @@ public:
void unmark_as_already_started(Badge<DOM::Range>);
void unmark_as_parser_inserted(Badge<DOM::Range>);
Utf16String text() { return child_text_content(); }
void set_text(Utf16String const& text) { string_replace_all(text); }
TrustedTypes::TrustedScriptOrString text() const { return child_text_content(); }
WebIDL::ExceptionOr<void> set_text(TrustedTypes::TrustedScriptOrString);
[[nodiscard]] bool async() const;
void set_async(bool);
@ -142,6 +143,9 @@ private:
Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;
size_t m_source_line_number { 1 };
// https://www.w3.org/TR/trusted-types/#htmlscriptelement-script-text
Utf16String m_script_text;
};
}

View file

@ -18,7 +18,8 @@ interface HTMLScriptElement : HTMLElement {
[CEReactions, Reflect] attribute DOMString integrity;
[CEReactions, Enumerated=FetchPriorityAttribute, Reflect=fetchpriority] attribute DOMString fetchPriority;
[CEReactions] attribute Utf16DOMString text;
// https://www.w3.org/TR/trusted-types/#enforcement-in-scripts
[CEReactions] attribute (TrustedScript or Utf16DOMString) text;
static boolean supports(DOMString type);