diff --git a/layout/base/PresShell.cpp b/layout/base/PresShell.cpp index 71bddc4a21865..6e5f48320fb4c 100644 --- a/layout/base/PresShell.cpp +++ b/layout/base/PresShell.cpp @@ -3093,11 +3093,16 @@ nsresult PresShell::GoToAnchor(const nsAString& aAnchorName, // end node. // 3.4.2 While target is non-null and is not an element, set target to // target's parent. + // ------ + // Common closest ancestor is not suitable here, as it can scroll to positions + // where no text directive is visible. Instead, scroll to the start container + // of the text directive. + // see https://bugzil.la/1906895 and + // https://github.com/WICG/scroll-to-text-fragment/issues/259 Element* textFragmentTargetElement = [&aFirstTextDirective]() -> Element* { - nsINode* node = - aFirstTextDirective - ? aFirstTextDirective->GetClosestCommonInclusiveAncestor() - : nullptr; + nsINode* node = aFirstTextDirective + ? aFirstTextDirective->GetStartContainer() + : nullptr; while (node && !node->IsElement()) { node = node->GetParent(); } diff --git a/testing/web-platform/tests/scroll-to-text-fragment/scroll-to-text-fragment.html b/testing/web-platform/tests/scroll-to-text-fragment/scroll-to-text-fragment.html index 91f282f849f54..2dbd575bcbb95 100644 --- a/testing/web-platform/tests/scroll-to-text-fragment/scroll-to-text-fragment.html +++ b/testing/web-platform/tests/scroll-to-text-fragment/scroll-to-text-fragment.html @@ -234,6 +234,11 @@ fragment: '#:~:text=horizontally%20scrolled%20text', expect_position: 'horizontal-scroll', description: 'Text directive should horizontally scroll into view' + }, + { + fragment: '#:~:text=Element,This', + expect_position: 'element', + description: 'Text directive that spans a range larger than the viewport should scroll the start into view' } ];