WICG / scroll-to-text-fragment

Proposal to allow specifying a text snippet in a URL fragment
Other
589 stars 42 forks source link

Scroll to the start node of the first text directive instead of its common ancestor #259

Open jnjaeschke opened 3 months ago

jnjaeschke commented 3 months ago

If a text directive should be scrolled to, it should be visible in the center of the view. However, scrolling the common ancestor of a range into the center of the view is not necessarily scrolling the range into view.

Example:

<html>
  <style>
    .space {
      margin-top: 200vh;
    }
  </style>
  <body>
    <p class="space">Test</p>
    <p class="space">Test2</p>
  </body>
</html>

Text Directive: :~:text=Test,Test2

An implementation that follows the spec would scroll in between the two <p>, so that neither Test nor Test2 would be visible. This is confusing for users. Therefore, my proposal is to change the spec so that the start container of the range is visible at the center of the view instead of the common ancestor.

Chrome already follows this proposal and scrolls Test into the center of the view. Safari and Gecko (as of now) follow the spec. I'm planning to change the implementation in Gecko to align with Chrome.

/cc @zcorpan

bokand commented 3 months ago

Yeah, I agree that makes more sense. The intent here was that the first common ancestor is used to set the :target pseudo class since we can only set it on one element (I think?). But scrolling to it seems like a bug - we could separate the element used for :target and scroll the start container as you suggest.

annevk commented 3 months ago

cc @megangardner