Skip to content

Simplified "getAssociatedLabels" with custom elements support - #43997

Closed
Konstantin Roman (nihil-pro) wants to merge 2 commits into
microsoft:mainfrom
nihil-pro:update-fix-43984
Closed

Konstantin Roman (nihil-pro) wants to merge 2 commits into
microsoft:mainfrom
nihil-pro:update-fix-43984

Conversation

@nihil-pro

Copy link
Copy Markdown

fix(selectors): support labels for any correctly form-associated custom elements

"labels" property exists only for INPUT, BUTTON, METER, OUTPUT, PROGRESS, SELECT and TEXTAREA; but hardcoding these names limits custom elements.

Any custom element that wants to be a real form element must also implement a "labels" getter:

class CustomFormElement extends HTMLElement {
  static formAssociated = true;
  #internals;

  constructor() {
    super();
    this.#internals = this.attachInternals();
  }

  get labels() {
    return this.#internals.labels;
  }
}

Also, for a hidden input, the "labels" getter will return null. No reasons to double check.
So this function can be simplified dramatically: return Array.from(Reflect.get(element, "labels") || []);

Fixes #43898

@nihil-pro

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@yury-s

Copy link
Copy Markdown
Member

This reverts the fix from #43900: SE-077 getByLabel should work fails with this change. It also does not work for elements that define a labels getter, because selectors run in an isolated world where page-defined getters are not visible. That is why we read label.control. Closing.

@nihil-pro

Copy link
Copy Markdown
Author

My mistake. Sorry for the oversight. Isolated world changes a lot

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: getByLabel (and accessible name) ignore <label> for form-associated custom elements

2 participants