Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the rules around whitespace in attribute selectors?

Tags:

I'm reading the spec on attribute selectors, but I can't find anything that says if whitespace is allowed. I'm guessing it's allowed at the beginning, before and after the operator, and at the end. Is this correct?

like image 857
mpen Avatar asked May 08 '11 22:05

mpen


People also ask

Can a CSS selector have spaces in it?

A space in a CSS selector has very special meaning. It means that the part of the selector that occurs right of the space is within (a child of) the part of the selector to the left. This doesn't apply to your second example, because it has no space.

What is a selector rule?

A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them.

What are attributes selectors?

The CSS Attribute Selector is used to select an element with some specific attribute or attribute value. It is an excellent way to style the HTML elements by grouping them based on some specific attributes and the attribute selector will select those elements with similar attributes.


1 Answers

The rules on whitespace in attribute selectors are stated in the grammar. Here's the Selectors 3 production for attribute selectors (some tokens substituted with their string equivalents for illustration; S* represents 0 or more whitespace characters):

attrib   : '[' S* [ namespace_prefix ]? IDENT S*         [ [ '^=' |             '$=' |             '*=' |             '=' |             '~=' |             '|=' ] S* [ IDENT | STRING ] S*         ]? ']'   ; 

Of course, the grammar isn't terribly useful to someone looking to understand how to write attribute selectors, as it's intended for someone who's implementing a selector engine.

Here's a plain-English explanation:

Whitespace before the attribute selector

This isn't covered in the above production, but the first obvious rule is that if you're attaching an attribute selector to another simple selector or a pseudo-element, don't use a space:

a[href]::after 

If you do, the space is treated as a descendant combinator instead, with the universal selector implied on the attribute selector and anything that may follow it. In other words, these selectors are equivalent to each other, but different from the above:

a [href] ::after a *[href] *::after 

Whitespace inside the attribute selector

Whether you have any whitespace within the brackets and around the comparison operator doesn't matter; I find that browsers seem to treat them as if they weren't there (but I haven't tested extensively). These are all valid according to the grammar and, as far as I've seen, work in all modern browsers:

a[href] a[ href ] a[ href="http://stackoverflow.com" ] a[href ^= "http://"] a[ href ^= "http://" ] 

Whitespace is not allowed between the ^ (or other symbol) and = as these are treated as a single token, and tokens cannot be broken apart.

If IE7 and IE8 implement the grammar correctly, they should be able to handle them all as well.

If a namespace prefix is used, whitespace is not allowed between the prefix and the attribute name.

These are incorrect:

unit[sh| quantity] unit[ sh| quantity="200" ] unit[sh| quantity = "200"] 

These are correct:

unit[sh|quantity] unit[ sh|quantity="200" ] unit[sh|quantity = "200"] 

Whitespace within the attribute value

But notice the quotes around the attribute values above; if you leave them out, and you try to select something whose attribute has spaces in its value you have a syntax error.

This is incorrect:

div[class=one two] 

This is correct:

div[class="one two"] 

This is because an unquoted attribute value is treated as an identifier, which doesn't include whitespace (for obvious reasons), whereas a quoted value is treated as a string. See this spec for more details.

To prevent such errors, I strongly recommend always quoting attribute values, whether in HTML, XHTML (required), XML (required), CSS or jQuery (once required).

Whitespace after the attribute value

As of Selectors 4 (following the original publication of this answer), attribute selectors can accept flags in the form of an identifier appearing after the attribute value. Two flags have been defined pertaining to character case, one for case-insensitive matching:

div[data-foo="bar" i] 

And one for case-sensitive matching (whose addition I had a part in, albeit by proxy of the WHATWG):

ol[type="A" s] ol[type="a" s] 

The grammar has been updated thus:

attrib   : '[' S* attrib_name ']'     | '[' S* attrib_name attrib_match [ IDENT | STRING ] S* attrib_flags? ']'   ;  attrib_name   : wqname_prefix? IDENT S*  attrib_match   : [ '=' |       PREFIX-MATCH |       SUFFIX-MATCH |       SUBSTRING-MATCH |       INCLUDE-MATCH |       DASH-MATCH     ] S*  attrib_flags   : IDENT S* 

In plain English: if the attribute value is not quoted (i.e. it is an identifier), whitespace between it and attrib_flags is required; otherwise, if the attribute value is quoted then whitespace is optional, but strongly recommended for the sake of readability. Whitespace between attrib_flags and the closing bracket is optional as always.

like image 188
BoltClock Avatar answered Sep 26 '22 22:09

BoltClock