The current draft specification only allows text matches to occur at word boundaries (see section §3.5.3). As clarified in example 12:
The substring "mountain range" is word bounded within the string "An impressive mountain range" but not within "An impressive mountain ranger".
As for the reason, the only hint in the spec is at the top of the section:
Limiting matching to word boundaries is one of the mitigations to limit cross-origin information leakage.
Given the various other mitigations against cross-origin information leakage (in section §3.4) that appear to rule out automated, repeated pattern searches, are there still scenarios where this is an important issue? And if so, would limiting text to word boundaries really make a significant difference?
Even if both questions would be answered with yes, may it be possible and desirable to somewhat separate the concerns of expressivity and information leakage — perhaps having one algorithm that defines what fragment is being pointed at, while another defines whether this is a permissible target to scroll to? This would give flexibility and leave browsers free to not apply the limitation in situations where it can determine that there is no risk of an attack (e.g. in documents without external resources).
Are there other reasons for imposing this limitation?
Downsides of word boundaries
I came up with at least a few reasons against this limitation, besides the obvious aspect of complicating the algorithm:
- Language specificity: As acknowledged in the spec, not every language/script has an equally clear a concept of word boundaries. Even if the
Intl.Segmenter will be standardised and make a definition easier, it could be asked if in every language the reasoning behind the word boundary limitation is equally valid (depends on what exactly is the reasoning is).
- The limitation inhibits many valid use cases; I imagine a web-based spell checker service may want to point you at an error in a word. Or in the many documents that are not prose but other strings, there could be various reasons to point at fragments of a string, e.g. a sequence inside a chromosome.
- Browsers and/or other applications could (should?) enable users to create links to selections, but the user experience for link creation would be confusing if a user can target some selections, but not others; or if alternatively the selection would be changed automagically to end at word boundaries.
- As most other selection/targeting/annotation softwares do not seem to limit selections this way, it could hamper conversions between formats (for example converting a highlight in a pdf file into a link to its equivalent html page). Also I imagine that other formats will some day consider adopting linking to text fragments, without the limitation because they have no issues of information leakage, and thereby create a subtle inconsistency that may puzzle both developers and users.
Drop word boundary limitation after prefix and before suffix?
Update: this has been done in #148
Even if the word boundary limitation would be retained for information leakage, I fail to see why one would require the prefix to end at a word boundary, or the suffix to start at a boundary. In case a prefix is given, I would expect only the concatenation prefix + textStart to be word bounded, rather than both individually, because the combination is what is being matched. And likewise for (textEnd ?? textStart) + suffix. This way, one could still link to e.g. a typo in a word by giving the rest of the word as its context (see the example in the demo I made: :~:text=poi-,i,-nt).
Using the prefix/suffix strings to allow arbitrary within-a-word selection, instead of only to disambiguate between multiple occurrences, would greatly increase the expressivity of the text fragment directive, and thereby remove the link creation UX issue as one can always generate a directive that describes the user’s selection.
Note that one could still choose to allow (just not require) whitespace after the prefix and before the suffix, as is currently the case. And it would be helpful to still allow prefix/suffix to be in the previous/next block element; as is required in another example in my demo (that selects a block element’s whole content).
…and/or between textStart and textEnd?
Yet another example in my demo shows a scenario where it would be helpful if one can point at a long uninterrupted string without having to quote the whole string. Even if boundaries around the match are still required, it could be considered to allow textStart to end and textEnd to begin without a boundary when both are present.
… or altogether?
(apologies for this lengthy polemic; among the many aspects of the spec that seem well thought-through, the reasoning behind this one just kept puzzling me while implementing it!)
The current draft specification only allows text matches to occur at word boundaries (see section §3.5.3). As clarified in example 12:
As for the reason, the only hint in the spec is at the top of the section:
Given the various other mitigations against cross-origin information leakage (in section §3.4) that appear to rule out automated, repeated pattern searches, are there still scenarios where this is an important issue? And if so, would limiting text to word boundaries really make a significant difference?
Even if both questions would be answered with yes, may it be possible and desirable to somewhat separate the concerns of expressivity and information leakage — perhaps having one algorithm that defines what fragment is being pointed at, while another defines whether this is a permissible target to scroll to? This would give flexibility and leave browsers free to not apply the limitation in situations where it can determine that there is no risk of an attack (e.g. in documents without external resources).
Are there other reasons for imposing this limitation?
Downsides of word boundaries
I came up with at least a few reasons against this limitation, besides the obvious aspect of complicating the algorithm:
Intl.Segmenterwill be standardised and make a definition easier, it could be asked if in every language the reasoning behind the word boundary limitation is equally valid (depends on what exactly is the reasoning is).Drop word boundary limitation after prefix and before suffix?
Update: this has been done in #148
Even if the word boundary limitation would be retained for information leakage, I fail to see why one would require the prefix to end at a word boundary, or the suffix to start at a boundary. In case a prefix is given, I would expect only the concatenation
prefix + textStartto be word bounded, rather than both individually, because the combination is what is being matched. And likewise for(textEnd ?? textStart) + suffix. This way, one could still link to e.g. a typo in a word by giving the rest of the word as its context (see the example in the demo I made::~:text=poi-,i,-nt).Using the prefix/suffix strings to allow arbitrary within-a-word selection, instead of only to disambiguate between multiple occurrences, would greatly increase the expressivity of the text fragment directive, and thereby remove the link creation UX issue as one can always generate a directive that describes the user’s selection.
Note that one could still choose to allow (just not require) whitespace after the prefix and before the suffix, as is currently the case. And it would be helpful to still allow prefix/suffix to be in the previous/next block element; as is required in another example in my demo (that selects a block element’s whole content).
…and/or between textStart and textEnd?
Yet another example in my demo shows a scenario where it would be helpful if one can point at a long uninterrupted string without having to quote the whole string. Even if boundaries around the match are still required, it could be considered to allow textStart to end and textEnd to begin without a boundary when both are present.
… or altogether?
(apologies for this lengthy polemic; among the many aspects of the spec that seem well thought-through, the reasoning behind this one just kept puzzling me while implementing it!)