4.2. Serializing Selectors

A serialization is here defined as a string representing one of the syntactical constructs defined in [Selectors] section 4.

4.2.1. Serialization of a group of selectors

The serialization of a group of selectors is the concatenation of the serialization in parsing order of each individual selector in the group separated by a comma "," and a space (U+0020).

4.2.2. Serialization of a selector

The serialization of a selector is the concatenation of the serialization in parsing order of each sequence of simple selectors in the selector separated by a space (U+0020) if the combinator is not the descendant combinator, the serialization of the combinator separating the sequence and the next one in selector parsing order, another space (U+0020) if the combinator is not the descendant combinator. The serialization of the pseudo-element potentially appended to the last sequence of simple selectors in the selector is appended to the serialization of the selector.

4.2.3. Serialization of a combinator

The serialization of a combinator is defined as a space (U+0020) for the descendant combinator, ">" for the child combinator, "+" for the adjacent sibling combinator, "-" for the general sibling combinator.

4.2.4. Serialization of a sequence of simple selectors

The serialization of a sequence of simple selectors is the concatenation of the serialization in parsing order of all the simple selectors in the sequence. A "*" (U+002A) will be prepended if no universal selector or type element selector is present in the sequence of simple selectors.

4.2.5. Serialization of a pseudo-element

The serialization of a pseudo-element is the concatenation of "::" (U+002E U+002E) and the escaped pseudo-element name.

4.2.6. Serialization of a simple selector

4.2.6.1 Universal selector and type element selector
The serialization of a universal selector is the concatenation of
  1. If the namespace prefix maps to a namespace that is not the default namespace and is not the null namespace (not in a namespace): the escaped namespace prefix, followed by a "|" (U+007C).

  2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace): "|" (U+007C).

  3. If this is a type selector: the escaped element name.

  4. If this is a universal selector: "*" (U+002A).

4.2.6.2. Attribute selector

The serialization of an attribute selector is the concatenation of

  1. "[" (U+005B)

  2. If the namespace prefix maps to a namespace that is not the null namespace (not in a namespace),  the escaped namespace prefix, followed by a "|" (U+007C)

  3. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace), "|" (U+007C)

  4. The escaped attribute name

  5. If there is an attribute value specified, "=", "~=", "|=", "^=", "$=", or "*=" as appropriate (depending on the type of attribute selector), followed by the string escaped attribute value

  6. "]" (U+005D)

4.2.6.3. Class selector

The serialization of a class selector is the concatenation of a "." (U+002E) and the escaped class name.

4.2.6.4. ID selector

The serialization of an ID selector is the concatenation of a "#" (U+0023) and the escaped ID.

4.2.6.5. Pseudo-classes without argument

The serialization of a pseudo-class taking no argument is the concatenation of a ":" (U+003A) and the escaped pseudo-class name.

4.2.6.6. The language pseudo-class

The serialization of a language pseudo-class is the concatenation of a ":" (U+003A), the escaped pseudo-class name, a "(" (U+0028), the escaped argument of the pseudo-class and finally a ")" (U+0029).

4.2.6.7. :nth-child() and friends

The serialization of such a pseudo-class is the concatenation of a ":" (U+003A), the escaped pseudo-class name, a "(" (U+0028), the serialization of the argument and finally a ")" (U+0029).

The serialization of the argument of the pseudo-class is as follows:

  1. If the value is odd let it be "2n+1".

  2. If the value is even let it be "2n".

  3. If a is zero let it be b serialized as <integer>.

  4. If a is one or minus one and b is zero let it be "n" (U+006E).

  5. If a is one or minus one let the value be "n" (U+006E), followed by "+" (U+002B) if b is positive, followed by b serialized as <integer>.

  6. If b is zero let the value be a serialized as <integer>, followed by "n" (U+006E).

  7. Otherwise let the value be a serialized as <integer>, followed by "n" (U+006E), followed by "+" (U+002B) if b is positive, followed by b serialized as <integer>.

4.2.6.7. The negation pseudo-class

The serialization of a language pseudo-class is the concatenation of a ":" (U+003A), the escaped pseudo-class name, a "(" (U+0028), the serialization of the simple selector being the argument of the pseudo-class and finally a ")" (U+0029).