[selectors4] Behavior undefined for multiple elements sharing the same ID

# Sangwhan Moon (4 days ago)

Hello www-style,

While dealing with a strange edge case related to duplicated IDs (triggered via cloneNode(), but also can be done by poorly written markup) I noticed that the selectors spec doesn't define what implementations are expected to do when there are multiple elements which define the same ID.

While canonically it is expected for an ID to be unique, implementations seem to be happy to allow duplicates - would it make sense to at least mention this in the section defining ID selectors? Neither HTML nor DOM seems to explicitly require uniqueness as a conformance requirement, but selectors does.

(Return order for selection against duplicate IDs seem to be logical order on the three implementations I tried,whether this belongs in normative or non-normative is a question that remains to be answered...)

Sangwhan

P.S. I'm not on www-style, so if there is anything that I need to reply back on, please copy me. (I'm not entirely sure about the reply etiquette on this ML, vaguely guessing Reply-all is frowned upon)

Contact us to advertise here
# Xidorn Quan (4 days ago)

On Mon, Sep 11, 2017, at 04:04 PM, Sangwhan Moon wrote:

While dealing with a strange edge case related to duplicated IDs (triggered via cloneNode(), but also can be done by poorly written markup) I noticed that the selectors spec doesn't define what implementations are expected to do when there are multiple elements which define the same ID.

The spec makes it pretty clear that all elements with the same ID should be matched.

The spec says:

An ID selector represents an element instance that has an identifier that matches the identifier in the ID selector.

and it specifically mentions the case after:

# Sangwhan Moon (4 days ago)

On 11 Sep 2017, at 3:17 PM, Xidorn Quan me@upsuper.org wrote:

On Mon, Sep 11, 2017, at 04:04 PM, Sangwhan Moon wrote:

While dealing with a strange edge case related to duplicated IDs (triggered via cloneNode(), but also can be done by poorly written markup) I noticed that the selectors spec doesn't define what implementations are expected to do when there are multiple elements which define the same ID.

The spec makes it pretty clear that all elements with the same ID should be matched.

The spec says:

An ID selector represents an element instance that has an identifier that matches the identifier in the ID selector. and it specifically mentions the case after: (It is possible in non-conforming documents for multiple elements to match a single ID selector.)

Oh, I completely missed that statement between the parentheses. Thanks.

I was referring to this bit: "whatever the document language, an ID typed attribute can be used to uniquely identify its element." - my confusion mostly comes from the fact that unique IDs for conformance is implied in HTML at the moment, but is not enforced (and from what I gather, not defined either - which makes it a bit unclear if it duplicate IDs is against conformance or not.) - I suppose that's something I need to bring up with HTML and report back what is actually a conforming document in this context.

(As a minor nit, all the examples in the same section also imply singular, adding to the confusion. :-( )

Sangwhan

# Simon Pieters (4 days ago)

On Mon, Sep 11, 2017 at 8:46 AM, Sangwhan Moon sangwhan@iki.fi wrote:

On 11 Sep 2017, at 3:17 PM, Xidorn Quan me@upsuper.org wrote:

On Mon, Sep 11, 2017, at 04:04 PM, Sangwhan Moon wrote:

While dealing with a strange edge case related to duplicated IDs (triggered via cloneNode(), but also can be done by poorly written markup) I noticed that the selectors spec doesn't define what implementations are expected to do when there are multiple elements which define the same ID.

The spec makes it pretty clear that all elements with the same ID should be matched.

The spec says:

An ID selector represents an element instance that has an identifier that matches the identifier in the ID selector. and it specifically mentions the case after: (It is possible in non-conforming documents for multiple elements to match a single ID selector.)

Oh, I completely missed that statement between the parentheses. Thanks.

I was referring to this bit: "whatever the document language, an ID typed attribute can be used to uniquely identify its element." - my confusion mostly comes from the fact that unique IDs for conformance is implied in HTML at the moment, but is not enforced (and from what I gather, not defined either - which makes it a bit unclear if it duplicate IDs is against conformance or not.) - I suppose that's something I need to bring up with HTML and report back what is actually a conforming document in this context.

"When specified on HTML elements, the id attribute value must be unique amongst all the IDs in the element's tree and must contain at least one character. The value must not contain any ASCII whitespace."

html.spec.whatwg.org/multipage/dom.html#the-id-attribute

HTH,

(As a minor nit, all the examples in the same section also imply singular,

# Tab Atkins Jr. (4 days ago)

On Mon, Sep 11, 2017 at 12:23 AM, Simon Pieters simonp@opera.com wrote:

On Mon, Sep 11, 2017 at 8:46 AM, Sangwhan Moon sangwhan@iki.fi wrote:

On 11 Sep 2017, at 3:17 PM, Xidorn Quan me@upsuper.org wrote:

On Mon, Sep 11, 2017, at 04:04 PM, Sangwhan Moon wrote:

While dealing with a strange edge case related to duplicated IDs (triggered via cloneNode(), but also can be done by poorly written markup) I noticed that the selectors spec doesn't define what implementations are expected to do when there are multiple elements which define the same ID.

The spec makes it pretty clear that all elements with the same ID should be matched.

The spec says:

An ID selector represents an element instance that has an identifier that matches the identifier in the ID selector. and it specifically mentions the case after: (It is possible in non-conforming documents for multiple elements to match a single ID selector.)

Oh, I completely missed that statement between the parentheses. Thanks.

I was referring to this bit: "whatever the document language, an ID typed attribute can be used to uniquely identify its element." - my confusion mostly comes from the fact that unique IDs for conformance is implied in HTML at the moment, but is not enforced (and from what I gather, not defined either - which makes it a bit unclear if it duplicate IDs is against conformance or not.) - I suppose that's something I need to bring up with HTML and report back what is actually a conforming document in this context.

"When specified on HTML elements, the id attribute value must be unique amongst all the IDs in the element's tree and must contain at least one character. The value must not contain any ASCII whitespace."

html.spec.whatwg.org/multipage/dom.html#the-id-attribute

Yeah, CSS expects that IDs are singular in the document, but defines how the ID selector works regardless; and HTML defines that a valid document has unique IDs (but also defines how its own things work regardless).

~TJ

Want more features?

Request early access to our private beta of readable email premium.