Open Bug 1752564 Opened 3 years ago Updated 3 years ago

Accessing Element.labels may cause a (misleading) getElementById warning message

Categories

(Core :: DOM: Core & HTML, defect)

defect

Tracking

()

People

(Reporter: nchevobbe, Unassigned)

Details

Steps to reproduce

  1. Go to data:text/html,<meta charset=utf8><label id="label1" for="">Label 1</label><input id=""><script>console.log(document.querySelector("input").labels)</script>
  2. Open the console

Actual results

The following warning message is displayed in the console:

Empty string passed to getElementById()

(defined in https://searchfox.org/mozilla-release/rev/713683b4a6b03aba9e6bd80d65a8e13c462f071a/dom/locales/en-US/chrome/dom/dom.properties#25, emitted from https://searchfox.org/mozilla-release/rev/713683b4a6b03aba9e6bd80d65a8e13c462f071a/dom/base/nsContentUtils.cpp#3998-4001)

This can be quite confusing for the user because clicking on the message location will make them jump to the line where the labels access is done (where there are no mentions of getElementById to be found.

That's something a couple folks (me included) experienced while investigating Bug 1750214.


We could have a better warning message saying that one element as an empty string id.

cc'ing smaug as I think he implemented labels and might have an idea of what to do here.

Flags: needinfo?(bugs)

https://searchfox.org/mozilla-central/rev/7056a708787621758bef9793a93aa7ca8375eeef/dom/base/DocumentOrShadowRoot.cpp#237,239
I guess the caller could explicitly check whether the string is empty and that way avoid the warning.
Though, do we want another warning hinting that for="" is empty?

Flags: needinfo?(bugs)
Severity: -- → S3
You need to log in before you can comment on or make changes to this bug.