Skip to content

Truly independent web browser

3.5 KB
Raw Blame
/*
 * Copyright (c) 2023, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/HTMLDetailsElement.h>
#include <LibWeb/HTML/HTMLSummaryElement.h>

namespace Web::HTML {

GC_DEFINE_ALLOCATOR(HTMLSummaryElement);

// AD-HOC: The spec defines the association between a summary element and it's details element only for author defined
//         summary elements (i.e. the first summary element child of a details element) but we want the same behavior
//         to apply to the default summary - the spec doesn't specify how to do this so we piggy back on the existing
//         algorithms for author defined summary elements.
static GC::Ptr<HTMLDetailsElement> associated_details_element(HTMLSummaryElement& summary)
{
    if (auto parent = summary.parent_element()) {
        if (parent->is_html_details_element()) {
            if (parent->first_child_of_type<HTMLSummaryElement>() == &summary)
                return GC::Ptr { as<HTMLDetailsElement>(*parent) };
        }
    }

    if (auto* shadow_root = as_if<DOM::ShadowRoot>(summary.root()); shadow_root && shadow_root->is_user_agent_internal()) {
        auto* host = shadow_root->host();

        if (auto* details = as_if<HTMLDetailsElement>(host); details && details->is_default_summary(summary))
            return GC::Ptr { details };
    }

    return nullptr;
}

HTMLSummaryElement::HTMLSummaryElement(DOM::Document& document, DOM::QualifiedName qualified_name)
    : HTMLElement(document, move(qualified_name))
{
}

bool HTMLSummaryElement::has_activation_behavior() const
{
    return true;
}

void HTMLSummaryElement::activation_behavior(DOM::Event const&)
{
    // The activation behavior of summary elements is to run the following steps:

    // 1. If this summary element is not the summary for its parent details, then return.
    if (!is_summary_for_its_parent_details())
        return;

    // 2. Let parent be this summary element's parent.
    auto parent = associated_details_element(*this);

    // 3. If the open attribute is present on parent, then remove it. Otherwise, set parent's open attribute to the empty string.
    if (parent->has_attribute(HTML::AttributeNames::open))
        parent->remove_attribute(HTML::AttributeNames::open);
    else
        parent->set_attribute_value(HTML::AttributeNames::open, Utf16String {});
}

// https://html.spec.whatwg.org/multipage/interactive-elements.html#summary-for-its-parent-details
bool HTMLSummaryElement::is_summary_for_its_parent_details() const
{
    // A summary element is a summary for its parent details if the following algorithm returns true:

    // 1. If this summary element has no parent, then return false.
    if (!parent_element())
        return false;

    // 2. Let parent be this summary element's parent.
    // 3. If parent is not a details element, then return false.
    // 4. If parent's first summary element child is not this summary element, then return false.
    // 5. Return true.
    return associated_details_element(const_cast<HTMLSummaryElement&>(*this)) != nullptr;
}

// https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-summary-element
bool HTMLSummaryElement::is_focusable() const
{
    // summary elements that are the first summary element child of a details element
    return (Base::is_focusable() || is_summary_for_its_parent_details())
        && meets_focusable_area_rendering_requirements();
}

HTMLSummaryElement::~HTMLSummaryElement() = default;

}