whatwg / whatwg/html

<datalist> behavior(s) are inconsistent and unhelpful when options are prefiltered

Open
#9,986 3 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

addition/proposal needs implementer interest topic: forms
Dominant language
HTML
Stars
9.4k
Forks
3.2k
PR merge metrics
PR metrics pending

Description

What is the issue with the HTML Standard?

[First, my apologies for clumping what is probably a handful of complaints about browser-specific issues into a single issue here, but I believe the underlying issue is that the spec here isn't very clear on how datalist elements are actually supposed to work. Or, if it is, the behavior is less-than-helpful. More than anything, I'm hoping someone here can suggest the best way to proceed with this.]

One of the more common use cases for datalist is to provide users typing into an <input type="text" /> element a list of options pulled from a search API. For example, I have a project (npmgraph.js.org) that uses this NPM search endpoint to construct a DOM structure like this:

<input type="text" id="search-field" list="search-results" value="mi">

<datalist id="search-results">
  <option value="mi">The simplest and most obvious way to use multiple inheritance in Javascript that I could think of.</option>
  <option value="confiance-test">Test de fiabilidad y contenido estadistico</option>
  <option value="@activity-design/mi-design">组件库</option>
  <option value="@cheap-glitch/mi-cron">A microscopic parser for standard cron expressions.</option>
</datalist>

CodePen example demonstrating this markup.

Expected Behavior

In the case described above, the options in datalist come from an authoritative search API - i.e. ALL of the options are known to match the, "mi" string the user has entered into the input field. For that reason, we expect all options to be presented to the user. Something like this:

CleanShot 2023-12-09 at 08 48 44@2x

IMPORTANT: The 2nd option, "confiance-test" does not contain the input text value ("mi") but should still be included.

Actual Behavior

No production browsers handle this case well. In all cases, one or more options are omitted, presumably on the basis that the input text, "mi", isn't contained in the option 'value' (or inner-content, depending on implementation). While this isn't all that surprising, it is not desirable. Again, the options here represent a known set of results from an API that has taken care applying the desired search criteria.

Browser Screenshot Comment
Edge CleanShot 2023-12-09 at 08 48 44@2x Omits non-matching options
Firefox CleanShot 2023-12-09 at 08 48 44@2x Omits non-matching options. Does not display inner-content
Safari CleanShot 2023-12-09 at 08 48 44@2x Omits non-matching options
Chrome CleanShot 2023-12-09 at 08 48 44@2x Omits non-matching options
Safari (iOS) CleanShot 2023-12-09 at 08 48 44@2x Omits non-matching options. Does not display inner-content.
Chrome (Android) CleanShot 2023-12-09 at 08 48 44@2x Only shows first option?

Proposal(s)

First and foremost, it would be nice if there were a way to specify that the contents of the datalist were canonical. I.e. that the client should not attempt to filter the options in any way and, instead, just display as many as is feasible. Some possible ways of implementing this:

<!-- "canonical" attribute" -->
<input type="text" canonical list="search-results" />

<!-- "unfiltered:" list name prefix" -->
<input type="text" list="unfiltered:search-results" />

I'm sure there are others that readers more familiar with the HTML spec / process can suggest.

Second, (and maybe there's already an effort to standardize this?) clients should show both the value and the inner-content of the options, ideally in a manner that is css-style-able.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the HTML Standard's datalist and text input behavior, then compare the linked CodePen example and the browser cases described here. Determine whether the proposal should define canonical or unfiltered option handling and how option values and inner content are presented. A concrete, agreed specification change and interoperability expectations are needed before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
html
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.