Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/HTML/HTMLScriptElement.cpp (C++)

Code before:
{
    m_already_started = false;
}

void HTMLScriptElement::unmark_as_parser_inserted(Badge<DOM::Range>)
{
    m_parser_document = nullptr;
}

// NOTE: Manual [ReflectURL] until IDL generator supports TrustedScriptURLOrString.
// https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes
TrustedTypes::TrustedScriptURLOrString HTMLScriptElement::src() const
{
    // 1. If contentAttributeValue is null, then return the empty string.
    if (!has_attribute(AttributeNames::src))
        return Utf16String {};

    auto raw = get_attribute_value(AttributeNames::src);

    // 2. Let urlString be the result of encoding-parsing-and-serializing a URL given
    //    contentAttributeValue, relative to element’s node document.
    auto url_string = document().encoding_parse_and_serialize_url(raw);

    // 3. If urlString is not failure, then return urlString.
    if (url_string.has_value())
        return Utf16String::from_utf8(*url_string);

    // Otherwise return the empty string.
    return Utf16String {};
}

// 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, this’s 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 this’s 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 {};
}


Review comment (suggestion):
You can use `::from_utf8_without_validation` here since the UTF-8 source (`encoding_parse_and_serialize_url`) should be trusted / validated already.

Code after:
{
    m_already_started = false;
}

void HTMLScriptElement::unmark_as_parser_inserted(Badge<DOM::Range>)
{
    m_parser_document = nullptr;
}

// https://github.com/w3c/trusted-types/blob/0f01aee35c3684e8cb20e7d83445ad65938f545d/spec/index.bs#L1341
TrustedTypes::TrustedScriptURLOrString HTMLScriptElement::src() const
{
    // 1. Let element be the result of running this's get the element.

    // 2. Let contentAttributeValue be the result of running this's get the content attribute.
    auto maybe_src = get_attribute(AttributeNames::src);

    // 3. If contentAttributeValue is null, then return the empty string.
    if (!maybe_src.has_value())
        return Utf16String {};

    auto const& raw = *maybe_src;

    // 4. Let urlString be the result of encoding-parsing-and-serializing a URL given contentAttributeValue, relative to element's node document.
    auto url_string = document().encoding_parse_and_serialize_url(raw);

    // 5. If urlString is not failure, then return urlString.
    if (url_string.has_value())
        return Utf16String::from_utf8_without_validation(*url_string);

    // 6. Return contentAttributeValue, converted to a scalar value string.
    return Utf16String {};
}

// 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, this’s 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 this’s 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);