Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/DOM/Element.cpp (C++)

Code before:

    // - a fieldset element that is a disabled fieldset
    if (is<HTML::HTMLFieldSetElement>(this))
        return static_cast<HTML::HTMLFieldSetElement const&>(*this).is_disabled();

    // FIXME: - a form-associated custom element that is disabled
    return false;
}

// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#fragment-parsing-algorithm-steps
WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> Element::parse_fragment(StringView markup)
{
    // 1. Let algorithm be the HTML fragment parsing algorithm.
    auto algorithm = HTML::HTMLParser::parse_html_fragment;

    // FIXME: 2. If context's node document is an XML document, then set algorithm to the XML fragment parsing algorithm.
    if (document().is_xml_document()) {
        dbgln("FIXME: Handle fragment parsing of XML documents");
    }

    // 3. Let newChildren be the result of invoking algorithm given context and markup.
    auto new_children_result = algorithm(*this, markup, HTML::HTMLParser::AllowDeclarativeShadowRoots::No);
    if (new_children_result.is_exception())
        return new_children_result.exception();

    auto new_children = new_children_result.release_value();

    // 4. Let fragment be a new DocumentFragment whose node document is context's node document.
    auto fragment = realm().create<DOM::DocumentFragment>(document());

    // 5. For each node of newChildren, in tree order: append node to fragment.
    for (auto& child : new_children)
        TRY(fragment->append_child(*child));

    // 6. Return fragment.
    return fragment;
}

// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-outerhtml
WebIDL::ExceptionOr<String> Element::outer_html() const
{
    return serialize_fragment(HTML::RequireWellFormed::Yes, FragmentSerializationMode::Outer);
}

// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-outerhtml
WebIDL::ExceptionOr<void> Element::set_outer_html(String const& value)
{
    // 1. FIXME: Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, the given value, "Element outerHTML", and "script".

    // 2. Let parent be this's parent.
    auto* parent = this->parent();

Review comment (suggestion):
Does `TRY()` not work here? eg,

```c++
auto new_children = TRY(algorithm(*this, markup, HTML::HTMLParser::AllowDeclarativeShadowRoots::No));
```

Code after:
}

// https://html.spec.whatwg.org/multipage/semantics-other.html#concept-element-disabled
bool Element::is_actually_disabled() const
{
    // An element is said to be actually disabled if it is one of the following:
    // - a button element that is disabled
    // - an input element that is disabled
    // - a select element that is disabled
    // - a textarea element that is disabled
    if (is<HTML::HTMLButtonElement>(this) || is<HTML::HTMLInputElement>(this) || is<HTML::HTMLSelectElement>(this) || is<HTML::HTMLTextAreaElement>(this)) {
        auto const& form_associated_element = as<HTML::FormAssociatedElement>(*this);
        return !form_associated_element.enabled();
    }

    // - an optgroup element that has a disabled attribute
    if (is<HTML::HTMLOptGroupElement>(this))
        return has_attribute(HTML::AttributeNames::disabled);

    // - an option element that is disabled
    if (is<HTML::HTMLOptionElement>(this))
        return static_cast<HTML::HTMLOptionElement const&>(*this).disabled();

    // - a fieldset element that is a disabled fieldset
    if (is<HTML::HTMLFieldSetElement>(this))
        return static_cast<HTML::HTMLFieldSetElement const&>(*this).is_disabled();

    // FIXME: - a form-associated custom element that is disabled
    return false;
}

// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#fragment-parsing-algorithm-steps
WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> Element::parse_fragment(StringView markup)
{
    // 1. Let algorithm be the HTML fragment parsing algorithm.
    auto algorithm = HTML::HTMLParser::parse_html_fragment;

    // 2. If context's node document is an XML document, then set algorithm to the XML fragment parsing algorithm.
    if (document().is_xml_document()) {
        algorithm = XMLFragmentParser::parse_xml_fragment;
    }

    // 3. Let newChildren be the result of invoking algorithm given context and markup.
    auto new_children = TRY(algorithm(*this, markup, HTML::HTMLParser::AllowDeclarativeShadowRoots::No));

    // 4. Let fragment be a new DocumentFragment whose node document is context's node document.
    auto fragment = realm().create<DOM::DocumentFragment>(document());

    // 5. For each node of newChildren, in tree order: append node to fragment.
    for (auto& child : new_children)
        TRY(fragment->append_child(*child));