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

Code before:
    // 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;
}

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


Review comment (suggestion):
You can prevent doing two lookups by calling into `get_attribute` or `get_attribute_ns`, which both return `Optional<String>`.

Code after:
    // 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)
{
    // 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,