Repository navigation
Description
I would like to propose a new DOM event called “beforematch” (TAG review).
The beforematch event would be fired in response to find-in-page and ScrollToTextFragment on content which was hidden by a new CSS property, content-visibility: hidden-matchable.
When find-in-page or ScrollToTextFragment tries to scroll to a match which is within the hidden-matchable subtree, the beforematch event will fire on the element with the hidden-matchable style before the browser scrolls, allowing the page to change the style to reveal the content so the browser can scroll to it and show it to the user.
An example usage of this feature would be to search the content of collapsed sections of mobile wikipedia, which are not currently searchable.
Sketch of edits to spec
The find-in-page algorithm will be modified as shown in the github repo - the step where the active match is “scrolled into view” will be replaced with:
- At step 12 of the next update-the-rendering opportunity:
- If the DOM range representing the active match has been collapsed, start the search over again starting at this collapsed range.
- Let “matchable ancestor” be the nearest flat-tree ancestor element of the beginning of the DOM range representing the active match which has the
content-visibility: hidden-matchableproperty. - If the beforematch event has been disabled or there is no matchable ancestor, call scrollIntoView() on the active match and end the algorithm.
- Fire the beforematch event on the matchable ancestor.
- Cause a need for another update-the-rendering.
- At step 12 of the subsequent update-the-rendering opportunity:
- Disable the beforematch event for the remaining lifetime of the document and start the search over again starting at the end of the active match DOM range if any of the following conditions are true (explained in the privacy section):
- The DOM range representing the active match has been collapsed.
- The active match has the
content-visibility: hidden-matchableorcontent-visibility: hiddenproperties. - The active match has the
display: noneproperty. - The active match’s
visibilityproperty is notvisible.
- Call scrollIntoView() on the active match.
- Disable the beforematch event for the remaining lifetime of the document and start the search over again starting at the end of the active match DOM range if any of the following conditions are true (explained in the privacy section):
Security/privacy impact
Leaking information to the page about what the user is searching for with find-in-page is a big concern with this feature, but there is already some information which find-in-page necessarily exposes to the page. To limit the information exposed by this feature, we have designed two mitigations:
- The beforematch event will only be fired on text hidden with the
content-visibility: hidden-matchableCSS property. This prevents the page from attaching beforematch event listeners to all of the visible content in the page and snooping on what the user is searching for. - If the page fails to reveal the active match when the browser tries to scroll to the active match after firing the beforematch event, the beforematch event will be disabled for the remaining lifetime of the document. By “reveal,” I mean that the active match fulfills all of these requirements:
- The DOM range of the target match is not collapsed (meaning that the text was removed not from the dom).
- The
content-visibilityCSS property is nothiddenorhidden-matchable. - The
displayCSS property is notdisplay: none. - The
visibilityCSS property isvisibility: visible.
Open questions
Right now, I am proposing that the beforematch event is only fired on text with the content-visibility: hidden-matchable property in the ancestor chain. However, we have some other options:
- Stick with firing beforematch on
content-visibility: hidden-matchable.
Cons:- Implementing browsers might also need to implement some of the
content-visibility: hiddenproperties, depending on how the spec is phrased.
- Implementing browsers might also need to implement some of the
- Fire beforematch on
content-visibility: hidden-matchableandvisibility: hidden.
Cons:- There is already a lot of
visibility: hiddencontent out there on the web, and if we started firing beforematch on all of it, the page would be required to reveal it or else it would be locked out of using beforematch as described in mitigation 2 above. This means that many pages will get locked out of beforematch all the time by default, which would not be good.
- There is already a lot of
- Fire beforematch on
content-visibility: hiddenandvisibility: hiddenif they also have another new CSS property, let’s call itsearchable: true.
Cons:- Requires two CSS properties to get the desired functionality. In the
visibility: hiddencase, you’d also have to add even more CSS to get it to stop taking up space in the layout if you want it to function likecontent-visibility: hidden. - It would become a bit more complicated and ambiguous to choose an element to fire the beforematch event on. Do we fire it on the element with the hidden property, the element with the searchable property, or the closest one?
- Requires two CSS properties to get the desired functionality. In the
Activity
domenic commentedon Oct 8, 2020
FWIW, I'm very excited and supportive of this, and happy to help.
This looks pretty nice. You'll need to do a bit of extra work to track what iteration of "update-the-rendering" you're in; probably some kind of per-event-loop state variable with three values? Also, instead of calling scrollIntoView() directly, you'll want to call https://drafts.csswg.org/cssom-view/#scroll-an-element-into-view
tomayac commentedon Oct 12, 2020
Talking as the developer of the Link to Text Fragment extension (60K+ users), it's a top feature request of users to link to expandable items (e.g., accordions), so adding a platform capability that would allow sites to detect when and react to when a match is about to happen is highly desirable.
josepharhar commentedon Oct 14, 2020
<3
The way I implemented this was with a single cancellable callback variable for both of the two spots where we wait for the next update-the-rendering. This way, if another find-in-page request comes in, we will cancel the callback by replacing it with a new callback. The callback itself has the next method which will run (step 1 vs step 2) as well as a bunch of state to call the methods with, including the DOM range to scroll to as well as a flag to say whether the match originally had content-visibility:hidden-matchable in an ancestor when we first found it.
I see now that the spec steps I listed don't really cover this. What is a per-event-loop state variable? Are there any examples of this or anything else that sounds similar to the behavior I described?
Thanks! Would we be "calling" or "running" this step?
Awesome! Do you have any links to these feature requests?
alice commentedon Oct 15, 2020
Coming here from the TAG review.
We were curious about what the timing constraints are on developers for the requirement to show content with a match in order to continue having the feature enabled on the page.
The spec edits above make it look like every single beforematch event must result in content being shown, even if another event happens very quickly afterwards (say, because the user was typing a word or phrase and the find in page algorithm eagerly matched a partial word before they finished typing).
Will there be any throttling on the event being fired, to avoid jittering as find in page matches "L", then "Lo", then, "Lor", etc? Or have I misunderstood something?
domenic commentedon Oct 15, 2020
By this I meant declaring something in the spec like "Each event loop has an X, initially 0." But perhaps sticking closer to your implementation structure might be good. In which case it would be something like "Each event loop has a beforematch steps, which is null or a series of steps" plus more "Each event loop has..." for the other state you mention.
A similar example where the spec sets steps and later runs then would be https://html.spec.whatwg.org/#lazy-load-resumption-steps .
We use them interchangeably, but I think we'd usually write "running" in spec prose.
josepharhar commentedon Oct 15, 2020
Yes, every beforematch event handler must reveal the content in this case. With regards to timing, this has been an issue since before the privacy mitigations due to the fact that we still have to choose when to make the browser scroll. We added an async step (waiting for another update-the-rendering) in response to early developer feedback since their beforematch handler didn't synchronously reveal the content.
If the "L", "Lo", and "Lor" are all part of the same text then the beforematch event will only be fired on the first "L". If they are all parts of separate text, then a beforematch event would be fired on each one and each one would need to be revealed.
We don't currently have any plans to throttle the event, do you have an idea of how this could work? Could you elaborate?
alice commentedon Oct 16, 2020
That sounds less than ideal, then, if the user is typing "Lorem ipsum" but they only get a match on "L"? Have I missed something?
Basically wait for XX msec before firing the event, and cancel it if the query text has changed in the meantime. You could potentially combine that with a longer timeout for shorter text (because "L" will have more matches/noise than "Lo", etc.)
josepharhar commentedon Oct 16, 2020
Yeah so I suppose that if the user is searching for something, but in the process of typing in their query they get an active match for some text which isn't the final query they want, and that active match is hidden and gets revealed, that would be less than ideal:
Is this the case you're talking about?
This sounds similar to @kenchris's comment from the TAG review:
Safari does this for all usage of find-in-page, and the delay is pretty visible and I could imagine that some users might not like this behavior for find-in-page since it may seem less responsive.
If we added a delay just for the hidden-matchable/beforematch case and made it small enough that there isn't a negative impact on user experience, then I feel pretty open to the idea. I can see how having sections open while you're still typing isn't good UX. 👍
alice commentedon Oct 18, 2020
Yes, exactly that. Or even if "Lorem Ipsum" is also in a different
hidden-matchablesubtree - then you have "Lots of kittens" showing and potentially hiding, and then "Lorem ipsum" showing... a lot of rapid changes back to back.You could also explore having a grace period before locking the page out of the
beforematchfunctionality - i.e. the firstbeforematchevent starts a timer, and you have to show some content which had a match before the timer elapses (maybe with a "last chance" callback?). That would be a much more complicated API, though.Seems like it would be sensible to do some experiments to figure out what the best user experience is, in any case.
alice commentedon Oct 18, 2020
Another option might be to do something similar to what the Scroll To Text Fragment API did, and only match on word boundaries. Was that explored in the security/privacy review?
vmpstr commentedon Oct 20, 2020
This seems like the current behavior of find-in-page (more or less, in Chromium at least): each character typed will instantly find the next match that matches the whole query and scroll to it. The scroll can also be observed by script and it can react in any way it wants to after the scroll happens. The added feature with this proposal is that there's an interleaved event which is able to modify style/dom before the scroll occurs.
If I understand this correctly, then it seems like it would break existing find-in-page feature. Currently the match happens if any substring of the word (or multiple words) matches the query. If we only update the match on word boundaries, it seems like it would break use-cases. If we only fire beforematch on word boundaries, it seems like it would be hard to explain and also limit use-cases
I think the distinction here is that beforematch fires on hidden-matchable content to allow script to make the match visible; once visible, the find-in-page finds the text as before without the need for beforematch.
I'd just like to clarify my understanding of what happens with a specific example. Let me know if I'm correct in understanding this. Consider:
Consider the user taking the following steps:
The sequence of events is then the following.
beforematchevent is fired on#fooat the next rendering opportunity.content-visibility: hidden-matchablestyle.beforematchevents happen. 'Lor' is highlighted in 'Lorem' in response to user typingbeforematchevents happen,#fooremains visible since it no longer hashidden-matchablestyle. The 'Lord' text is scrolled into view and 'Lord' of 'Lord of the Rings' is highlighted in response to user inputjosepharhar commentedon Oct 21, 2020
I agree that trying to avoid locking the page out of beforematch is important. The current requestAnimationFrame delay in the algorithm should help prevent it from happening. I'm hesitant to have an actual timer, since pages shouldn't have an actual timed delay before revealing the content, right...? The current algorithm works for
setTimeout(/* reveal content */, 0)andrequestAnimationFrame(/* reveal content */)in the beforematch handler.Earlier today I was toying with the idea of checking to see if the match has been revealed and trying to scroll both before as well as after the requestAnimationFrame in order to avoid the situation where the beforematch handler reveals the content and then something else in the page decides to hide it again or remove it from the dom before the browser checks after the requestAnimationFrame.
Yeah, I think this could be limiting and counter-intuitive, and I don't understand how it would be a useful mitigation for beforematch. We are concerned with the page being able to build out what the user is searching for iteratively, in which case all matches would be for entire words.
After thinking about this some more, I think that "rapid changes" already describes the status quo: by having instant synchronous scrolling to whatever match you type into find-in-page today, chances are that the page is going to be jumping around as it scrolls to the match for whatever character you just typed in. If the page reveals content in response to each time that happens, is it really different from the status quo? Is it really that bad?
Yes, this is the behavior that I'm proposing.
alice commentedon Oct 23, 2020
Ahhh that's what I was missing. Thanks for the worked example showing this.
I think it has the potential to be more annoying than the status quo, because it's expanding parts of the page that you're not interested in (and thus triggering layout and content moving around) rather than just scrolling to them.
I'm imagining a mobile wikipedia page potentially expanding 2-3 sections before the one you're actually interested in. Then you try to scroll back up to where you were before you did a search, but there's all this extra content there.
Could we do an experiment to find out?
I'm speculating that pages may want to avoid the annoying behaviour described above by, say, waiting for either a pause in typing or for a minimum number of characters matched, before showing the match. Forcing pages to show content for every single
beforematchevent rules this out. But maybe I'm wrong anyway and nobody will want to do this.9 remaining items
[-]Proposal: beforematch event and content-visibility: hidden-matchable[/-][+]Proposal: beforematch event and hidden=until-matchable attribute[/+]annevk commentedon Feb 25, 2021
You'll also need to adjust
embed[hidden] { display: inline; height: 0; width: 0; }somehow, I suspect. Though perhaps that rule can be removed due to plugin removal?One thing to look out for is that we don't create another event with mutation-event timing as we do more of these user-action-mutates-the-tree features.
annevk commentedon Feb 25, 2021
In the CSS issue @smaug---- raised a question about the
hiddenIDL attribute. What's the idea there, add a new IDL attribute that can reflect all three states?hftf commentedon Feb 25, 2021
I wrote a comment in the CSSWG thread about how searchability is the desired new primitive. I think
hidden=until-foundwill conflate two distinct concepts, visibility and searchability, and will not scale to accommodating similar use cases for controlling searchability (such as w3c/csswg-drafts#3460).I'm also not convinced that searchability belongs in HTML rather than CSS. Wouldn't a CSS property have the same advantage of not requiring JS event handling to make the feature just work by default, and require the exact same amount of computation as an HTML attribute? (Or even less, since for HTML you have to re-add
hidden=until-foundmanually after each search, and thus its searchability-related semantics toggles on and off.) Please let me know if I understood it wrong.A CSS property would also allow concisely controlling the searchability of many elements at once using the power of selectors, while an HTML attribute would require repetitively adding an attribute on each element.
vmpstr commentedon Feb 25, 2021
Can you elaborate on how the "unhiding" would happen automatically in the CSS case? The problem is that the visibility (or lack thereof) can come from some class somewhere, and it's unclear to me how one would "make it visible" automatically. And even if this is possible, then how would one hide the content again?
As for the attribute version, the proposal is that it would come with some automatic hiding, but that would be specified as the UA stylesheet, so the author would be able to override the default behavior
hftf commentedon Feb 26, 2021
Sorry, I was oversimplifying and meant "attempt to unhide it" instead. In both the HTML attribute and CSS property cases, isn't it equally true that visibility can derive from elsewhere? The browser still needs to compute styles fully to decide whether the matched hidden content can actually be shown, right? And isn't it CSS that ultimately sets the invisibility of
[hidden]already (via the UA stylesheet, which you mention the user would be able to override)?I'm now also wondering, is it really that desirable for this feature to work by default (i.e. not require any JS)?
vmpstr commentedon Feb 26, 2021
Yes, you're right that the visibility can still come from a number of properties. The nice thing about an attribute is that it affects this one element (and only this one element). So, in response to a find-in-page match, the UA can remove the attribute. This accomplishes one of two things: either this clears sufficient style making content visible and thus the match can be shown to the user, or if the content is still hidden then it makes that content no longer searchable (since it no longer has
hidden=until-found).With CSS, the UA can remove the inline style since that only affects the one element, but there is no elegant way to remove other rules that would affect the element, such as
div > p { display: none; }in your example.As for the question of whether this should work without JS: it's definitely a nice-to-have. The previous proposal of content-visibility: hidden-matchable did not have any automatic ways of revealing content and relied exclusively on beforematch and JS to reveal content. With an attribute, we can unlock more cases that can work without script (e.g. the
<details>element would benefit from this naturally)domenic commentedon Feb 26, 2021
This plan looks good to me. To summarize and reiterate the tricky parts:
Changing a boolean attribute into a tri-state attribute will be tricky.
You'd change it to be an enumerated attribute with three states: e.g. true, until found, and false. The invalid value default would be true and the missing value default would be false. The attribute's keywords would be the empty string and "until-found", with the empty string mapping to the true state and "until-found" mapping to the until found state. We could also add a "false" keyword mapping to the false state, which would be more consistent with some of the other similar attributes, but might cause compat problems.
Speaking of other similar attributes, consistency is totally lost. We have: draggable (true/false + auto); translate (yes/no); autocomplete (on/off); contenteditable (true/false + inherit); spellcheck (true/false + default); autocapitalize (on = sentences/off = none/words/characters). Also a lot of ARIA attributes which use true/false. I picked true/false since it seems slightly more prevalent but I don't have a strong opinion.
IDL reflection gets impacted. I see two paths:
Double reflection: leave
element.hiddenas a boolean with the same logic as now (i.e., any value, includinguntil-hidden, always returns true). Add some new property, e.g.el.hiddenState, which does the enumerated reflection. In this case you'd want to modify my above enumerated attribute by adding a "true" keyword mapping to the true state and saying that one is canonical; this has no impact on compat but it ensures thatelement.setAttribute("hidden", "asdf"); element.hiddenStatereturns"true"not"".Custom reflection: leave
element.hiddenas a boolean with the same logic as now (i.e., any value, includinguntil-hidden, always returns true). Do not add a new property.This is similar to how
element.draggable,element.contentEditable, andelement.spellcheckwork; when you're in their third state, they return whatever is "really" happening. Forhidden=""that is pretty straightforward though: since the browser removes thehidden=""attribute when finding the element, ifhidden="until-found"is on the element, then the element is still hidden, soelement.hiddenshould return true.Event timing. Be sure that the event is on a queued task. In particular I believe the case @annevk is concerned about is something like:
The user types "foo" in their search bar. No matches.
The page author does
someElement.textContent = "foo".This should remove the
hidden=""attribute, and fire the event, asynchronously---not synchronously---from thetextContentsetter.[-]Proposal: beforematch event and hidden=until-matchable attribute[/-][+]Proposal: beforematch event and hidden=until-found attribute[/+]josepharhar commentedon Jan 7, 2022
I have an updated explainer here: https://github.com/WICG/display-locking/blob/main/explainers/hidden-content-explainer.md
I opened a PR here: #7475
josepharhar commentedon Jan 7, 2022
I don't fully understand this concern if we are just talking about the attribute itself and not the reflected
element.hiddenIDL.If the attribute isn't present, the element isn't hidden, like it is today.
If the attribute is
'until-found', then do our new logic.If the attribute is anything else, then make it hidden like it would be today.
So there's no way we can support
element.hidden = 'until-found'?I'm inclined to choose this option, but maybe if people really want to avoid
element.setAttribute()then adding another property would be helpful.domenic commentedon Jan 10, 2022
I think for the content attribute there's no concern. I was just mentioning that the spec prose will be tricky. I haven't looked at the PR yet to see how that went; will comment over there :).
I guess we could; we would just abandon the idea of
hiddenusing the spec's existing reflection infrastructure and write custom getter steps and setter steps. That's probably fine though.One of the big benefits of adding properties instead of forcing the use of setAttribute() is that some frameworks, like React, are property-centric.
Add hidden=until-found and beforematch
Add hidden=until-found and beforematch