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

Code before:
    auto value = MUST(design_mode.to_lowercase());

    // 2. If value is "on" and this's design mode enabled is false, then:
    if (value == "on"sv && !m_design_mode_enabled) {
        // 1. Set this's design mode enabled to true.
        set_design_mode_enabled_state(true);
        // 2. Reset this's active range's start and end boundary points to be at the start of this.
        if (auto selection = get_selection()) {
            TRY(selection->collapse(this, 0));
            update_layout(UpdateLayoutReason::DocumentSetDesignMode);
        }
        // 3. Run the focusing steps for this's document element, if non-null.
        if (auto* document_element = this->document_element(); document_element)
            HTML::run_focusing_steps(document_element);
    }
    // 3. If value is "off", then set this's design mode enabled to false.
    else if (value == "off"sv) {
        set_design_mode_enabled_state(false);
    }
    return {};
}

static Element* retarget_from_ua_internal_shadow_root(Element* element)
{
    if (!element)
        return nullptr;
    while (auto shadow_root = element->containing_shadow_root()) {
        if (!shadow_root->is_user_agent_internal())
            break;
        element = shadow_root->host();
    }
    return element;
}

// https://drafts.csswg.org/cssom-view/#dom-document-elementfrompoint
Element const* Document::element_from_point(double x, double y)
{
    // 1. If either argument is negative, x is greater than the viewport width excluding the size of a rendered scroll
    //    bar (if any), or y is greater than the viewport height excluding the size of a rendered scroll bar (if any), or
    //    there is no viewport associated with the document, return null and terminate these steps.
    auto viewport_rect = this->viewport_rect();
    CSSPixelPoint position { x, y };
    // FIXME: This should account for the size of the scroll bar.
    if (x < 0 || y < 0 || position.x() > viewport_rect.width() || position.y() > viewport_rect.height())
        return nullptr;

    // Ensure the layout tree exists prior to hit testing.
    update_layout(UpdateLayoutReason::DocumentElementFromPoint);

    // 2. If there is a box in the viewport that would be a target for hit testing at coordinates x,y, when applying the transforms
    //    that apply to the descendants of the viewport, return the associated element and terminate these steps.

Review comment (suggestion):
Both callers do a null check first so `element` always has a value. 

Code after:
    auto value = MUST(design_mode.to_lowercase());

    // 2. If value is "on" and this's design mode enabled is false, then:
    if (value == "on"sv && !m_design_mode_enabled) {
        // 1. Set this's design mode enabled to true.
        set_design_mode_enabled_state(true);
        // 2. Reset this's active range's start and end boundary points to be at the start of this.
        if (auto selection = get_selection()) {
            TRY(selection->collapse(this, 0));
            update_layout(UpdateLayoutReason::DocumentSetDesignMode);
        }
        // 3. Run the focusing steps for this's document element, if non-null.
        if (auto* document_element = this->document_element(); document_element)
            HTML::run_focusing_steps(document_element);
    }
    // 3. If value is "off", then set this's design mode enabled to false.
    else if (value == "off"sv) {
        set_design_mode_enabled_state(false);
    }
    return {};
}

static Element* retarget_from_ua_internal_shadow_root(Element& element)
{
    auto* result = &element;
    while (auto shadow_root = result->containing_shadow_root()) {
        if (!shadow_root->is_user_agent_internal())
            break;
        result = shadow_root->host();
    }
    return result;
}

// https://drafts.csswg.org/cssom-view/#dom-document-elementfrompoint
Element const* Document::element_from_point(double x, double y)
{
    // 1. If either argument is negative, x is greater than the viewport width excluding the size of a rendered scroll
    //    bar (if any), or y is greater than the viewport height excluding the size of a rendered scroll bar (if any), or
    //    there is no viewport associated with the document, return null and terminate these steps.
    auto viewport_rect = this->viewport_rect();
    CSSPixelPoint position { x, y };
    // FIXME: This should account for the size of the scroll bar.
    if (x < 0 || y < 0 || position.x() > viewport_rect.width() || position.y() > viewport_rect.height())
        return nullptr;

    // Ensure the layout tree exists prior to hit testing.
    update_layout(UpdateLayoutReason::DocumentElementFromPoint);

    // 2. If there is a box in the viewport that would be a target for hit testing at coordinates x,y, when applying the transforms
    //    that apply to the descendants of the viewport, return the associated element and terminate these steps.
    GC::Ptr<Element> hit_element;