I wish there was a (simpler) way to highlight text in inputs
When I was building the search engine for my blog, one feature I wanted to implement was syntax highlighting within the search input field. I wanted special operators (i.e. has:noalt, which shows posts that contain one or more images without alt text) to have a different background colour indicating that a given segment of text had some semantic meaning.
Here is an example of the GitHub search feature which has semantic syntax highlighting:

In the above image, the "capjamesg/indieweb-etherpad-archiver-v2" text is set in a light blue font; the text background is in a darker blue. This text is highlighted because it follows "repo:". This part of the query means that I want to scope my search to focus on a specific repository, "capjamesg/indieweb-etherpad-archiver-v2".
By highlighting the text that is part of the search query, I have a visual indication that what I have typed has some special meaning to the software.
Regex101 has a similar semantic syntax highlighting feature in the regular expression form field on their website:

Various characters in the above screenshot appear with coloured backgrounds; each one has semantic meaning.
The status quo
There doesn’t appear to be a semantic way to implement text highlighting in a native input tag. GitHub uses an input tag as the text and a lot of other code; a cursory read doesn’t make it obvious exactly what is going on. Regex101 uses a contenteditable div and gives every character in it a span tag. Both of these involve more code than I would like, all of which I would have to maintain.
I also explored an implementation of an input field that involved clipped background gradients, but there turned out to be many limits: maintaining state while scrolling, glitches in positioning the background, the requirement to use a monospaced font for the input so I can use ch to estimate where characters are in the input field.
What I would like
In my dream browser implementation, I would like to be able to apply the following styles:
- Text colour
- Background colour
I tried to use the CSS Custom Highlight API on an input field but it didn’t work. It feels like this API could be a stepping stone to highlighting text in an input field?
For my use case, which is to style a text input field, I would like to be able to specify the character indices at which a highlight should start and end. I can then use JavaScript with my own grammar parser to determine what character indices should be highlighted, and in what colour (I may want to use one colour for filter keywords, for example, and another for sort keywords).
Security
I suspect there are security considerations for this idea: what if the input field has white text that hides from a user what is in the form field? A malicious site could make it seem like there is no text in a form field visibly even if there is such text. With that said, I wanted to write down my thoughts to start a discussion.