Re: [csswg-drafts] [css-pseudo-4] Proposal: Highlight API (#4307)

> > I'm a little worried about the ergonomics of the API for adding a range to a particular named group when the caller is not sure if that group already exists.
> 
> Yeah, it'd be:
> 
> ```js
> if(!CSS.highlights.has("foo")) {
>   CSS.highlights.set("foo", new HighlightRangeGroup);
> }
> CSS.highlights.get("foo").add(myRange);
> ```
> 
> This is assuming that the HighlightRangeGroup is live and immediately responds to changes. Depending on semantics, it might be more like TypedOM and require you to re-set it before it sees the change, in which case the code could look like:
> 
> ```js
> CSS.highlights.set("foo",
>   (CSS.highlights.get("foo") || new HighlightRangeGroup)
>   .add(anotherRange));
> ```
> 
> So yeah, a little painful.
> 
> That said, in practice, I'm not sure why a page would run into this situation. If they're doing highlighting, presumably they'd create and register the highlight group sometime early in the page lifecycle, and just hold onto it and modify the set as they need to change things?

The HighlightsMap is a [maplike](https://heycam.github.io/webidl/#idl-maplike) object, so I would expect it to behave just like the [JS Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) in this regard. So this should work:
```
if(!CSS.highlights.has("foo")) {
  CSS.highlights.set("foo", new HighlightRangeGroup);
}
CSS.highlights.get("foo").add(myRange);
```

Here's an example of how this may be used: A spellchecking extension like Grammarly creates a highlight group for its spellcheck highlights when it first parses the page, and adds the group to the CSS.highlights map. (The extension may call .has to ensure there are no name conflicts before calling .set on the highlights map.) The extension will hold onto the group's name or the group itself. Subsequently, as the user types, a new misspelling is created. In response, the extension will create a range to surround the new spelling error and add the range to the group, either directly if it was holding onto the group itself or via `CSS.highlight.get(...).add(...)` if it was holding onto the name.

-- 
GitHub Notification of comment by sanketj
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/4307#issuecomment-531691052 using your GitHub account

Received on Monday, 16 September 2019 08:48:45 UTC