LibWeb: Add textContent attribute of HTMLScriptElement for TrustedTypes

This commit is contained in:
Tete17 2025-08-06 14:13:09 +02:00 committed by Jelle Raaijmakers
parent f65dca1b53
commit 6b9c44390f
4 changed files with 42 additions and 10 deletions

View File

@ -678,6 +678,33 @@ WebIDL::ExceptionOr<void> HTMLScriptElement::set_src(TrustedTypes::TrustedScript
return {};
}
// https://w3c.github.io/trusted-types/dist/spec/#the-textContent-idl-attribute
Variant<GC::Root<TrustedTypes::TrustedScript>, Utf16String, Empty> HTMLScriptElement::text_content() const
{
// 1. Return the result of running get text content with this.
return descendant_text_content();
}
// https://w3c.github.io/trusted-types/dist/spec/#the-textContent-idl-attribute
WebIDL::ExceptionOr<void> HTMLScriptElement::set_text_content(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 textContent, and script.
auto const value = TRY(TrustedTypes::get_trusted_type_compliant_string(
TrustedTypes::TrustedTypeName::TrustedScript,
HTML::relevant_global_object(*this),
text,
TrustedTypes::InjectionSink::HTMLScriptElementtextContent,
TrustedTypes::Script.to_string()));
// 2. Set thiss script text value to value.
m_script_text = value;
// 3. Run set text content with this and value.
string_replace_all(value);
return {};
}
// https://html.spec.whatwg.org/multipage/scripting.html#dom-script-async
bool HTMLScriptElement::async() const
{

View File

@ -65,6 +65,9 @@ public:
TrustedTypes::TrustedScriptURLOrString src() const { return Utf16String::from_utf8(get_attribute_value(AttributeNames::src)); }
WebIDL::ExceptionOr<void> set_src(TrustedTypes::TrustedScriptURLOrString);
Variant<GC::Root<TrustedTypes::TrustedScript>, Utf16String, Empty> text_content() const;
WebIDL::ExceptionOr<void> set_text_content(TrustedTypes::TrustedScriptOrString);
[[nodiscard]] bool async() const;
void set_async(bool);

View File

@ -20,6 +20,7 @@ interface HTMLScriptElement : HTMLElement {
// https://www.w3.org/TR/trusted-types/#enforcement-in-scripts
[CEReactions] attribute (TrustedScript or Utf16DOMString) text;
[CEReactions] attribute (TrustedScriptURL or Utf16USVString) src;
[CEReactions] attribute (TrustedScript or Utf16DOMString)? textContent;
static boolean supports(DOMString type);

View File

@ -23,6 +23,7 @@ namespace Web::TrustedTypes {
__ENUMERATE_INJECTION_SINKS(HTMLIFrameElementsrcdoc, "HTMLIFrameElement srcdoc") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementsrc, "HTMLScriptElement src") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementtext, "HTMLScriptElement text") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementtextContent, "HTMLScriptElement textContent") \
__ENUMERATE_INJECTION_SINKS(Locationhref, "Location href") \
__ENUMERATE_INJECTION_SINKS(SVGScriptElementhref, "SVGScriptElement href") \
ENUMERATE_GLOBAL_EVENT_HANDLERS(EVENT_HANDLERS_INJECTION_SINKS) \