Skip to content

Instantly share code, notes, and snippets.

@mrego
Created October 6, 2022 14:41
Show Gist options
  • Save mrego/c900b2908780fa047501f5641fda92d1 to your computer and use it in GitHub Desktop.
Save mrego/c900b2908780fa047501f5641fda92d1 to your computer and use it in GitHub Desktop.
Use empty string for IDREF attribute reflection

Use empty string for IDREF attribute reflection

This is about attribute reflection for element references for things like ariaLabelledByElements.

Problem

Imagine we have a label an an input:

  <label id="foo">A label</label>
  <input id="myinput" />

And we want to set a relationship between both in JavaScript:

  myinput.ariaLabelledByElements = [ foo ];

Right now with the current spec text, the attribute aria-labelledby will be set to foo in the input element. That's known as sprouting:

  <label id="foo">A label</label>
  <input id="myinput" aria-labelledby="foo"/>

This has a problem if something changes later, as we can leave out of sync IDs values there in different situations. Examples:

  • The label ID gets modified:
      foo.id = "bar";
    The HTML would be:
      <label id="bar">A label</label>
      <input id="myinput" aria-labelledby="foo"/>
    But if you check the relationship from JavaScript or in the a11y tree, you still get the proper label associated to the element. This would be very confusing if there's another element called foo on the tree, as the attribute would be somehow pointing to that other element, while the actual relationship set is pointing to the bar one.
  • The label element is removed from the tree:
      document.removeChild(foo);
    In this case attribute would have a stale ID:
      <input id="myinput" aria-labelledby="foo"/>

Proposal

Set always the empty string, that means that a element reference has been set from JavaScript. And we never get into the case of having an out of sync ID. If needed people could query which elements have an empty attribute to know if they have an underlying relationship set.

In the previous example when we do the association we'll get:

  <label id="foo">A label</label>
  <input id="myinput" aria-labelledby=""/>

So whatever changes later regarding the label element has no bad effects.

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment