WICG / scroll-to-text-fragment

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

Remove / Rephrase "4.3 Determine if Fragment Id is needed" #244

Open jnjaeschke opened 9 months ago

jnjaeschke commented 9 months ago

From how I understand the mentioned section, the User Agent may arbitrarily provide a fallback element-id in case the document changed and the text directive is not found on the page anymore. However, there is an example which should explain which fallback id is appropriate and which isn't.

It is unclear to me how that should work. Given that a page may have element-ids for every HTML element (as an extreme case), how should the User Agent decide which id to use as a fallback? Should it be an element which is in the same viewport (then the link would depend on the resolution / size of the window at creation time)? Or should it be the next id when traversing backwards?

To me this imposes the risk that an id is picked which is misleading and unless specified in more detail I would propose to remove this section from the spec.

CC @zcorpan

bokand commented 9 months ago

The example given is meant as a warning that the UA shouldn't just blindly tack the text onto an existing fragment id.

Generating the fallback id automatically is indeed difficult to do well. This (non-normative) section is admittedly less prescriptive than event the surrounding sections. I think it boils down to: when generating a text directive, strip out the fragment-id unless you're sure it's what the user wants. (though it seems in Chrome we didn't follow our own advice...)

I think there's some potentially useful heuristics that might work reasonably. E.g. look at the first ancestor that's a section-like element (<h1>, <h2>, <h#>, <section>, <details>, etc.) and if it has an id, use that. Otherwise use no id. Maybe something along those lines would be better?

zcorpan commented 9 months ago

Always no id seems more predictable and avoids issues. We don't know how sites use the id attribute so it might not be suitable to include in the URL.

bokand commented 9 months ago

I think this is up to individual UAs to decide how to best do - but I'll reword this section as a reminder that the existing fragment should likely be removed when generating a new text directive link.

bokand commented 8 months ago

Was just reminded of #75 - it seems useful to start a search from an element-id if one is given, rather than only using it as a fallback. This allows the UA to generate a unique directive in the presence of highly repetitive text by including a nearby id (e.g. see https://crbug.com/1507811 for an example).

I'll try to address these issues together.