WICG / scroll-to-text-fragment

Proposal to allow specifying a text snippet in a URL fragment
Other
586 stars 43 forks source link

What should happen if the highlighted text changes? #224

Open bokand opened 1 year ago

bokand commented 1 year ago

e.g. Jake's example from https://github.com/WICG/scroll-to-text-fragment/issues/217#issuecomment-1496217320: https://output.jsbin.com/vohimek/3/quiet

Chrome's behavior of keeping parts of the highlight across a word is clearly poor but the spec doesn't specify any behavior here.

We could specify:

  1. If any of the highlighted text changes, remove the highlight.
  2. Keep the highlight if, and only if, the changed text still matches the directive (i.e. the wildcard text in the middle of a range-based directive). Likely to be computationally intensive as we'd have to re-perform the search on each DOM mutation. We could optimize it since we don't have to search the whole document but this is implementation intensive.
  3. Keep highlighting the same range

I slightly lean to option 1 since it means the highlighted text is always what was searched for but wouldn't be opposed to option 3.

annevk commented 1 year ago

What does 3 mean? Ideally this matches :target behavior I think, which I think argues for 2.

bokand commented 1 year ago

3 means that the highlight is created with a Range (node A offset a, node B offset b) and we continue to highlight that range even if the content within it changes, though we'd have to decide what to do at range boundaries (node A or B are removed or their content changed to make the offset invalid)

2 is the most intuitive and is consistent with :target but would require, for each highlight, to observe a (potentially-large) subtree for mutations (I guess option 1 also has this) and re-perform the text search each time. I don't have a great intuition for how expensive a mutation observer can be or how often mutations might fire but that makes me a bit worried. The text search could be performed over a limited subtree (e.g. the :target which is the first common ancestor of the starting and ending node of the highlight) and without context but that could still be a non-trivial tree walk.

annevk commented 1 year ago

Fair, @megangardner thoughts about this?

Presumably we have this problem with find-in-page and various other features as well. Perhaps consistency with them should be sought rather than :target. Hmm.