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

Code before:
{
    // FIXME: According to the spec we should only scroll here if document has no parser or parsing has stopped.
    //        It should be ok to remove this after we implement navigation events and scrolling will happen in
    //        "process scroll behavior".
    //  To try to scroll to the fragment for a Document document, perform the following steps in parallel:
    //  1. Wait for an implementation-defined amount of time. (This is intended to allow the user agent to
    //     optimize the user experience in the face of performance concerns.)
    //  2. Queue a global task on the navigation and traversal task source given document's relevant global
    //     object to run these steps:
    //      1. If document has no parser, or its parser has stopped parsing, or the user agent has reason to
    //         believe the user is no longer interested in scrolling to the fragment, then abort these steps.
    //      2. Scroll to the fragment given document.
    //      3. If document's indicated part is still null, then try to scroll to the fragment for document.

    scroll_to_the_fragment();
}

// https://drafts.csswg.org/cssom-view-1/#scroll-to-the-beginning-of-the-document
void Document::scroll_to_the_beginning_of_the_document()
{
    // FIXME: Actually implement this algorithm
    if (auto navigable = this->navigable())
        navigable->perform_scroll_of_viewport({ 0, 0 });
}

StringView Document::ready_state() const
{
    switch (m_readiness) {
    case HTML::DocumentReadyState::Loading:
        return "loading"sv;
    case HTML::DocumentReadyState::Interactive:
        return "interactive"sv;
    case HTML::DocumentReadyState::Complete:
        return "complete"sv;
    }
    VERIFY_NOT_REACHED();
}

// https://html.spec.whatwg.org/multipage/dom.html#update-the-current-document-readiness
void Document::update_readiness(HTML::DocumentReadyState readiness_value)
{
    // 1. If document's current document readiness equals readinessValue, then return.
    if (m_readiness == readiness_value)
        return;

    // 2. Set document's current document readiness to readinessValue.
    m_readiness = readiness_value;

    // 3. If document is associated with an HTML parser, then:
    if (m_parser) {
        // 1. Let now be the current high resolution time given document's relevant global object.

Review comment (bug):
This is supposed to scroll the viewport, not its scrolling box. I did try that, and then tried implementing the algorithm properly, but it was causing test-web to crash or hang internally (even when guarding against a null navigable). I did raise an issue on the spec (https://github.com/w3c/csswg-drafts/issues/13301) because the way it calls the algorithm doesn't match up, so I'll probably see if that gets resolved before delving into scroll-land again.

Code after:

// https://html.spec.whatwg.org/multipage/browsing-the-web.html#try-to-scroll-to-the-fragment
void Document::try_to_scroll_to_the_fragment()
{
    // FIXME: According to the spec we should only scroll here if document has no parser or parsing has stopped.
    //        It should be ok to remove this after we implement navigation events and scrolling will happen in
    //        "process scroll behavior".
    //  To try to scroll to the fragment for a Document document, perform the following steps in parallel:
    //  1. Wait for an implementation-defined amount of time. (This is intended to allow the user agent to
    //     optimize the user experience in the face of performance concerns.)
    //  2. Queue a global task on the navigation and traversal task source given document's relevant global
    //     object to run these steps:
    //      1. If document has no parser, or its parser has stopped parsing, or the user agent has reason to
    //         believe the user is no longer interested in scrolling to the fragment, then abort these steps.
    //      2. Scroll to the fragment given document.
    //      3. If document's indicated part is still null, then try to scroll to the fragment for document.

    scroll_to_the_fragment();
}

// https://drafts.csswg.org/cssom-view-1/#scroll-to-the-beginning-of-the-document
void Document::scroll_to_the_beginning_of_the_document()
{
    // FIXME: Actually implement this algorithm
    if (auto navigable = this->navigable())
        navigable->perform_scroll_of_viewport_scrolling_box({ 0, 0 });
}

StringView Document::ready_state() const
{
    switch (m_readiness) {
    case HTML::DocumentReadyState::Loading:
        return "loading"sv;
    case HTML::DocumentReadyState::Interactive:
        return "interactive"sv;
    case HTML::DocumentReadyState::Complete:
        return "complete"sv;
    }
    VERIFY_NOT_REACHED();
}

// https://html.spec.whatwg.org/multipage/dom.html#update-the-current-document-readiness
void Document::update_readiness(HTML::DocumentReadyState readiness_value)
{
    // 1. If document's current document readiness equals readinessValue, then return.
    if (m_readiness == readiness_value)
        return;

    // 2. Set document's current document readiness to readinessValue.
    m_readiness = readiness_value;