diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index b5f1d02151..358e59e035 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -1796,6 +1797,64 @@ Element* Document::find_a_potential_indicated_element(DeprecatedString fragment) return nullptr; } +// https://html.spec.whatwg.org/multipage/browsing-the-web.html#scroll-to-the-fragment-identifier +void Document::scroll_to_the_fragment() +{ + // To scroll to the fragment given a Document document: + + // 1. If document's indicated part is null, then set document's target element to null. + auto indicated_part = determine_the_indicated_part(); + if (indicated_part.has() && indicated_part.get() == nullptr) { + set_target_element(nullptr); + } + + // 2. Otherwise, if document's indicated part is top of the document, then: + else if (indicated_part.has()) { + // 1. Set document's target element to null. + set_target_element(nullptr); + + // 2. Scroll to the beginning of the document for document. [CSSOMVIEW] + scroll_to_the_beginning_of_the_document(); + + // 3. Return. + return; + } + + // 3. Otherwise: + else { + // 1. Assert: document's indicated part is an element. + VERIFY(indicated_part.has()); + + // 2. Let target be document's indicated part. + auto target = indicated_part.get(); + + // 3. Set document's target element to target. + set_target_element(target); + + // FIXME: 4. Run the ancestor details revealing algorithm on target. + + // FIXME: 5. Run the ancestor hidden-until-found revealing algorithm on target. + + // 6. Scroll target into view, with behavior set to "auto", block set to "start", and inline set to "nearest". [CSSOMVIEW] + // FIXME: Do this properly! + (void)target->scroll_into_view(); + + // 7. Run the focusing steps for target, with the Document's viewport as the fallback target. + // FIXME: Pass the Document's viewport somehow. + HTML::run_focusing_steps(target); + + // FIXME: 8. Move the sequential focus navigation starting point to target. + } +} + +// 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 browsing_context = this->browsing_context()) + browsing_context->scroll_to({ 0, 0 }); +} + DeprecatedString Document::ready_state() const { switch (m_readiness) { diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index a25f43e4df..cac5a93d22 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -298,6 +298,9 @@ public: Element const* target_element() const { return m_target_element.ptr(); } void set_target_element(Element*); + void scroll_to_the_fragment(); + void scroll_to_the_beginning_of_the_document(); + bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; } JS::NonnullGCPtr appropriate_template_contents_owner_document(); diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp index 45c2302738..ce091da702 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -1407,11 +1407,8 @@ WebIDL::ExceptionOr BrowsingContext::traverse_the_history(size_t entry_ind } // 10. If entry's persisted user state is null, and its URL's fragment is non-null, then scroll to the fragment. - if (!entry->url.fragment().is_null()) { - // FIXME: Implement the full "scroll to the fragment" algorithm: - // https://html.spec.whatwg.org/multipage/browsing-the-web.html#scroll-to-the-fragment-identifier - scroll_to_anchor(entry->url.fragment()); - } + if (!entry->url.fragment().is_null()) + active_document()->scroll_to_the_fragment(); // 11. Set the current entry to entry. m_session_history_index = entry_index;