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

Code before:
    // 1. Set el's result to result.
    m_result = move(result);

    // 2. If el's steps to run when the result is ready are not null, then run them.
    if (m_steps_to_run_when_the_result_is_ready)
        m_steps_to_run_when_the_result_is_ready();

    // 3. Set el's steps to run when the result is ready to null.
    m_steps_to_run_when_the_result_is_ready = nullptr;

    // 4. Set el's delaying the load event to false.
    m_document_load_event_delayer.clear();
}

void HTMLScriptElement::unmark_as_already_started(Badge<DOM::Range>)
{
    m_already_started = false;
}

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

// 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(

Review comment (suggestion):
The regression was due to losing the URL extended attribute from the IDL in f65dca1b5334bde716dc45a0c90659ebe36a7f22. Since that was originally added as a non standard attribute, this is now specified as ReflectURL in the HTML spec here: https://html.spec.whatwg.org/multipage/scripting.html#the-script-element:xattr-reflecturl

Ideally, the fix here would be just adding back that extended attribute. However, the IDL from the trusted types spec isn't updated there, they probably need to do an update. I am also not sure if our IDL generator supports that reflection since the trusted types type is no longer just a USVString

Code after:
    // 1. Set el's result to result.
    m_result = move(result);

    // 2. If el's steps to run when the result is ready are not null, then run them.
    if (m_steps_to_run_when_the_result_is_ready)
        m_steps_to_run_when_the_result_is_ready();

    // 3. Set el's steps to run when the result is ready to null.
    m_steps_to_run_when_the_result_is_ready = nullptr;

    // 4. Set el's delaying the load event to false.
    m_document_load_event_delayer.clear();
}

void HTMLScriptElement::unmark_as_already_started(Badge<DOM::Range>)
{
    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)