Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
3842 lines (3063 sloc) 132 KB
<pre class='metadata'>
Title: CSS Syntax Module Level 3
Shortname: css-syntax
Level: 3
Status: ED
Work Status: Testing
Group: csswg
ED: https://drafts.csswg.org/css-syntax/
TR: https://www.w3.org/TR/css-syntax-3/
Previous Version: https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/
Previous Version: https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/
Previous Version: https://www.w3.org/TR/2013/WD-css-syntax-3-20131105/
Previous Version: https://www.w3.org/TR/2013/WD-css-syntax-3-20130919/
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/, w3cid 42199
Editor: Simon Sapin, Mozilla, http://exyr.org/about/, w3cid 58001
Abstract: This module describes, in general terms, the basic structure and syntax of CSS stylesheets. It defines, in detail, the syntax and parsing of CSS - how to turn a stream of bytes into a meaningful stylesheet.
Ignored Terms: <keyframes-name>, <keyframe-rule>, <keyframe-selector>, <translation-value>, <media-query-list>, <unicode-range-token>
Ignored Vars: +b, -b, foo
</pre>
<pre class=link-defaults>
spec:css-text-decor-3; type:property; text:text-decoration
spec:css-color-3; type:property; text:color
spec:css-transforms-1; type:function; text:translatex()
spec:encoding-1; type:dfn; text:get an encoding
spec:html; type:element; text:a
spec:infra; type:dfn;
text:string
text:list
</pre>
<h2 id="intro">
Introduction</h2>
<em>This section is not normative.</em>
This module defines the abstract syntax and parsing of CSS stylesheets
and other things which use CSS syntax
(such as the HTML <code>style</code> attribute).
It defines algorithms for converting a stream of Unicode <a>code points</a>
(in other words, text)
into a stream of CSS tokens,
and then further into CSS objects
such as stylesheets, rules, and declarations.
<h3 id="placement">
Module interactions</h3>
This module defines the syntax and parsing of CSS stylesheets.
It supersedes the lexical scanner and grammar defined in CSS 2.1.
<h2 id='syntax-description'>
Description of CSS's Syntax</h2>
<em>This section is not normative.</em>
A CSS document is a series of <a>style rules</a>--
which are <a>qualified rules</a> that apply styles to elements in a document--
and <a>at-rules</a>--
which define special processing rules or values for the CSS document.
A <a>qualified rule</a> starts with a prelude
then has a {}-wrapped block containing a sequence of declarations.
The meaning of the prelude varies based on the context that the rule appears in--
for <a>style rules</a>, it's a selector which specifies what elements the declarations will apply to.
Each declaration has a name,
followed by a colon and the declaration value.
Declarations are separated by semicolons.
<div class='example'>
A typical rule might look something like this:
<pre>
p > a {
color: blue;
text-decoration: underline;
}
</pre>
In the above rule, "<code>p > a</code>" is the selector,
which, if the source document is HTML,
selects any <{a}> elements that are children of a <{p}> element.
"<code>color: blue</code>" is a declaration specifying that,
for the elements that match the selector,
their 'color' property should have the value ''blue''.
Similarly, their 'text-decoration' property should have the value ''underline''.
</div>
<a>At-rules</a> are all different, but they have a basic structure in common.
They start with an "@" <a>code point</a> followed by their name as a CSS keyword.
Some <a>at-rules</a> are simple statements,
with their name followed by more CSS values to specify their behavior,
and finally ended by a semicolon.
Others are blocks;
they can have CSS values following their name,
but they end with a {}-wrapped block,
similar to a <a>qualified rule</a>.
Even the contents of these blocks are specific to the given <a>at-rule</a>:
sometimes they contain a sequence of declarations, like a <a>qualified rule</a>;
other times, they may contain additional blocks, or at-rules, or other structures altogether.
<div class='example'>
Here are several examples of <a>at-rules</a> that illustrate the varied syntax they may contain.
<pre>@import "my-styles.css";</pre>
The ''@import'' <a>at-rule</a> is a simple statement.
After its name, it takes a single string or ''url()'' function to indicate the stylesheet that it should import.
<pre>
@page :left {
margin-left: 4cm;
margin-right: 3cm;
}
</pre>
The ''@page'' <a>at-rule</a> consists of an optional page selector (the '':left'' pseudoclass),
followed by a block of properties that apply to the page when printed.
In this way, it's very similar to a normal style rule,
except that its properties don't apply to any "element",
but rather the page itself.
<pre>
@media print {
body { font-size: 10pt }
}
</pre>
The ''@media'' <a>at-rule</a> begins with a media type
and a list of optional media queries.
Its block contains entire rules,
which are only applied when the ''@media''s conditions are fulfilled.
</div>
Property names and <a>at-rule</a> names are always <a>identifiers</a>,
which have to start with a letter or a hyphen followed by a letter,
and then can contain letters, numbers, hyphens, or underscores.
You can include any <a>code point</a> at all,
even ones that CSS uses in its syntax,
by <a>escaping</a> it.
The syntax of selectors is defined in the <a href="https://www.w3.org/TR/selectors/">Selectors spec</a>.
Similarly, the syntax of the wide variety of CSS values is defined in the <a href="https://www.w3.org/TR/css3-values/">Values &amp; Units spec</a>.
The special syntaxes of individual <a>at-rules</a> can be found in the specs that define them.
<h3 id="escaping">
Escaping</h3>
<em>This section is not normative.</em>
Any Unicode <a>code point</a> can be included in an <a>identifier</a> or quoted string
by <dfn id="escape-codepoint">escaping</dfn> it.
CSS escape sequences start with a backslash (\), and continue with:
<ul>
<li>
Any Unicode <a>code point</a> that is not a <a>hex digits</a> or a <a>newline</a>.
The escape sequence is replaced by that <a>code point</a>.
<li>
Or one to six <a>hex digits</a>, followed by an optional <a>whitespace</a>.
The escape sequence is replaced by the Unicode <a>code point</a>
whose value is given by the hexadecimal digits.
This optional whitespace allow hexadecimal escape sequences
to be followed by "real" hex digits.
<p class=example>
An <a>identifier</a> with the value "&B"
could be written as ''\26 B'' or ''\000026B''.
<p class=note>
A "real" space after the escape sequence must be doubled.
</ul>
<h3 id="error-handling">
Error Handling</h3>
<em>This section is not normative.</em>
When errors occur in CSS,
the parser attempts to recover gracefully,
throwing away only the minimum amount of content
before returning to parsing as normal.
This is because errors aren't always mistakes--
new syntax looks like an error to an old parser,
and it's useful to be able to add new syntax to the language
without worrying about stylesheets that include it being completely broken in older UAs.
The precise error-recovery behavior is detailed in the parser itself,
but it's simple enough that a short description is fairly accurate.
<ul>
<li>
At the "top level" of a stylesheet,
an <<at-keyword-token>> starts an at-rule.
Anything else starts a qualified rule,
and is included in the rule's prelude.
This may produce an invalid selector,
but that's not the concern of the CSS parser--
at worst, it means the selector will match nothing.
<li>
Once an at-rule starts,
nothing is invalid from the parser's standpoint;
it's all part of the at-rule's prelude.
Encountering a <<semicolon-token>> ends the at-rule immediately,
while encountering an opening curly-brace <a href="#tokendef-open-curly">&lt;{-token></a> starts the at-rule's body.
The at-rule seeks forward, matching blocks (content surrounded by (), {}, or [])
until it finds a closing curly-brace <a href="#tokendef-close-curly">&lt;}-token></a> that isn't matched by anything else
or inside of another block.
The contents of the at-rule are then interpreted according to the at-rule's own grammar.
<li>
Qualified rules work similarly,
except that semicolons don't end them;
instead, they are just taken in as part of the rule's prelude.
When the first {} block is found,
the contents are always interpreted as a list of declarations.
<li>
When interpreting a list of declarations,
unknown syntax at any point causes the parser to throw away whatever declaration it's currently building,
and seek forward until it finds a semicolon (or the end of the block).
It then starts fresh, trying to parse a declaration again.
<li>
If the stylesheet ends while any rule, declaration, function, string, etc. are still open,
everything is automatically closed.
This doesn't make them invalid,
though they may be incomplete
and thus thrown away when they are verified against their grammar.
</ul>
After each construct (declaration, style rule, at-rule) is parsed,
the user agent checks it against its expected grammar.
If it does not match the grammar,
it's <dfn export for=css>invalid</dfn>,
and gets <dfn export for=css>ignored</dfn> by the UA,
which treats it as if it wasn't there at all.
<!--
████████ ███████ ██ ██ ████████ ██ ██ ████ ████████ ████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ █████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██
██ ███████ ██ ██ ████████ ██ ██ ████ ████████ ████ ██ ██ ██████
-->
<h2 id="tokenizing-and-parsing">
Tokenizing and Parsing CSS</h2>
User agents must use the parsing rules described in this specification
to generate the CSSOM trees from text/css resources.
Together, these rules define what is referred to as the CSS parser.
This specification defines the parsing rules for CSS documents,
whether they are syntactically correct or not.
Certain points in the parsing algorithm are said to be <dfn lt="parse error">parse errors</dfn>.
The error handling for parse errors is well-defined:
user agents must either act as described below when encountering such problems,
or must abort processing at the first error that they encounter for which they do not wish to apply the rules described below.
Conformance checkers must report at least one parse error condition to the user
if one or more parse error conditions exist in the document
and must not report parse error conditions
if none exist in the document.
Conformance checkers may report more than one parse error condition if more than one parse error condition exists in the document.
Conformance checkers are not required to recover from parse errors,
but if they do,
they must recover in the same way as user agents.
<h3 id="parsing-overview">
Overview of the Parsing Model</h3>
The input to the CSS parsing process consists of a stream of Unicode <a>code points</a>,
which is passed through a tokenization stage followed by a tree construction stage.
The output is a CSSStyleSheet object.
Note: Implementations that do not support scripting do not have to actually create a CSSOM CSSStyleSheet object,
but the CSSOM tree in such cases is still used as the model for the rest of the specification.
<h3 id="input-byte-stream">
The input byte stream</h3>
When parsing a stylesheet,
the stream of Unicode <a>code points</a> that comprises the input to the tokenization stage
might be initially seen by the user agent as a stream of bytes
(typically coming over the network or from the local file system).
If so, the user agent must decode these bytes into <a>code points</a> according to a particular character encoding.
<div algorithm>
To <dfn for=CSS lt="decode bytes">decode</dfn> a |stylesheet|’s stream of bytes into a stream of <a>code points</a>:
1. [=Determine the fallback encoding=] of |stylesheet|,
and let |fallback| be the result.
2. [=Decode=] |stylesheet|’s stream of bytes
with fallback encoding |fallback|,
and return the result.
Note: The <a>decode</a> algorithm
gives precedence to a byte order mark (BOM),
and only uses the fallback when none is found.
</div>
<div algorithm>
To <dfn>determine the fallback encoding</dfn> of a |stylesheet|:
<ol>
<li>
If HTTP or equivalent protocol provides an |encoding label| (e.g. via the charset parameter of the Content-Type header) for the |stylesheet|,
[=get an encoding=] from |encoding label|.
If that does not return failure,
return it.
<li>
Otherwise, check |stylesheet|’s byte stream.
If the first 1024 bytes of the stream begin with the hex sequence
<pre>40 63 68 61 72 73 65 74 20 22 XX* 22 3B</pre>
where each <code>XX</code> byte is a value between 0<sub>16</sub> and 21<sub>16</sub> inclusive
or a value between 23<sub>16</sub> and 7F<sub>16</sub> inclusive,
then [=get an encoding=]
from a string formed out of
the sequence of <code>XX</code> bytes,
interpreted as <code>ASCII</code>.
<details class='note'>
<summary>What does that byte sequence mean?</summary>
The byte sequence above,
when decoded as ASCII,
is the string "<code>@charset "…";</code>",
where the "…" is the sequence of bytes corresponding to the encoding's label.
</details>
If the return value was <code>utf-16be</code> or <code>utf-16le</code>,
return <code>utf-8</code>;
if it was anything else except failure,
return it.
<details class='note'>
<summary>Why use utf-8 when the declaration says utf-16?</summary>
The bytes of the encoding declaration spell out “<code>@charset "…";</code>” in ASCII,
but UTF-16 is not ASCII-compatible.
Either you've typed in complete gibberish (like <code>䁣桡牳整•utf-16be∻</code>) to get the right bytes in the document,
which we don't want to encourage,
or your document is actually in an ASCII-compatible encoding
and your encoding declaration is lying.
Either way, defaulting to UTF-8 is a decent answer.
As well, this mimics the behavior of HTML's <code>&lt;meta charset></code> attribute.
</details>
Note: Note that the syntax of an encoding declaration <em>looks like</em> the syntax of an <a>at-rule</a> named ''@charset'',
but no such rule actually exists,
and the rules for how you can write it are much more restrictive than they would normally be for recognizing such a rule.
A number of things you can do in CSS that would produce a valid ''@charset'' rule (if one existed),
such as using multiple spaces, comments, or single quotes,
will cause the encoding declaration to not be recognized.
This behavior keeps the encoding declaration as simple as possible,
and thus maximizes the likelihood of it being implemented correctly.
<li>
Otherwise, if an <a>environment encoding</a> is provided by the referring document,
return it.
<li>
Otherwise, return <code>utf-8</code>.
</ol>
<div class='note'>
Though UTF-8 is the default encoding for the web,
and many newer web-based file formats assume or require UTF-8 encoding,
CSS was created before it was clear which encoding would win,
and thus can't automatically assume the stylesheet is UTF-8.
Stylesheet authors <em>should</em> author their stylesheets in UTF-8,
and ensure that either an HTTP header (or equivalent method) declares the encoding of the stylesheet to be UTF-8,
or that the referring document declares its encoding to be UTF-8.
(In HTML, this is done by adding a <code>&lt;meta charset=utf-8></code> element to the head of the document.)
If neither of these options are available,
authors should begin the stylesheet with a UTF-8 BOM
or the exact characters
<pre>@charset "utf-8";</pre>
</div>
</div>
Document languages that refer to CSS stylesheets that are decoded from bytes
may define an <dfn export>environment encoding</dfn> for each such stylesheet,
which is used as a fallback when other encoding hints are not available or can not be used.
The concept of <a>environment encoding</a> only exists for compatibility with legacy content.
New formats and new linking mechanisms <b>should not</b> provide an <a>environment encoding</a>,
so the stylesheet defaults to UTF-8 instead in the absence of more explicit information.
Note: [[HTML]] defines <a href="https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet">the environment encoding for <code>&lt;link rel=stylesheet></code></a>.
Note: [[CSSOM]] defines <a href="https://drafts.csswg.org/cssom/#requirements-on-user-agents-implementing-the-xml-stylesheet-processing-instruction">the environment encoding for <code>&lt;xml-stylesheet?></code></a>.
Note: [[CSS-CASCADE-3]] defines <a at-rule lt=@import>the environment encoding for <code>@import</code></a>.
<h3 id="input-preprocessing">
Preprocessing the input stream</h3>
The <dfn>input stream</dfn> consists of the [=filtered code points=]
pushed into it as the input byte stream is decoded.
<div algorithm>
To <dfn for=CSS lt="filter code points|filtered code points">filter code points</dfn>
from a stream of (unfiltered) [=code points=] |input|:
<ul>
<li>
Replace any U+000D CARRIAGE RETURN (CR) <a>code points</a>,
U+000C FORM FEED (FF) <a>code points</a>,
or pairs of U+000D CARRIAGE RETURN (CR) followed by U+000A LINE FEED (LF) in |input|
by a single U+000A LINE FEED (LF) <a>code point</a>.
<li>
Replace any U+0000 NULL or <a>surrogate</a> <a>code points</a> in |input|
with U+FFFD REPLACEMENT CHARACTER (�).
</ul>
</div>
<h2 id="tokenization">
Tokenization</h2>
To <dfn for=CSS lt="tokenize|tokenization">tokenize</dfn> a stream of <a>code points</a>
into a stream of CSS tokens |input|,
repeatedly <a>consume a token</a> from |input|
until an <<EOF-token>> is reached,
pushing each of the returned tokens into a stream.
Note: Each call to the <a>consume a token</a> algorithm
returns a single token,
so it can also be used "on-demand" to tokenize a stream of <a>code points</a> <em>during</em> parsing,
if so desired.
The output of tokenization step is a stream of zero or more of the following tokens:
<dfn>&lt;ident-token></dfn>,
<dfn>&lt;function-token></dfn>,
<dfn>&lt;at-keyword-token></dfn>,
<dfn>&lt;hash-token></dfn>,
<dfn>&lt;string-token></dfn>,
<dfn>&lt;bad-string-token></dfn>,
<dfn>&lt;url-token></dfn>,
<dfn>&lt;bad-url-token></dfn>,
<dfn>&lt;delim-token></dfn>,
<dfn>&lt;number-token></dfn>,
<dfn>&lt;percentage-token></dfn>,
<dfn>&lt;dimension-token></dfn>,
<dfn>&lt;whitespace-token></dfn>,
<dfn>&lt;CDO-token></dfn>,
<dfn>&lt;CDC-token></dfn>,
<dfn>&lt;colon-token></dfn>,
<dfn>&lt;semicolon-token></dfn>,
<dfn>&lt;comma-token></dfn>,
<dfn id="tokendef-open-square">&lt;[-token></dfn>,
<dfn id="tokendef-close-square">&lt;]-token></dfn>,
<dfn id="tokendef-open-paren">&lt;(-token></dfn>,
<dfn id="tokendef-close-paren">&lt;)-token></dfn>,
<dfn id="tokendef-open-curly">&lt;{-token></dfn>,
and <dfn id="tokendef-close-curly">&lt;}-token></dfn>.
<ul>
<li>
<<ident-token>>, <<function-token>>, <<at-keyword-token>>, <<hash-token>>, <<string-token>>, and <<url-token>> have a value composed of zero or more <a>code points</a>.
Additionally, hash tokens have a type flag set to either "id" or "unrestricted". The type flag defaults to "unrestricted" if not otherwise set.
<li>
<<delim-token>> has a value composed of a single <a>code point</a>.
<li>
<<number-token>>, <<percentage-token>>, and <<dimension-token>> have a numeric value.
<<number-token>> and <<dimension-token>> additionally have a type flag set to either "integer" or "number". The type flag defaults to "integer" if not otherwise set.
<<dimension-token>> additionally have a unit composed of one or more <a>code points</a>.
</ul>
Note: The type flag of hash tokens is used in the Selectors syntax [[SELECT]].
Only hash tokens with the "id" type are valid <a href="https://www.w3.org/TR/selectors/#id-selectors">ID selectors</a>.
<!--
████████ ███ ████ ██ ████████ ███████ ███ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██ ██ ██ ██ ████████ ██ ██ ██ ██ ██ ██
██ ██ █████████ ██ ██ ██ ██ ██ ██ █████████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████ ████████ ██ ██ ███████ ██ ██ ████████
-->
<h3 id='token-diagrams'>
Token Railroad Diagrams</h3>
<em>This section is non-normative.</em>
This section presents an informative view of the tokenizer,
in the form of railroad diagrams.
Railroad diagrams are more compact than an explicit parser,
but often easier to read than an regular expression.
These diagrams are <em>informative</em> and <em>incomplete</em>;
they describe the grammar of "correct" tokens,
but do not describe error-handling at all.
They are provided solely to make it easier to get an intuitive grasp of the syntax of each token.
Diagrams with names such as <em>&lt;foo-token></em> represent tokens.
The rest are productions referred to by other diagrams.
<dl>
<dt id="comment-diagram">comment
<dd>
<pre class='railroad'>
T: /*
Star:
N: anything but * followed by /
T: */
</pre>
<dt id="newline-diagram">newline
<dd>
<pre class='railroad'>
Choice:
T: \n
T: \r\n
T: \r
T: \f
</pre>
<dt id="whitespace-diagram">whitespace
<dd>
<pre class='railroad'>
Choice:
T: space
T: \t
N: newline
</pre>
<dt id="hex-digit-diagram">hex digit
<dd>
<pre class='railroad'>
N: 0-9 a-f or A-F
</pre>
<dt id="escape-diagram">escape
<dd>
<pre class='railroad'>
T: \
Choice:
N: not newline or hex digit
Seq:
Plus:
N: hex digit
C: 1-6 times
Opt: skip
N: whitespace
</pre>
<dt id="whitespace-token-diagram"><<whitespace-token>>
<dd>
<pre class='railroad'>
Plus:
N: whitespace
</pre>
<dt id="ws*-diagram">ws*
<dd>
<pre class='railroad'>
Star:
N: <whitespace-token>
</pre>
<dt id="ident-token-diagram"><<ident-token>>
<dd>
<pre class='railroad'>
Or: 1
T: --
Seq:
Opt: skip
T: -
Or:
N: a-z A-Z _ or non-ASCII
N: escape
Star:
Or:
N: a-z A-Z 0-9 _ - or non-ASCII
N: escape
</pre>
<dt id="function-token-diagram"><<function-token>>
<dd>
<pre class='railroad'>
N: <ident-token>
T: (
</pre>
<dt id="at-keyword-token-diagram"><<at-keyword-token>>
<dd>
<pre class='railroad'>
T: @
N: <ident-token>
</pre>
<dt id="hash-token-diagram"><<hash-token>>
<dd>
<pre class='railroad'>
T: #
Plus:
Choice:
N:a-z A-Z 0-9 _ - or non-ASCII
N: escape
</pre>
<dt id="string-token-diagram"><<string-token>>
<dd>
<pre class='railroad'>
Choice:
Seq:
T: "
Star:
Choice:
N: not " \ or newline
N: escape
Seq:
T: \
N: newline
T: "
Seq:
T: '
Star:
Choice:
N: not ' \ or newline
N: escape
Seq:
T: \
N: newline
T: '
</pre>
<dt id="url-token-diagram"><<url-token>>
<dd>
<pre class='railroad'>
N: <ident-token "url">
T: (
N: ws*
Star:
Choice:
N: not " ' ( ) \ ws or non-printable
N: escape
N: ws*
T: )
</pre>
<dt id="number-token-diagram"><<number-token>>
<dd>
<pre class='railroad'>
Choice: 1
T: +
Skip:
T: -
Choice:
Seq:
Plus:
N: digit
T: .
Plus:
N: digit
Plus:
N: digit
Seq:
T: .
Plus:
N: digit
Opt: skip
Seq:
Choice:
T: e
T: E
Choice: 1
T: +
S:
T: -
Plus:
N: digit
</pre>
<dt id="dimension-token-diagram"><<dimension-token>>
<dd>
<pre class='railroad'>
N: <number-token>
N: <ident-token>
</pre>
<dt id="percentage-token-diagram"><<percentage-token>>
<dd>
<pre class='railroad'>
N: <number-token>
T: %
</pre>
<dt id="CDO-token-diagram"><<CDO-token>>
<dd>
<pre class='railroad'>
T: <<!---->!--
</pre>
<dt id="CDC-token-diagram"><<CDC-token>>
<dd>
<pre class='railroad'>
T: -->
</pre>
</dl>
<!--
████████ ████████ ██ ██ ██████
██ ██ ██ ███ ██ ██ ██
██ ██ ██ ████ ██ ██
██ ██ ██████ ██ ██ ██ ██████
██ ██ ██ ██ ████ ██
██ ██ ██ ██ ███ ██ ██
████████ ██ ██ ██ ██████
-->
<h3 id="tokenizer-definitions">
Definitions</h3>
This section defines several terms used during the tokenization phase.
<dl export>
<dt><dfn>next input code point</dfn>
<dd>
The first <a>code point</a> in the <a>input stream</a> that has not yet been consumed.
<dt><dfn>current input code point</dfn>
<dd>
The last <a>code point</a> to have been consumed.
<dt><dfn>reconsume the current input code point</dfn>
<dd>
Push the <a>current input code point</a> back onto the front of the <a>input stream</a>,
so that the next time you are instructed to consume the <a>next input code point</a>,
it will instead reconsume the <a>current input code point</a>.
<dt><dfn>EOF code point</dfn>
<dd>
A conceptual <a>code point</a> representing the end of the <a>input stream</a>.
Whenever the <a>input stream</a> is empty,
the <a>next input code point</a> is always an EOF code point.
<dt><dfn export>digit</dfn>
<dd>
A <a>code point</a> between U+0030 DIGIT ZERO (0) and U+0039 DIGIT NINE (9) inclusive.
<dt><dfn export>hex digit</dfn>
<dd>
A <a>digit</a>,
or a <a>code point</a> between U+0041 LATIN CAPITAL LETTER A (A) and U+0046 LATIN CAPITAL LETTER F (F) inclusive,
or a <a>code point</a> between U+0061 LATIN SMALL LETTER A (a) and U+0066 LATIN SMALL LETTER F (f) inclusive.
<dt><dfn export>uppercase letter</dfn>
<dd>
A <a>code point</a> between U+0041 LATIN CAPITAL LETTER A (A) and U+005A LATIN CAPITAL LETTER Z (Z) inclusive.
<dt><dfn export>lowercase letter</dfn>
<dd>
A <a>code point</a> between U+0061 LATIN SMALL LETTER A (a) and U+007A LATIN SMALL LETTER Z (z) inclusive.
<dt><dfn export>letter</dfn>
<dd>
An <a>uppercase letter</a>
or a <a>lowercase letter</a>.
<dt><dfn export>non-ASCII code point</dfn>
<dd>
A <a>code point</a> with a value equal to or greater than U+0080 &lt;control>.
<dt><dfn export lt="identifier-start code point | name-start code point" oldids="name-start-code-point">identifier-start code point</dfn>
<dd>
A <a>letter</a>,
a <a>non-ASCII code point</a>,
or U+005F LOW LINE (_).
<dt><dfn export lt="identifier code point | name code point" oldids="name-code-point">identifier code point</dfn>
<dd>
An <a>identifier-start code point</a>,
a <a>digit</a>,
or U+002D HYPHEN-MINUS (-).
<dt><dfn export>non-printable code point</dfn>
<dd>
A <a>code point</a> between U+0000 NULL and U+0008 BACKSPACE inclusive,
or U+000B LINE TABULATION,
or a <a>code point</a> between U+000E SHIFT OUT and U+001F INFORMATION SEPARATOR ONE inclusive,
or U+007F DELETE.
<dt><dfn export>newline</dfn>
<dd>
U+000A LINE FEED.
<span class='note'>
Note that U+000D CARRIAGE RETURN and U+000C FORM FEED are not included in this definition,
as they are converted to U+000A LINE FEED during <a href="#input-preprocessing">preprocessing</a>.
</span>
<dt><dfn export>whitespace</dfn>
<dd>A <a>newline</a>, U+0009 CHARACTER TABULATION, or U+0020 SPACE.
<dt><dfn export>maximum allowed code point</dfn>
<dd>The greatest <a>code point</a> defined by Unicode: U+10FFFF.
<dt><dfn export lt="identifier | ident | CSS identifier | CSS ident" oldids="css-identifier">identifier</dfn>
<dd>
A sequence of [=code points=] that has the same syntax as an <<ident-token>>.
Note: The part of an <<at-keyword-token>> after the "@",
the part of a <<hash-token>> (with the "id" type flag) after the "#",
the part of a <<function-token>> before the "(",
and the unit of a <<dimension-token>>
are all [=identifiers=].
<dt><dfn>representation</dfn>
<dd>
The <a>representation</a> of a token
is the subsequence of the <a>input stream</a>
consumed by the invocation of the <a>consume a token</a> algorithm
that produced it.
This is preserved for a few algorithms that rely on subtle details of the input text,
which a simple "re-serialization" of the tokens might disturb.
The <a>representation</a> is only consumed by internal algorithms,
and never directly exposed,
so it's not actually required to preserve the exact text;
equivalent methods,
such as associating each token with offsets into the source text,
also suffice.
Note: In particular, the <a>representation</a> preserves details
such as whether .009 was written as ''.009'' or ''9e-3'',
and whether a character was written literally
or as a CSS escape.
The former is necessary to properly parse <<urange>> productions;
the latter is basically an accidental leak of the tokenizing abstraction,
but allowed because it makes the impl easier to define.
If a token is ever produced by an algorithm directly,
rather than thru the tokenization algorithm in this specification,
its representation is the empty string.
</dl>
<!--
████████ ███████ ██ ██ ████████ ██ ██ ████ ████████ ████████ ████████
██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██
██ ██ ██ █████ ██████ ██ ██ ██ ██ ██ ██████ ████████
██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██
██ ███████ ██ ██ ████████ ██ ██ ████ ████████ ████████ ██ ██
-->
<h3 id="tokenizer-algorithms">
Tokenizer Algorithms</h3>
The algorithms defined in this section transform a stream of <a>code points</a> into a stream of tokens.
<h4 id="consume-token">
Consume a token</h4>
This section describes how to <dfn>consume a token</dfn> from a stream of <a>code points</a>.
It will return a single token of any type.
<a>Consume comments</a>.
Consume the <a>next input code point</a>.
<dl>
<dt><a>whitespace</a>
<dd>
Consume as much <a>whitespace</a> as possible.
Return a <<whitespace-token>>.
<dt>U+0022 QUOTATION MARK (")
<dd>
<a>Consume a string token</a>
and return it.
<dt>U+0023 NUMBER SIGN (#)
<dd>
If the <a>next input code point</a> is an <a>identifier code point</a>
or the <a lt="next input code point">next two input code points</a>
<a>are a valid escape</a>,
then:
<ol>
<li>
Create a <<hash-token>>.
<li>
If the <a lt="next input code point">next 3 input code points</a> <a>would start an identifier</a>,
set the <<hash-token>>’s type flag to "id".
<li>
<a>Consume an identifier</a>,
and set the <<hash-token>>’s value to the returned string.
<li>
Return the <<hash-token>>.
</ol>
Otherwise,
return a <<delim-token>>
with its value set to the <a>current input code point</a>.
<dt>U+0027 APOSTROPHE (&apos;)
<dd>
<a>Consume a string token</a>
and return it.
<dt>U+0028 LEFT PARENTHESIS (()
<dd>
Return a <a href="#tokendef-open-paren">&lt;(-token></a>.
<dt>U+0029 RIGHT PARENTHESIS ())
<dd>
Return a <a href="#tokendef-close-paren">&lt;)-token></a>.
<dt>U+002B PLUS SIGN (+)
<dd>
If the input stream <a>starts with a number</a>,
<a>reconsume the current input code point</a>,
<a>consume a numeric token</a>,
and return it.
Otherwise,
return a <<delim-token>>
with its value set to the <a>current input code point</a>.
<dt>U+002C COMMA (,)
<dd>
Return a <<comma-token>>.
<dt>U+002D HYPHEN-MINUS (-)
<dd>
If the input stream <a>starts with a number</a>,
<a>reconsume the current input code point</a>,
<a>consume a numeric token</a>,
and return it.
Otherwise,
if the <a lt="next input code point">next 2 input code points</a> are
U+002D HYPHEN-MINUS
U+003E GREATER-THAN SIGN
(->),
consume them
and return a <<CDC-token>>.
Otherwise,
if the input stream <a>starts with an identifier</a>,
<a>reconsume the current input code point</a>,
<a>consume an ident-like token</a>,
and return it.
Otherwise,
return a <<delim-token>>
with its value set to the <a>current input code point</a>.
<dt>U+002E FULL STOP (.)
<dd>
If the input stream <a>starts with a number</a>,
<a>reconsume the current input code point</a>,
<a>consume a numeric token</a>,
and return it.
Otherwise,
return a <<delim-token>>
with its value set to the <a>current input code point</a>.
<dt>U+003A COLON (:)
<dd>
Return a <<colon-token>>.
<dt>U+003B SEMICOLON (;)
<dd>
Return a <<semicolon-token>>.
<dt>U+003C LESS-THAN SIGN (&lt;)
<dd>
If the <a lt="next input code point">next 3 input code points</a> are
U+0021 EXCLAMATION MARK
U+002D HYPHEN-MINUS
U+002D HYPHEN-MINUS
(!--),
consume them
and return a <<CDO-token>>.
Otherwise,
return a <<delim-token>>
with its value set to the <a>current input code point</a>.
<dt>U+0040 COMMERCIAL AT (@)
<dd>
If the <a lt="next input code point">next 3 input code points</a>
<a>would start an identifier</a>,
<a>consume an identifier</a>,
create an <<at-keyword-token>> with its value set to the returned value,
and return it.
Otherwise,
return a <<delim-token>>
with its value set to the <a>current input code point</a>.
<dt>U+005B LEFT SQUARE BRACKET ([)
<dd>
Return a <a href="#tokendef-open-square">&lt;[-token></a>.
<dt>U+005C REVERSE SOLIDUS (\)
<dd>
If the input stream <a>starts with a valid escape</a>,
<a>reconsume the current input code point</a>,
<a>consume an ident-like token</a>,
and return it.
Otherwise,
this is a <a>parse error</a>.
Return a <<delim-token>>
with its value set to the <a>current input code point</a>.
<dt>U+005D RIGHT SQUARE BRACKET (])
<dd>
Return a <a href="#tokendef-close-square">&lt;]-token></a>.
<dt>U+007B LEFT CURLY BRACKET ({)
<dd>
Return a <a href="#tokendef-open-curly">&lt;{-token></a>.
<dt>U+007D RIGHT CURLY BRACKET (})
<dd>
Return a <a href="#tokendef-close-curly">&lt;}-token></a>.
<dt><a>digit</a>
<dd>
<a>Reconsume the current input code point</a>,
<a>consume a numeric token</a>,
and return it.
<dt><a>identifier-start code point</a>
<dd>
<a>Reconsume the current input code point</a>,
<a>consume an ident-like token</a>,
and return it.
<dt>EOF
<dd>
Return an <<EOF-token>>.
<dt>anything else
<dd>
Return a <<delim-token>>
with its value set to the <a>current input code point</a>.
</dl>
<h4 id="consume-comment">
Consume comments</h4>
This section describes how to <dfn>consume comments</dfn> from a stream of <a>code points</a>.
It returns nothing.
If the <a lt="next input code point">next two input code point</a> are
U+002F SOLIDUS (/) followed by a U+002A ASTERISK (*),
consume them
and all following <a>code points</a> up to and including
the first U+002A ASTERISK (*) followed by a U+002F SOLIDUS (/),
or up to an EOF code point.
Return to the start of this step.
If the preceding paragraph ended by consuming an EOF code point,
this is a <a>parse error</a>.
Return nothing.
<h4 id="consume-numeric-token">
Consume a numeric token</h4>
This section describes how to <dfn>consume a numeric token</dfn> from a stream of <a>code points</a>.
It returns either a <<number-token>>, <<percentage-token>>, or <<dimension-token>>.
<a>Consume a number</a> and let |number| be the result.
If the <a lt="next input code point">next 3 input code points</a> <a>would start an identifier</a>,
then:
<ol>
<li>Create a <<dimension-token>> with the same value and type flag as |number|,
and a unit set initially to the empty string.
<li><a>Consume an identifier</a>.
Set the <<dimension-token>>’s unit to the returned value.
<li>Return the <<dimension-token>>.
</ol>
Otherwise,
if the <a>next input code point</a> is U+0025 PERCENTAGE SIGN (%),
consume it.
Create a <<percentage-token>> with the same value and type flag as |number|,
and return it.
Otherwise,
create a <<number-token>> with the same value and type flag as |number|,
and return it.
<h4 id="consume-ident-like-token">
Consume an ident-like token</h4>
This section describes how to <dfn>consume an ident-like token</dfn> from a stream of <a>code points</a>.
It returns an <<ident-token>>, <<function-token>>, <<url-token>>, or <<bad-url-token>>.
<a>Consume an identifier</a>, and let |string| be the result.
If |string|’s value is an <a>ASCII case-insensitive</a> match for "url",
and the <a>next input code point</a> is U+0028 LEFT PARENTHESIS ((),
consume it.
While the <a lt="next input code point">next two input code points</a> are <a>whitespace</a>,
consume the <a>next input code point</a>.
If the <a lt="next input code point">next one or two input code points</a> are U+0022 QUOTATION MARK ("),
U+0027 APOSTROPHE (&apos;),
or <a>whitespace</a> followed by U+0022 QUOTATION MARK (") or U+0027 APOSTROPHE (&apos;),
then create a <<function-token>>
with its value set to |string|
and return it.
Otherwise,
<a>consume a url token</a>,
and return it.
Otherwise,
if the <a>next input code point</a> is U+0028 LEFT PARENTHESIS ((),
consume it.
Create a <<function-token>>
with its value set to |string|
and return it.
Otherwise,
create an <<ident-token>>
with its value set to |string|
and return it.
<h4 id="consume-string-token">
Consume a string token</h4>
This section describes how to <dfn>consume a string token</dfn> from a stream of <a>code points</a>.
It returns either a <<string-token>> or <<bad-string-token>>.
This algorithm may be called with an <var>ending code point</var>,
which denotes the <a>code point</a> that ends the string.
If an <var>ending code point</var> is not specified,
the <a>current input code point</a> is used.
Initially create a <<string-token>> with its value set to the empty string.
Repeatedly consume the <a>next input code point</a> from the stream:
<dl>
<dt><var>ending code point</var>
<dd>
Return the <<string-token>>.
<dt>EOF
<dd>
This is a <a>parse error</a>.
Return the <<string-token>>.
<dt><a>newline</a>
<dd>
This is a <a>parse error</a>.
<a>Reconsume the current input code point</a>,
create a <<bad-string-token>>, and return it.
<dt>U+005C REVERSE SOLIDUS (\)
<dd>
If the <a>next input code point</a> is EOF,
do nothing.
Otherwise,
if the <a>next input code point</a> is a newline,
consume it.
Otherwise,
<span class=note>(the stream <a>starts with a valid escape</a>)</span>
<a>consume an escaped code point</a>
and append the returned <a>code point</a> to the <<string-token>>’s value.
<dt>anything else
<dd>
Append the <a>current input code point</a> to the <<string-token>>’s value.
</dl>
<h4 id="consume-url-token">
Consume a url token</h4>
This section describes how to <dfn>consume a url token</dfn> from a stream of <a>code points</a>.
It returns either a <<url-token>> or a <<bad-url-token>>.
Note: This algorithm assumes that the initial "url(" has already been consumed.
This algorithm also assumes that it's being called to consume an "unquoted" value,
like ''url(foo)''.
A quoted value, like ''url("foo")'',
is parsed as a <<function-token>>.
<a>Consume an ident-like token</a> automatically handles this distinction;
this algorithm shouldn't be called directly otherwise.
<ol>
<li>
Initially create a <<url-token>> with its value set to the empty string.
<li>
Consume as much <a>whitespace</a> as possible.
<li>
Repeatedly consume the <a>next input code point</a> from the stream:
<dl>
<dt>U+0029 RIGHT PARENTHESIS ())
<dd>
Return the <<url-token>>.
<dt>EOF
<dd>
This is a <a>parse error</a>.
Return the <<url-token>>.
<dt><a>whitespace</a>
<dd>
Consume as much <a>whitespace</a> as possible.
If the <a>next input code point</a> is U+0029 RIGHT PARENTHESIS ()) or EOF,
consume it and return the <<url-token>>
(if EOF was encountered, this is a <a>parse error</a>);
otherwise,
<a>consume the remnants of a bad url</a>,
create a <<bad-url-token>>,
and return it.
<dt>U+0022 QUOTATION MARK (")
<dt>U+0027 APOSTROPHE (&apos;)
<dt>U+0028 LEFT PARENTHESIS (()
<dt><a>non-printable code point</a>
<dd>
This is a <a>parse error</a>.
<a>Consume the remnants of a bad url</a>,
create a <<bad-url-token>>,
and return it.
<dt>U+005C REVERSE SOLIDUS (\)
<dd>
If the stream <a>starts with a valid escape</a>,
<a>consume an escaped code point</a>
and append the returned <a>code point</a> to the <<url-token>>’s value.
Otherwise,
this is a <a>parse error</a>.
<a>Consume the remnants of a bad url</a>,
create a <<bad-url-token>>,
and return it.
<dt>anything else
<dd>
Append the <a>current input code point</a>
to the <<url-token>>’s value.
</dl>
</ol>
<h4 id="consume-escaped-code-point">
Consume an escaped code point</h4>
This section describes how to <dfn>consume an escaped code point</dfn>.
It assumes that the U+005C REVERSE SOLIDUS (\) has already been consumed
and that the next input code point has already been verified
to be part of a valid escape.
It will return a <a>code point</a>.
Consume the <a>next input code point</a>.
<dl>
<dt><a>hex digit</a>
<dd>
Consume as many <a>hex digits</a> as possible, but no more than 5.
<span class='note'>Note that this means 1-6 hex digits have been consumed in total.</span>
If the <a>next input code point</a> is
<a>whitespace</a>,
consume it as well.
Interpret the <a>hex digits</a> as a hexadecimal number.
If this number is zero,
or is for a <a>surrogate</a>,
or is greater than the <a>maximum allowed code point</a>,
return U+FFFD REPLACEMENT CHARACTER (�).
Otherwise, return the <a>code point</a> with that value.
<dt>EOF
<dd>
This is a <a>parse error</a>.
Return U+FFFD REPLACEMENT CHARACTER (�).
<dt>anything else
<dd>
Return the <a>current input code point</a>.
</dl>
<h4 id="starts-with-a-valid-escape">
Check if two code points are a valid escape</h4>
This section describes how to <dfn lt="check if two code points are a valid escape|are a valid escape|starts with a valid escape">check if two code points are a valid escape</dfn>.
The algorithm described here can be called explicitly with two <a>code points</a>,
or can be called with the input stream itself.
In the latter case, the two <a>code points</a> in question are
the <a>current input code point</a>
and the <a>next input code point</a>,
in that order.
Note: This algorithm will not consume any additional <a>code point</a>.
If the first <a>code point</a> is not U+005C REVERSE SOLIDUS (\),
return false.
Otherwise,
if the second <a>code point</a> is a <a>newline</a>,
return false.
Otherwise, return true.
<h4 id="would-start-an-identifier">
Check if three code points would start an identifier</dfn></h4>
This section describes how to <dfn lt="check if three code points would start an identifier|starts with an identifier|start with an identifier|would start an identifier">check if three code points would start an <a>identifier</a></dfn>.
The algorithm described here can be called explicitly with three <a>code points</a>,
or can be called with the input stream itself.
In the latter case, the three <a>code points</a> in question are
the <a>current input code point</a>
and the <a lt="next input code point">next two input code points</a>,
in that order.
Note: This algorithm will not consume any additional <a>code points</a>.
Look at the first <a>code point</a>:
<dl>
<dt>U+002D HYPHEN-MINUS
<dd>
If the second <a>code point</a> is an <a>identifier-start code point</a>
or a U+002D HYPHEN-MINUS,
or the second and third <a>code points</a> <a>are a valid escape</a>,
return true.
Otherwise, return false.
<dt><a>identifier-start code point</a>
<dd>
Return true.
<dt>U+005C REVERSE SOLIDUS (\)
<dd>
If the first and second <a>code points</a> <a>are a valid escape</a>,
return true.
Otherwise, return false.
<dt>anything else
<dd>
Return false.
</dl>
<h4 id="starts-with-a-number">
Check if three code points would start a number</h4>
This section describes how to <dfn lt="check if three code points would start a number|starts with a number|start with a number|would start a number">check if three code points would start a number</dfn>.
The algorithm described here can be called explicitly with three <a>code points</a>,
or can be called with the input stream itself.
In the latter case, the three <a>code points</a> in question are
the <a>current input code point</a>
and the <a lt="next input code point">next two input code points</a>,
in that order.
Note: This algorithm will not consume any additional <a>code points</a>.
Look at the first <a>code point</a>:
<dl>
<dt>U+002B PLUS SIGN (+)
<dt>U+002D HYPHEN-MINUS (-)
<dd>
If the second <a>code point</a>
is a <a>digit</a>,
return true.
Otherwise,
if the second <a>code point</a>
is a U+002E FULL STOP (.)
and the third <a>code point</a>
is a <a>digit</a>,
return true.
Otherwise, return false.
<dt>U+002E FULL STOP (.)
<dd>
If the second <a>code point</a>
is a <a>digit</a>,
return true.
Otherwise, return false.
<dt><a>digit</a>
<dd>
Return true.
<dt>anything else
<dd>
Return false.
</dl>
<h4 id="consume-name">
Consume an identifier</h4>
This section describes how to <dfn>consume an identifier</dfn> from a stream of <a>code points</a>.
It returns a string containing
the largest name that can be formed from adjacent <a>code points</a> in the stream, starting from the first.
Note: This algorithm does not do the verification of the first few <a>code points</a>
that are necessary to ensure the returned <a>code points</a> would constitute an <<ident-token>>.
If that is the intended use,
ensure that the stream <a>starts with an identifier</a>
before calling this algorithm.
Let <var>result</var> initially be an empty string.
Repeatedly consume the <a>next input code point</a> from the stream:
<dl>
<dt><a>identifier code point</a>
<dd>
Append the <a>code point</a> to <var>result</var>.
<dt>the stream <a>starts with a valid escape</a>
<dd>
<a>Consume an escaped code point</a>.
Append the returned <a>code point</a> to <var>result</var>.
<dt>anything else
<dd>
<a>Reconsume the current input code point</a>.
Return <var>result</var>.
</dl>
<h4 id="consume-number">
Consume a number</h4>
This section describes how to <dfn>consume a number</dfn> from a stream of <a>code points</a>.
It returns a numeric |value|,
and a |type| which is either "integer" or "number".
Note: This algorithm does not do the verification of the first few <a>code points</a>
that are necessary to ensure a number can be obtained from the stream.
Ensure that the stream <a>starts with a number</a>
before calling this algorithm.
Execute the following steps in order:
<ol>
<li>
Initially set <var>type</var> to "integer".
Let |repr| be the empty string.
<li>
If the <a>next input code point</a> is U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-),
consume it and append it to <var>repr</var>.
<li>
While the <a>next input code point</a> is a <a>digit</a>,
consume it and append it to <var>repr</var>.
<li>
If the <a lt="next input code point">next 2 input code points</a> are
U+002E FULL STOP (.) followed by a <a>digit</a>,
then:
<ol>
<li>Consume them.
<li>Append them to <var>repr</var>.
<li>Set <var>type</var> to "number".
<li>While the <a>next input code point</a> is a <a>digit</a>, consume it and append it to <var>repr</var>.
</ol>
<li>
If the <a lt="next input code point">next 2 or 3 input code points</a> are
U+0045 LATIN CAPITAL LETTER E (E) or U+0065 LATIN SMALL LETTER E (e),
optionally followed by U+002D HYPHEN-MINUS (-) or U+002B PLUS SIGN (+),
followed by a <a>digit</a>,
then:
<ol>
<li>Consume them.
<li>Append them to <var>repr</var>.
<li>Set <var>type</var> to "number".
<li>While the <a>next input code point</a> is a <a>digit</a>, consume it and append it to <var>repr</var>.
</ol>
<li>
<a lt="convert a string to a number">Convert <var>repr</var> to a number</a>,
and set the <var>value</var> to the returned value.
<li>
Return <var>value</var> and <var>type</var>.
</ol>
<h4 id="convert-string-to-number">
Convert a string to a number</h4>
This section describes how to <dfn>convert a string to a number</dfn>.
It returns a number.
Note: This algorithm does not do any verification to ensure that the string contains only a number.
Ensure that the string contains only a valid CSS number
before calling this algorithm.
Divide the string into seven components,
in order from left to right:
<ol>
<li>A <b>sign</b>:
a single U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-),
or the empty string.
Let <var>s</var> be the number -1 if the sign is U+002D HYPHEN-MINUS (-);
otherwise, let <var>s</var> be the number 1.
<li>An <b>integer part</b>:
zero or more <a>digits</a>.
If there is at least one digit,
let <var>i</var> be the number formed by interpreting the digits as a base-10 integer;
otherwise, let <var>i</var> be the number 0.
<li>A <b>decimal point</b>:
a single U+002E FULL STOP (.),
or the empty string.
<li>A <b>fractional part</b>:
zero or more <a>digits</a>.
If there is at least one digit,
let <var>f</var> be the number formed by interpreting the digits as a base-10 integer
and <var>d</var> be the number of digits;
otherwise, let <var>f</var> and <var>d</var> be the number 0.
<li>An <b>exponent indicator</b>:
a single U+0045 LATIN CAPITAL LETTER E (E) or U+0065 LATIN SMALL LETTER E (e),
or the empty string.
<li>An <b>exponent sign</b>:
a single U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-),
or the empty string.
Let <var>t</var> be the number -1 if the sign is U+002D HYPHEN-MINUS (-);
otherwise, let <var>t</var> be the number 1.
<li>An <b>exponent</b>:
zero or more <a>digits</a>.
If there is at least one digit,
let <var>e</var> be the number formed by interpreting the digits as a base-10 integer;
otherwise, let <var>e</var> be the number 0.
</ol>
Return the number <code>s·(i + f·10<sup>-d</sup>)·10<sup>te</sup></code>.
<h4 id="consume-remnants-of-bad-url">
Consume the remnants of a bad url</h4>
This section describes how to <dfn>consume the remnants of a bad url</dfn> from a stream of <a>code points</a>,
"cleaning up" after the tokenizer realizes that it's in the middle of a <<bad-url-token>> rather than a <<url-token>>.
It returns nothing;
its sole use is to consume enough of the input stream to reach a recovery point
where normal tokenizing can resume.
Repeatedly consume the <a>next input code point</a> from the stream:
<dl>
<dt>U+0029 RIGHT PARENTHESIS ())
<dt>EOF
<dd>
Return.
<dt>the input stream <a>starts with a valid escape</a>
<dd>
<a>Consume an escaped code point</a>.
<span class='note'>This allows an escaped right parenthesis ("\)") to be encountered without ending the <<bad-url-token>>.
This is otherwise identical to the "anything else" clause.</span>
<dt>anything else
<dd>
Do nothing.
</dl>
<!--
████████ ███ ████████ ██████ ████████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██ ██ ████████ ██████ ██████ ████████
██ █████████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██████ ████████ ██ ██
-->
<h2 id="parsing">
Parsing</h2>
The input to the parsing stage is a stream or list of tokens from the tokenization stage.
The output depends on how the parser is invoked,
as defined by the entry points listed later in this section.
The parser output can consist of at-rules,
qualified rules,
and/or declarations.
The parser's output is constructed according to the fundamental syntax of CSS,
without regards for the validity of any specific item.
Implementations may check the validity of items as they are returned by the various parser algorithms
and treat the algorithm as returning nothing if the item was invalid according to the implementation's own grammar knowledge,
or may construct a full tree as specified
and "clean up" afterwards by removing any invalid items.
The items that can appear in the tree are:
<dl export dfn-for=CSS>
<dt><dfn id="at-rule">at-rule</dfn>
<dd>
An at-rule has a name,
a prelude consisting of a list of component values,
and an optional block consisting of a simple {} block.
Note: This specification places no limits on what an at-rule's block may contain.
Individual at-rules must define whether they accept a block,
and if so,
how to parse it
(preferably using one of the parser algorithms or entry points defined in this specification).
<dt><dfn id="qualified-rule">qualified rule</dfn>
<dd>
A qualified rule has
a prelude consisting of a list of component values,
and a block consisting of a simple {} block.
Note: Most qualified rules will be style rules,
where the prelude is a selector [[SELECT]]
and the block a <a lt="parse a list of declarations">list of declarations</a>.
<dt><dfn id="declaration">declaration</dfn>
<dd>
A declaration has a name,
a value consisting of a list of component values,
and an <var>important</var> flag which is initially unset.
Declarations are further categorized as "properties" or "descriptors",
with the former typically appearing in <a>qualified rules</a>
and the latter appearing in <a>at-rules</a>.
(This categorization does not occur at the Syntax level;
instead, it is a product of where the declaration appears,
and is defined by the respective specifications defining the given rule.)
<dt><dfn id="component-value">component value</dfn>
<dd>
A component value is one of the [=preserved tokens=],
a [=function=],
or a [=simple block=].
<dt><dfn id="preserved-tokens">preserved tokens</dfn>
<dd>
Any token produced by the tokenizer
except for <<function-token>>s,
<a href="#tokendef-open-curly">&lt;{-token></a>s,
<a href="#tokendef-open-paren">&lt;(-token></a>s,
and <a href="#tokendef-open-square">&lt;[-token></a>s.
Note: The non-[=preserved tokens=] listed above are always consumed into higher-level objects,
either functions or simple blocks,
and so never appear in any parser output themselves.
Note: The tokens <a href="#tokendef-close-curly">&lt;}-token></a>s, <a href="#tokendef-close-paren">&lt;)-token></a>s, <a href="#tokendef-close-square">&lt;]-token></a>, <<bad-string-token>>, and <<bad-url-token>> are always parse errors,
but they are preserved in the token stream by this specification to allow other specs,
such as Media Queries,
to define more fine-grained error-handling
than just dropping an entire declaration or block.
<dt><dfn id="function">function</dfn>
<dd>
A function has a name
and a value consisting of a list of component values.
<dt><dfn id="simple-block">simple block</dfn>
<dd>
A simple block has an associated token (either a <a href="#tokendef-open-square">&lt;[-token></a>, <a href="#tokendef-open-paren">&lt;(-token></a>, or <a href="#tokendef-open-curly">&lt;{-token></a>)
and a value consisting of a list of component values.
</dl>
<!--
████████ ███ ████ ██ ████████ ███████ ███ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██ ██ ██ ██ ████████ ██ ██ ██ ██ ██ ██
██ ██ █████████ ██ ██ ██ ██ ██ ██ █████████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████ ████████ ██ ██ ███████ ██ ██ ████████
-->
<h3 id='parser-diagrams'>
Parser Railroad Diagrams</h3>
<em>This section is non-normative.</em>
This section presents an informative view of the parser,
in the form of railroad diagrams.
These diagrams are <em>informative</em> and <em>incomplete</em>;
they describe the grammar of "correct" stylesheets,
but do not describe error-handling at all.
They are provided solely to make it easier to get an intuitive grasp of the syntax.
<dl>
<dt id="stylesheet-diagram">Stylesheet
<dd>
<pre class='railroad'>
Star:
Choice: 3
N: <CDO-token>
N: <CDC-token>
N: <whitespace-token>
N: Qualified rule
N: At-rule
</pre>
<dt id="rule-list-diagram">Rule list
<dd>
<pre class='railroad'>
Star:
Choice: 1
N: <whitespace-token>
N: Qualified rule
N: At-rule
</pre>
<dt id="at-rule-diagram">At-rule
<dd>
<pre class='railroad'>
N: <at-keyword-token>
Star:
N: Component value
Choice:
N: {} block
T: ;
</pre>
<dt id="qualified-rule-diagram">Qualified rule
<dd>
<pre class='railroad'>
Star:
N: Component value
N: {} block
</pre>
<dt id="declaration-list-diagram">Declaration list
<dd>
<pre class='railroad'>
N: ws*
Choice:
Seq:
Opt:
N: Declaration
Opt:
Seq:
T: ;
N: Declaration list
Seq:
N: At-rule
N: Declaration list
</pre>
<dt id="declaration-diagram">Declaration
<dd>
<pre class='railroad'>
N: <ident-token>
N: ws*
T: :
Star:
N: Component value
Opt: skip
N: !important
</pre>
<dt id="!important-diagram">!important
<dd>
<pre class='railroad'>
T: !
N: ws*
N: <ident-token "important">
N: ws*
</pre>
<dt id="component-value-diagram">Component value
<dd>
<pre class='railroad'>
Choice:
N: Preserved token
N: {} block
N: () block
N: [] block
N: Function block
</pre>
<dt id="{}-block-diagram">{} block
<dd>
<pre class='railroad'>
T: {
Star:
N: Component value
T: }
</pre>
<dt id="()-block-diagram">() block
<dd>
<pre class='railroad'>
T: (
Star:
N: Component value
T: )
</pre>
<dt id="[]-block-diagram">[] block
<dd>
<pre class='railroad'>
T: [
Star:
N: Component value
T: ]
</pre>
<dt id="function-block-diagram">Function block
<dd>
<pre class='railroad'>
N: <function-token>
Star:
N: Component value
T: )
</pre>
</dl>
<!--
████████ ████████ ██ ██ ██████
██ ██ ██ ███ ██ ██ ██
██ ██ ██ ████ ██ ██
██ ██ ██████ ██ ██ ██ ██████
██ ██ ██ ██ ████ ██
██ ██ ██ ██ ███ ██ ██
████████ ██ ██ ██ ██████
-->
<h3 id="parser-definitions">
Definitions</h3>
<dl>
<dt><dfn>current input token</dfn>
<dd>
The token or <a>component value</a> currently being operated on, from the list of tokens produced by the tokenizer.
<dt><dfn>next input token</dfn>
<dd>
The token or <a>component value</a> following the <a>current input token</a> in the list of tokens produced by the tokenizer.
If there isn't a token following the <a>current input token</a>,
the <a>next input token</a> is an <<EOF-token>>.
<dt><dfn><<EOF-token>></dfn>
<dd>
A conceptual token representing the end of the list of tokens.
Whenever the list of tokens is empty,
the <a>next input token</a> is always an <<EOF-token>>.
<dt><dfn>consume the next input token</dfn>
<dd>
Let the <a>current input token</a> be the current <a>next input token</a>,
adjusting the <a>next input token</a> accordingly.
<dt><dfn>reconsume the current input token</dfn>
<dd>
The next time an algorithm instructs you to <a>consume the next input token</a>,
instead do nothing
(retain the <a>current input token</a> unchanged).
</dl>
<!--
████████ ██ ██ ████████ ████████ ██ ██ ████████ ███████ ████ ██ ██ ████████ ██████
██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
██ ████ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ████ ██ ██ ██
██████ ██ ██ ██ ██ ████████ ██ ████████ ██ ██ ██ ██ ██ ██ ██ ██████
██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
████████ ██ ██ ██ ██ ██ ██ ██ ███████ ████ ██ ██ ██ ██████
-->
<h3 id="parser-entry-points">
Parser Entry Points</h3>
The algorithms defined in this section produce high-level CSS objects
from lists of CSS tokens.
<div algorithm="normalize into a list of tokens">
The algorithms here are operate on a token stream as input,
but for convenience
can also be invoked with a number of other value types.
To <dfn export local-lt="normalize">normalize into a token stream</dfn> a given |input|:
1. If |input| is a list of CSS tokens,
return |input|.
2. If |input| is a list of CSS component values,
return |input|.
Note: The only difference between a list of tokens
and a list of component values
is that some objects that "contain" things,
like functions or blocks,
are a single entity in the component-value list,
but are multiple entities in a token list.
This makes no difference to any of the algorithms in this specification.
3. If |input| is a [=string=],
then [=filter code points=] from |input|,
[=tokenize=] the result,
and return the final result.
4. Assert: Only the preceding types should be passed as |input|.
</div>
Note: Other specs can define additional entry points for their own purposes.
<div class='note'>
The following notes should probably be translated into normative text in the relevant specs,
hooking this spec's terms:
<ul>
<li>
"<a>Parse a stylesheet</a>" is intended to be the normal parser entry point,
for parsing stylesheets.
<li>
"<a>Parse a list of rules</a>" is intended for the content of at-rules such as ''@media''.
It differs from "<a>Parse a stylesheet</a>" in the handling of <<CDO-token>> and <<CDC-token>>.
<li>
"<a>Parse a rule</a>" is intended for use by the <code>CSSStyleSheet#insertRule</code> method,
and similar functions which might exist,
which parse text into a single rule.
<li>
"<a>Parse a declaration</a>" is used in ''@supports'' conditions. [[CSS3-CONDITIONAL]]
<li>
"<a>Parse a list of declarations</a>" is for the contents of a <code>style</code> attribute,
which parses text into the contents of a single style rule.
<li>
"<a>Parse a component value</a>" is for things that need to consume a single value,
like the parsing rules for ''attr()''.
<li>
"<a>Parse a list of component values</a>" is for the contents of presentational attributes,
which parse text into a single declaration's value,
or for parsing a stand-alone selector [[SELECT]] or list of Media Queries [[MEDIAQ]],
as in <a href="https://www.w3.org/TR/selectors-api/">Selectors API</a>
or the <code>media</code> HTML attribute.
</ul>
</div>
<h4 id="parse-grammar">
Parse something according to a CSS grammar</h4>
It is often desirable to parse a string or token list
to see if it matches some CSS grammar,
and if it does,
to destructure it according to the grammar.
This section provides a generic hook for this kind of operation.
It should be invoked like
<span class="informative">"parse <var>foo</var> as a CSS <<color>>"</span>, or similar.
This algorithm returns either failure,
if the input does not match the provided grammar,
or the result of parsing the input according to the grammar,
which is an unspecified structure corresponding to the provided grammar specification.
The return value must only be interacted with by specification prose,
where the representation ambiguity is not problematic.
If it is meant to be exposed outside of spec language,
the spec using the result must explicitly translate it into a well-specified representation,
such as, for example, by invoking a CSS serialization algorithm
<span class=informative>(like "serialize as a CSS <<string>> value").</span>
Note: This algorithm,
and [=parse a comma-separated list according to a CSS grammar=],
are <em>usually</em> the only parsing algorithms other specs will want to call.
The remaining parsing algorithms are meant mostly for CSSOM
and related "explicitly constructing CSS structures" cases.
Consult the CSSWG for guidance first
if you think you need to use one of the other algorithms.
<div algorithm>
To <dfn export lt="parse something according to a CSS grammar|parse" for=CSS>parse something according to a CSS grammar</dfn>
(aka simply [=CSS/parse=])
given an |input|
and a CSS |grammar| production:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
<a>Parse a list of component values</a> from |input|,
and let <var>result</var> be the return value.
<li>
Attempt to match <var>result</var> against |grammar|.
If this is successful,
return the matched result;
otherwise, return failure.
</ol>
</div>
<h4 id="parse-comma-list">
Parse A Comma-Separated List According To A CSS Grammar</h4>
While one can definitely [=CSS/parse=] a value
according to a grammar with commas in it,
if <em>any</em> part of the value fails to parse,
the entire thing doesn't parse,
and returns failure.
Sometimes that's what's desired
(such as in list-valued CSS properties);
other times,
it's better to let each comma-separated sub-part of the value parse separately,
dealing with the parts that parse successfully one way,
and the parts that fail to parse another way
(typically ignoring them,
such as in <{img/sizes|&lt;img sizes&gt;}>).
This algorithm provides an easy hook to accomplish exactly that.
It returns a list of values split by "top-level" commas,
where each values is either failure
(if it failed to parse)
or the result of parsing
(an unspecified structure,
as described in the [=CSS/parse=] algorithm).
<div algorithm>
To <dfn export lt="parse a comma-separated list according to a CSS grammar|parse a list|parsing a list" for=CSS>parse a comma-separated list according to a CSS grammar</dfn>
(aka [=CSS/parse a list=])
given an |input|
and a CSS |grammar| production:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
If |input| contains only <<whitespace-token>>s,
return an empty [=list=].
<li>
<a>Parse a comma-separated list of component values</a> from |input|,
and let <var>list</var> be the return value.
<li>
[=list/For each=] |item| of |list|,
replace |item| with the result of
[=CSS/parsing=] |item| with |grammar|.
<li>
Return |list|.
</ol>
</div>
<h4 id="parse-stylesheet">
Parse a stylesheet</h4>
<div algorithm>
To <dfn export>parse a stylesheet</dfn> from an |input|:
<ol>
<li>
If |input| is a byte stream for stylesheet,
[=decode bytes=] from |input|,
and set |input| to the result.
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
Create a new stylesheet.
<li>
<a>Consume a list of rules</a> from |input|,
with the top-level flag set,
and set the stylesheet's value to the result.
<li>
Return the stylesheet.
</ol>
</div>
<h4 id="parse-list-of-rules">
Parse a list of rules</h4>
<div algorithm>
To <dfn export>parse a list of rules</dfn> from |input|:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
<a>Consume a list of rules</a> from the |input|,
with the <var ignore>top-level flag</var> unset.
<li>
Return the returned list.
</ol>
</div>
<h4 id="parse-rule">
Parse a rule</h4>
<div algorithm>
To <dfn export>parse a rule</dfn> from |input|:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
While the <a>next input token</a> from |input| is a <<whitespace-token>>,
<a>consume the next input token</a> from |input|.
<li>
If the <a>next input token</a> from |input| is an <<EOF-token>>,
return a syntax error.
Otherwise,
if the <a>next input token</a> from |input| is an <<at-keyword-token>>,
<a>consume an at-rule</a> from |input|,
and let <var>rule</var> be the return value.
Otherwise,
<a>consume a qualified rule</a> from |input|
and let <var>rule</var> be the return value.
If nothing was returned,
return a syntax error.
<li>
While the <a>next input token</a> from |input| is a <<whitespace-token>>,
<a>consume the next input token</a> from |input|.
<li>
If the <a>next input token</a> from |input| is an <<EOF-token>>,
return <var>rule</var>.
Otherwise, return a syntax error.
</ol>
</div>
<h4 id="parse-declaration">
Parse a declaration</h4>
Note: Unlike "<a>Parse a list of declarations</a>",
this parses only a declaration and not an at-rule.
<div algorithm>
To <dfn export>parse a declaration</dfn> from |input|:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
While the <a>next input token</a> from |input| is a <<whitespace-token>>,
<a>consume the next input token</a>.
<li>
If the <a>next input token</a> from |input| is not an <<ident-token>>,
return a syntax error.
<li>
<a>Consume a declaration</a> from |input|.
If anything was returned, return it.
Otherwise, return a syntax error.
</ol>
</div>
<h4 id="parse-list-of-declarations">
Parse a list of declarations</h4>
Note: Despite the name,
this actually parses a mixed list of declarations and at-rules,
as CSS 2.1 does for ''@page''.
Unexpected at-rules (which could be all of them, in a given context)
are invalid and will be ignored by the consumer.
<div algorithm>
To <dfn export>parse a list of declarations</dfn> from |input|:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
<a>Consume a list of declarations</a> from |input|,
and return the result.
</ol>
</div>
<h4 id="parse-component-value">
Parse a component value</h4>
<div algorithm>
To <dfn export>parse a component value</dfn> from |input|:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
While the <a>next input token</a> from |input| is a <<whitespace-token>>,
<a>consume the next input token</a> from |input|.
<li>
If the <a>next input token</a> from |input| is an <<EOF-token>>,
return a syntax error.
<li>
<a>Consume a component value</a> from |input|
and let <var>value</var> be the return value.
<li>
While the <a>next input token</a> from |input| is a <<whitespace-token>>,
<a>consume the next input token</a>.
<li>
If the <a>next input token</a> from |input| is an <<EOF-token>>,
return <var>value</var>.
Otherwise,
return a syntax error.
</ol>
</div>
<h4 id="parse-list-of-component-values">
Parse a list of component values</h4>
<div algorithm>
To <dfn export>parse a list of component values</dfn> from |input|:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
Repeatedly <a>consume a component value</a> from |input| until an <<EOF-token>> is returned,
appending the returned values (except the final <<EOF-token>>) into a list.
Return the list.
</ol>
</div>
<h4 id="parse-comma-separated-list-of-component-values">
Parse a comma-separated list of component values</h4>
<div algorithm>
To <dfn export>parse a comma-separated list of component values</dfn> from |input|:
<ol>
<li>
[=Normalize=] |input|,
and set |input| to the result.
<li>
Let <var>list of cvls</var> be an initially empty list of component value lists.
<li>
Repeatedly <a>consume a component value</a> from |input| until an <<EOF-token>> or <<comma-token>> is returned,
appending the returned values (except the final <<EOF-token>> or <<comma-token>>) into a list.
Append the list to <var>list of cvls</var>.
If it was a <<comma-token>> that was returned,
repeat this step.
<li>
Return <var>list of cvls</var>.
</ol>
</div>
<!--
███ ██ ██████ ███████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████ ██ ██ ██████
█████████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ████████ ██████ ███████ ██████
-->
<h3 id="parser-algorithms">
Parser Algorithms</h3>
The following algorithms comprise the parser.
They are called by the parser entry points above.
These algorithms may be called with a list of either tokens or of component values.
(The difference being that some tokens are replaced by <a>functions</a> and <a>simple blocks</a> in a list of component values.)
Similar to how the input stream returned EOF code points to represent when it was empty during the tokenization stage,
the lists in this stage must return an <<EOF-token>> when the next token is requested but they are empty.
An algorithm may be invoked with a specific list,
in which case it consumes only that list
(and when that list is exhausted,
it begins returning <<EOF-token>>s).
Otherwise,
it is implicitly invoked with the same list as the invoking algorithm.
<h4 id="consume-list-of-rules">
Consume a list of rules</h4>
To <dfn>consume a list of rules</dfn>:
Create an initially empty list of rules.
Repeatedly consume the <a>next input token</a>:
<dl>
<dt><<whitespace-token>>
<dd>
Do nothing.
<dt><<EOF-token>>
<dd>
Return the list of rules.
<dt><<CDO-token>>
<dt><<CDC-token>>
<dd>
If the <var>top-level flag</var> is set,
do nothing.
Otherwise,
<a>reconsume the current input token</a>.
<a>Consume a qualified rule</a>.
If anything is returned,
append it to the list of rules.
<dt><<at-keyword-token>>
<dd>
<a>Reconsume the current input token</a>.
<a>Consume an at-rule</a>,
and append the returned value to the list of rules.
<dt>anything else
<dd>
<a>Reconsume the current input token</a>.
<a>Consume a qualified rule</a>.
If anything is returned,
append it to the list of rules.
</dl>
<h4 id="consume-at-rule">
Consume an at-rule</h4>
To <dfn>consume an at-rule</dfn>:
<a>Consume the next input token</a>.
Create a new at-rule
with its name set to the value of the <a>current input token</a>,
its prelude initially set to an empty [=list=],
and its value initially set to nothing.
Repeatedly consume the <a>next input token</a>:
<dl>
<dt><<semicolon-token>>
<dd>
Return the at-rule.
<dt><<EOF-token>>
<dd>
This is a <a>parse error</a>.
Return the at-rule.
<dt><a href="#tokendef-open-curly">&lt;{-token></a>
<dd>
<a>Consume a simple block</a>
and assign it to the at-rule's block.
Return the at-rule.
<dt><a>simple block</a> with an associated token of <a href="#tokendef-open-curly">&lt;{-token></a>
<dd>
Assign the block to the at-rule's block.
Return the at-rule.
<dt>anything else
<dd>
<a>Reconsume the current input token</a>.
<a>Consume a component value</a>.
Append the returned value to the at-rule's prelude.
</dl>
<h4 id="consume-qualified-rule">
Consume a qualified rule</h4>
To <dfn>consume a qualified rule</dfn>:
Create a new qualified rule
with its prelude initially set to an empty [=list=],
and its value initially set to nothing.
Repeatedly consume the <a>next input token</a>:
<dl>
<dt><<EOF-token>>
<dd>
This is a <a>parse error</a>.
Return nothing.
<dt><a href="#tokendef-open-curly">&lt;{-token></a>
<dd>
<a>Consume a simple block</a>
and assign it to the qualified rule's block.
Return the qualified rule.
<dt><a>simple block</a> with an associated token of <a href="#tokendef-open-curly">&lt;{-token></a>
<dd>
Assign the block to the qualified rule's block.
Return the qualified rule.
<dt>anything else
<dd>
<a>Reconsume the current input token</a>.
<a>Consume a component value</a>.
Append the returned value to the qualified rule's prelude.
</dl>
<h4 id="consume-list-of-declarations">
Consume a list of declarations</h4>
To <dfn>consume a list of declarations</dfn>:
Create an initially empty list of declarations.
Repeatedly consume the <a>next input token</a>:
<dl>
<dt><<whitespace-token>>
<dt><<semicolon-token>>
<dd>
Do nothing.
<dt><<EOF-token>>
<dd>
Return the list of declarations.
<dt><<at-keyword-token>>
<dd>
<a>Reconsume the current input token</a>.
<a>Consume an at-rule</a>.
Append the returned rule to the list of declarations.
<dt><<ident-token>>
<dd>
Initialize a temporary list initially filled with the <a>current input token</a>.
As long as the <a>next input token</a> is anything other than a <<semicolon-token>> or <<EOF-token>>,
<a>consume a component value</a> and append it to the temporary list.
<a>Consume a declaration</a> from the temporary list.
If anything was returned,
append it to the list of declarations.
<dt>anything else</dd>
<dd>
This is a <a>parse error</a>.
<a>Reconsume the current input token</a>.
As long as the <a>next input token</a> is anything other than a <<semicolon-token>> or <<EOF-token>>,
<a>consume a component value</a>
and throw away the returned value.
</dl>
<h4 id="consume-declaration">
Consume a declaration</h4>
Note: This algorithm assumes that the <a>next input token</a> has already been checked to be an <<ident-token>>.
To <dfn>consume a declaration</dfn>:
<a>Consume the next input token</a>.
Create a new declaration
with its name set to the value of the <a>current input token</a>
and its value initially set to an empty [=list=].
<ol>
<li>
While the <a>next input token</a> is a <<whitespace-token>>,
<a>consume the next input token</a>.
<li>
If the <a>next input token</a> is anything other than a <<colon-token>>,
this is a <a>parse error</a>.
Return nothing.
Otherwise, <a>consume the next input token</a>.
<li>
While the <a>next input token</a> is a <<whitespace-token>>,
<a>consume the next input token</a>.
<li>
As long as the <a>next input token</a> is anything other than an <<EOF-token>>,
<a>consume a component value</a>
and append it to the declaration's value.
<li>
If the last two non-<<whitespace-token>>s in the declaration's value are
a <<delim-token>> with the value "!"
followed by an <<ident-token>> with a value that is an <a>ASCII case-insensitive</a> match for "important",
remove them from the declaration's value
and set the declaration's <var>important</var> flag to true.
<li>
While the last token in the declaration's value is a <<whitespace-token>>,
[=list/remove=] that token.
<li>
Return the declaration.
</ol>
<h4 id="consume-component-value">
Consume a component value</h4>
To <dfn>consume a component value</dfn>:
<a>Consume the next input token</a>.
If the <a>current input token</a>
is a <a href="#tokendef-open-curly">&lt;{-token></a>, <a href="#tokendef-open-square">&lt;[-token></a>, or <a href="#tokendef-open-paren">&lt;(-token></a>,
<a>consume a simple block</a>
and return it.
Otherwise, if the <a>current input token</a>
is a <<function-token>>,
<a>consume a function</a>
and return it.
Otherwise, return the <a>current input token</a>.
<h4 id="consume-simple-block">
Consume a simple block</h4>
Note: This algorithm assumes that the <a>current input token</a> has already been checked to be an <a href="#tokendef-open-curly">&lt;{-token></a>, <a href="#tokendef-open-square">&lt;[-token></a>, or <a href="#tokendef-open-paren">&lt;(-token></a>.
To <dfn>consume a simple block</dfn>:
The <dfn>ending token</dfn> is the mirror variant of the <a>current input token</a>.
(E.g. if it was called with <a href="#tokendef-open-square">&lt;[-token></a>, the <a>ending token</a> is <a href="#tokendef-close-square">&lt;]-token></a>.)
Create a <a>simple block</a> with its associated token set to the <a>current input token</a>
and with its value initially set to an empty [=list=].
Repeatedly consume the <a>next input token</a> and process it as follows:
<dl>
<dt><a>ending token</a>
<dd>
Return the block.
<dt><<EOF-token>>
<dd>
This is a <a>parse error</a>.
Return the block.
<dt>anything else
<dd>
<a>Reconsume the current input token</a>.
<a>Consume a component value</a>
and append it to the value of the block.
</dl>
Note: CSS has an unfortunate syntactic ambiguity
between blocks that can contain declarations
and blocks that can contain qualified rules,
so any "consume" algorithms that handle rules
will initially use this more generic algorithm
rather than the more specific
[=consume a list of declarations=]
or [=consume a list of rules=] algorithms.
These more specific algorithms are instead invoked when grammars are applied,
depending on whether it contains a <<declaration-list>>
or a <<rule-list>>/<<stylesheet>>.
<h4 id="consume-function">
Consume a function</h4>
Note: This algorithm assumes that the <a>current input token</a> has already been checked to be a <<function-token>>.
To <dfn>consume a function</dfn>:
Create a function with its name equal to the value of the <a>current input token</a>
and with its value initially set to an empty [=list=].
Repeatedly consume the <a>next input token</a> and process it as follows:
<dl>
<dt><a href="#tokendef-close-paren">&lt;)-token></a>
<dd>
Return the function.
<dt><<EOF-token>>
<dd>
This is a <a>parse error</a>.
Return the function.
<dt>anything else
<dd>
<a>Reconsume the current input token</a>.
<a>Consume a component value</a>
and append the returned value
to the function's value.
</dl>
<!--
███ ██ ██ ████████
██ ██ ███ ██ ██ ██ ██
██ ██ ████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██████ ████████
█████████ ██ ████ ██ ██ ██
██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ████████
-->
<h2 id="anb-microsyntax">
The <var>An+B</var> microsyntax</h2>
Several things in CSS,
such as the <span class=informative>'':nth-child()''</span> pseudoclass,
need to indicate indexes in a list.
The <var>An+B</var> microsyntax is useful for this,
allowing an author to easily indicate single elements
or all elements at regularly-spaced intervals in a list.
The <dfn export>An+B</dfn> notation defines an integer step (|A|) and offset (|B|),
and represents the <var>An+B</var>th elements in a list,
for every positive integer or zero value of <var>n</var>,
with the first element in the list having index 1 (not 0).
For values of <var>A</var> and <var>B</var> greater than 0,
this effectively divides the list into groups of <var>A</var> elements
(the last group taking the remainder),
and selecting the <var>B</var>th element of each group.
The <var>An+B</var> notation also accepts the ''even'' and ''odd'' keywords,
which have the same meaning as ''2n'' and ''2n+1'', respectively.
<div class="example">
<p>Examples:
<pre><!--
-->2n+0 /* represents all of the even elements in the list */&#xa;<!--
-->even /* same */&#xa;<!--
-->4n+1 /* represents the 1st, 5th, 9th, 13th, etc. elements in the list */</pre>
</div>
The values of <var>A</var> and <var>B</var> can be negative,
but only the positive results of <var>An+B</var>,
for <var>n</var> ≥ 0,
are used.
<div class="example">
<p>Example:
<pre><!--
-->-1n+6 /* represents the first 6 elements of the list */&#xa;<!--
-->-4n+10 /* represents the 2nd, 6th, and 10th elements of the list */
</pre>
</div>
If both <var>A</var> and <var>B</var> are 0,
the pseudo-class represents no element in the list.
<h3 id='anb-syntax'>
Informal Syntax Description</h3>
<em>This section is non-normative.</em>
When <var>A</var> is 0, the <var>An</var> part may be omitted
(unless the <var>B</var> part is already omitted).
When <var>An</var> is not included
and <var>B</var> is non-negative,
the ''+'' sign before <var>B</var> (when allowed)
may also be omitted.
In this case the syntax simplifies to just <var>B</var>.
<div class="example">
<p>Examples:
<pre><!--
-->0n+5 /* represents the 5th element in the list */&#xa;<!--
-->5 /* same */</pre>
</div>
When <var>A</var> is 1 or -1,
the <code>1</code> may be omitted from the rule.
<div class="example">
<p>Examples:
<p>The following notations are therefore equivalent:
<pre><!--
-->1n+0 /* represents all elements in the list */&#xa;<!--
-->n+0 /* same */&#xa;<!--
-->n /* same */</pre>
</div>
If <var>B</var> is 0, then every <var>A</var>th element is picked.
In such a case,
the <var>+B</var> (or <var>-B</var>) part may be omitted
unless the <var>A</var> part is already omitted.
<div class="example">
<p>Examples:
<pre><!--
-->2n+0 /* represents every even element in the list */&#xa;<!--
-->2n /* same */</pre>
</div>
When B is negative, its minus sign replaces the ''+'' sign.
<div class="example">
<p>Valid example:
<pre>3n-6</pre>
<p>Invalid example:
<pre>3n + -6</pre>
</div>
Whitespace is permitted on either side of the ''+'' or ''-''
that separates the <var>An</var> and <var>B</var> parts when both are present.
<div class="example">
<p>Valid Examples with white space:
<pre><!--
-->3n + 1&#xa;<!--
-->+3n - 2&#xa;<!--
-->-n+ 6&#xa;<!--
-->+6</pre>
<p>Invalid Examples with white space:
<pre><!--
-->3 n&#xa;<!--
-->+ 2n&#xa;<!--
-->+ 2</pre>
</div>
<h3 id="the-anb-type">
The <code>&lt;an+b></code> type</h3>
The <var>An+B</var> notation was originally defined using a slightly different tokenizer than the rest of CSS,
resulting in a somewhat odd definition when expressed in terms of CSS tokens.
This section describes how to recognize the <var>An+B</var> notation in terms of CSS tokens
(thus defining the <var>&lt;an+b></var> type for CSS grammar purposes),
and how to interpret the CSS tokens to obtain values for <var>A</var> and <var>B</var>.
The <var>&lt;an+b></var> type is defined
(using the <a href="https://www.w3.org/TR/css3-values/#value-defs">Value Definition Syntax in the Values &amp; Units spec</a>)
as:
<pre class='prod'>
<dfn id="anb-production">&lt;an+b></dfn> =
odd | even |
<var>&lt;integer></var> |
<var>&lt;n-dimension></var> |
'+'?<sup><a href="#anb-plus"></a></sup> n |
-n |
<var>&lt;ndashdigit-dimension></var> |
'+'?<sup><a href="#anb-plus"></a></sup> <var>&lt;ndashdigit-ident></var> |
<var>&lt;dashndashdigit-ident></var> |
<var>&lt;n-dimension></var> <var>&lt;signed-integer></var> |
'+'?<sup><a href="#anb-plus"></a></sup> n <var>&lt;signed-integer></var> |
-n <var>&lt;signed-integer></var> |
<var>&lt;ndash-dimension></var> <var>&lt;signless-integer></var> |
'+'?<sup><a href="#anb-plus"></a></sup> n- <var>&lt;signless-integer></var> |
-n- <var>&lt;signless-integer></var> |
<var>&lt;n-dimension></var> ['+' | '-'] <var>&lt;signless-integer></var>
'+'?<sup><a href="#anb-plus"></a></sup> n ['+' | '-'] <var>&lt;signless-integer></var> |
-n ['+' | '-'] <var>&lt;signless-integer></var>
</pre>
where:
<ul>
<li><dfn><code>&lt;n-dimension></code></dfn> is a <<dimension-token>> with its type flag set to "integer", and a unit that is an <a>ASCII case-insensitive</a> match for "n"
<li><dfn><code>&lt;ndash-dimension></code></dfn> is a <<dimension-token>> with its type flag set to "integer", and a unit that is an <a>ASCII case-insensitive</a> match for "n-"
<li><dfn><code>&lt;ndashdigit-dimension></code></dfn> is a <<dimension-token>> with its type flag set to "integer", and a unit that is an <a>ASCII case-insensitive</a> match for "n-*", where "*" is a series of one or more <a>digits</a>
<li><dfn><code>&lt;ndashdigit-ident></code></dfn> is an <<ident-token>> whose value is an <a>ASCII case-insensitive</a> match for "n-*", where "*" is a series of one or more <a>digits</a>
<li><dfn><code>&lt;dashndashdigit-ident></code></dfn> is an <<ident-token>> whose value is an <a>ASCII case-insensitive</a> match for "-n-*", where "*" is a series of one or more <a>digits</a>
<li><dfn><code>&lt;integer></code></dfn> is a <<number-token>> with its type flag set to "integer"
<li><dfn><code>&lt;signed-integer></code></dfn> is a <<number-token>> with its type flag set to "integer", and whose <a>representation</a> starts with "+" or "-"
<li><dfn><code>&lt;signless-integer></code></dfn> is a <<number-token>> with its type flag set to "integer", and whose <a>representation</a> starts with a <a>digit</a>
</ul>
<p id="anb-plus">
<sup></sup>: When a plus sign (+) precedes an ident starting with "n", as in the cases marked above,
there must be no whitespace between the two tokens,
or else the tokens do not match the above grammar.
Whitespace is valid (and ignored) between any other two tokens.
The clauses of the production are interpreted as follows:
<dl>
<dt>''odd''
<dd>
<var>A</var> is 2, <var>B</var> is 1.
<dt>''even''
<dd>
<var>A</var> is 2, <var>B</var> is 0.
<dt><code><var>&lt;integer></var></code>
<dd>
<var>A</var> is 0, <var>B</var> is the integer’s value.
<dt><code><var>&lt;n-dimension></var></code>
<dt><code>'+'? n</code>
<dt><code>-n</code>
<dd>
<var>A</var> is the dimension's value, 1, or -1, respectively.
<var>B</var> is 0.
<dt><code><var>&lt;ndashdigit-dimension></var></code>
<dt><code>'+'? <var>&lt;ndashdigit-ident></var></code>
<dd>
<var>A</var> is the dimension's value or 1, respectively.
<var>B</var> is the dimension's unit or ident's value, respectively,
with the first <a>code point</a> removed and the remainder interpreted as a base-10 number.
<span class=note>B is negative.</span>
<dt><code><var>&lt;dashndashdigit-ident></var></code>
<dd>
<var>A</var> is -1.
<var>B</var> is the ident's value, with the first two <a>code points</a> removed and the remainder interpreted as a base-10 number.
<span class=note>B is negative.</span>
<dt><code><var>&lt;n-dimension></var> <var>&lt;signed-integer></var></code>
<dt><code>'+'? n <var>&lt;signed-integer></var></code>
<dt><code>-n <var>&lt;signed-integer></var></code>
<dd>
<var>A</var> is the dimension's value, 1, or -1, respectively.
<var>B</var> is the integer’s value.
<dt><code><var>&lt;ndash-dimension></var> <var>&lt;signless-integer></var></code>
<dt><code>'+'? n- <var>&lt;signless-integer></var></code>
<dt><code>-n- <var>&lt;signless-integer></var></code>
<dd>
<var>A</var> is the dimension's value, 1, or -1, respectively.
<var>B</var> is the negation of the integer’s value.
<dt><code><var>&lt;n-dimension></var> ['+' | '-'] <var>&lt;signless-integer></var></code>
<dt><code>'+'? n ['+' | '-'] <var>&lt;signless-integer></var></code>
<dt><code>-n ['+' | '-'] <var>&lt;signless-integer></var></code>
<dd>
<var>A</var> is the dimension's value, 1, or -1, respectively.
<var>B</var> is the integer’s value.
If a <code>'-'</code> was provided between the two, <var>B</var> is instead the negation of the integer’s value.
</dl>
<!--
██ ██ ████████ ███ ██ ██ ██████ ████████
██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ████ ██ ██ ██
██ ██ ████████ ██ ██ ██ ██ ██ ██ ████ ██████
██ ██ ██ ██ █████████ ██ ████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
███████ ██ ██ ██ ██ ██ ██ ██████ ████████
-->
<h2 id="urange">
The Unicode-Range microsyntax</h2>
Some constructs,
<span class=informative>such as the 'unicode-range' descriptor for the ''@font-face'' rule,</span>
need a way to describe one or more unicode code points.
The <dfn>&lt;urange></dfn> production represents a range of one or more unicode code points.
Informally, the <<urange>> production has three forms:
<dl>
<dt>U+0001
<dd>
Defines a range consisting of a single code point,
in this case the code point "1".
<dt>U+0001-00ff
<dd>
Defines a range of codepoints between the first and the second value inclusive,
in this case the range between "1" and "ff" (255 in decimal) inclusive.
<dt>U+00??
<dd>
Defines a range of codepoints where the "?" characters range over all <a>hex digits</a>,
in this case defining the same as the value ''U+0000-00ff''.
</dl>
In each form, a maximum of 6 digits is allowed for each hexadecimal number
(if you treat "?" as a hexadecimal digit).
<h3 id="urange-syntax">
The <<urange>> type</h3>
The <<urange>> notation was originally defined as a primitive token in CSS,
but it is used very rarely,
and collides with legitimate <<ident-token>>s in confusing ways.
This section describes how to recognize the <<urange>> notation
in terms of existing CSS tokens,
and how to interpret it as a range of unicode codepoints.
<details class=note>
<summary>What are the confusing collisions?</summary>
For example, in the CSS <nobr>''u + a { color: green; }''</nobr>,
the intended meaning is that an <code>a</code> element
following a <code>u</code> element
should be colored green.
Whitespace is not normally required between combinators
and the surrounding selectors,
so it <em>should</em> be equivalent to minify it to
<nobr>''u+a{color:green;}''</nobr>.
With any other combinator, the two pieces of CSS would be equivalent,
but due to the previous existence of a specialized unicode-range token,
the selector portion of the minified code now contains a unicode-range,
not two idents and a combinator.
It thus fails to match the Selectors grammar,
and the rule is thrown out as invalid.
(This example is taken from a real-world bug reported to Firefox.)
</details>
Note: The syntax described here is intentionally very low-level,
and geared toward implementors.
Authors should instead read the informal syntax description in the previous section,
as it contains all information necessary to use <<urange>>,
and is actually readable.
The <<urange>> type is defined
(using the <a href="https://www.w3.org/TR/css3-values/#value-defs">Value Definition Syntax in the Values & Units spec</a>) as:
<pre class="prod">
<<urange>> =
u '+' <<ident-token>> '?'* |
u <<dimension-token>> '?'* |
u <<number-token>> '?'* |
u <<number-token>> <<dimension-token>> |
u <<number-token>> <<number-token>> |
u '+' '?'+
</pre>
In this production,
no whitespace can occur between any of the tokens.
The <<urange>> production represents a range of one or more contiguous unicode code points
as a <var>start value</var> and an <var>end value</var>,
which are non-negative integers.
To interpret the production above into a range,
execute the following steps in order:
1. Skipping the first ''u'' token,
concatenate the <a>representations</a> of all the tokens in the production together.
Let this be <var>text</var>.
2. If the first character of <var>text</var> is U+002B PLUS SIGN,
consume it.
Otherwise,
this is an invalid <<urange>>,
and this algorithm must exit.
3. Consume as many <a>hex digits</a> from <var>text</var> as possible.
then consume as many U+003F QUESTION MARK (?) <a>code points</a> as possible.
If zero <a>code points</a> were consumed,
or more than six <a>code points</a> were consumed,
this is an invalid <<urange>>,
and this algorithm must exit.
If any U+003F QUESTION MARK (?) <a>code points</a> were consumed, then:
1. If there are any <a>code points</a> left in <var>text</var>,
this is an invalid <<urange>>,
and this algorithm must exit.
2. Interpret the consumed <a>code points</a> as a hexadecimal number,
with the U+003F QUESTION MARK (?) <a>code points</a>
replaced by U+0030 DIGIT ZERO (0) <a>code points</a>.
This is the <var>start value</var>.
3. Interpret the consumed <a>code points</a> as a hexadecimal number again,
with the U+003F QUESTION MARK (?) <a>code points</a>
replaced by U+0046 LATIN CAPITAL LETTER F (F) <a>code points</a>.
This is the <var>end value</var>.
4. Exit this algorithm.
Otherwise, interpret the consumed <a>code points</a> as a hexadecimal number.
This is the <var>start value</var>.
4. If there are no <a>code points</a> left in <var>text</var>,
The <var>end value</var> is the same as the <var>start value</var>.
Exit this algorithm.
5. If the next <a>code point</a> in <var>text</var> is U+002D HYPHEN-MINUS (-),
consume it.
Otherwise,
this is an invalid <<urange>>,
and this algorithm must exit.
6. Consume as many <a>hex digits</a> as possible from <var>text</var>.
If zero <a>hex digits</a> were consumed,
or more than 6 <a>hex digits</a> were consumed,
this is an invalid <<urange>>,
and this algorithm must exit.
If there are any <a>code points</a> left in <var>text</var>,
this is an invalid <<urange>>,
and this algorithm must exit.
7. Interpret the consumed <a>code points</a> as a hexadecimal number.
This is the <var>end value</var>.
To determine what codepoints the <<urange>> represents:
1. If <var>end value</var> is greater than the <a>maximum allowed code point</a>,
the <<urange>> is invalid and a syntax error.
2. If <var>start value</var> is greater than <var>end value</var>,
the <<urange>> is invalid and a syntax error.
3. Otherwise, the <<urange>> represents a contiguous range of codepoints from <var>start value</var> to <var>end value</var>, inclusive.
Note: The syntax of <<urange>> is intentionally fairly wide;
its patterns capture every possible token sequence
that the informal syntax can generate.
However, it requires no whitespace between its constituent tokens,
which renders it fairly safe to use in practice.
Even grammars which have a <<urange>> followed by a <<number>> or <<dimension>>
(which might appear to be ambiguous
if an author specifies the <<urange>> with the ''u <<number>>'' clause)
are actually quite safe,
as an author would have to intentionally separate the <<urange>> and the <<number>>/<<dimension>>
with a comment rather than whitespace
for it to be ambiguous.
Thus, while it's <em>possible</em> for authors to write things that are parsed in confusing ways,
the actual code they'd have to write to cause the confusion is, itself, confusing and rare.
<!--
██████ ████████ ███ ██ ██ ██ ██ ███ ████████ ██████
██ ██ ██ ██ ██ ██ ███ ███ ███ ███ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ████ ████ ████ ████ ██ ██ ██ ██ ██
██ ████ ████████ ██ ██ ██ ███ ██ ██ ███ ██ ██ ██ ████████ ██████
██ ██ ██ ██ █████████ ██ ██ ██ ██ █████████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████
-->
<h2 id='rule-defs'>
Defining Grammars for Rules and Other Values</h2>
The <a href="https://www.w3.org/TR/css3-values/">Values</a> spec defines how to specify a grammar for properties.
This section does the same, but for rules.
Just like in property grammars,
the notation <code>&lt;foo></code> refers to the "foo" grammar term,
assumed to be defined elsewhere.
Substituting the <code>&lt;foo></code> for its definition results in a semantically identical grammar.
Several types of tokens are written literally, without quotes:
<ul>
<li><<ident-token>>s (such as <code>auto</code>, <code>disc</code>, etc), which are simply written as their value.
<li><<at-keyword-token>>s, which are written as an @ character followed by the token's value, like <code>@media</code>.
<li><<function-token>>s, which are written as the function name followed by a ( character, like <code>translate(</code>.
<li>The <<colon-token>> (written as <code>:</code>), <<comma-token>> (written as <code>,</code>), <<semicolon-token>> (written as <code>;</code>), <a href="#tokendef-open-paren">&lt;(-token></a>, <a href="#tokendef-close-paren">&lt;)-token></a>, <a href="#tokendef-open-curly">&lt;{-token></a>, and <a href="#tokendef-close-curly">&lt;}-token></a>s.
</ul>
Tokens match if their value is a match for the value defined in the grammar.
Unless otherwise specified, all matches are <a>ASCII case-insensitive</a>.
Note: Although it is possible, with <a>escaping</a>,
to construct an <<ident-token>> whose value ends with <code>(</code> or starts with <code>@</code>,
such a tokens is not a <<function-token>> or an <<at-keyword-token>>
and does not match corresponding grammar definitions.
<<delim-token>>s are written with their value enclosed in single quotes.
For example, a <<delim-token>> containing the "+" <a>code point</a> is written as <code>'+'</code>.
Similarly, the <a href="#tokendef-open-square">&lt;[-token></a> and <a href="#tokendef-close-square">&lt;]-token></a>s must be written in single quotes,
as they're used by the syntax of the grammar itself to group clauses.
<<whitespace-token>> is never indicated in the grammar;
<<whitespace-token>>s are allowed before, after, and between any two tokens,
unless explicitly specified otherwise in prose definitions.
(For example, if the prelude of a rule is a selector,
whitespace is significant.)
When defining a function or a block,
the ending token must be specified in the grammar,
but if it's not present in the eventual token stream,
it still matches.
<div class='example'>
For example, the syntax of the ''translateX()'' function is:
<pre>translateX( <<translation-value>> )</pre>
However, the stylesheet may end with the function unclosed, like:
<pre>.foo { transform: translate(50px</pre>
The CSS parser parses this as a style rule containing one declaration,
whose value is a function named "translate".
This matches the above grammar,
even though the ending token didn't appear in the token stream,
because by the time the parser is finished,
the presence of the ending token is no longer possible to determine;
all you have is the fact that there's a block and a function.
</div>
<h3 id='declaration-rule-list'>
Defining Block Contents: the <<declaration-list>>, <<rule-list>>, and <<stylesheet>> productions</h3>
The CSS parser is agnostic as to the contents of blocks,
such as those that come at the end of some at-rules.
Defining the generic grammar of the blocks in terms of tokens is non-trivial,
but there are dedicated and unambiguous algorithms defined for parsing this.
The <dfn>&lt;declaration-list></dfn> production represents a list of declarations.
It may only be used in grammars as the sole value in a block,
and represents that the contents of the block must be parsed using the <a>consume a list of declarations</a> algorithm.
Similarly, the <dfn>&lt;rule-list></dfn> production represents a list of rules,
and may only be used in grammars as the sole value in a block.
It represents that the contents of the block must be parsed using the <a>consume a list of rules</a> algorithm.
Finally, the <dfn>&lt;stylesheet></dfn> production represents a list of rules.
It is identical to <<rule-list>>,
except that blocks using it default to accepting all rules
that aren't otherwise limited to a particular context.
<div class='example'>
For example, the ''@font-face'' rule is defined to have an empty prelude,
and to contain a list of declarations.
This is expressed with the following grammar:
<pre>@font-face { <<declaration-list>> }</pre>
This is a complete and sufficient definition of the rule's grammar.
For another example,
''@keyframes'' rules are more complex,
interpreting their prelude as a name and containing keyframes rules in their block
Their grammar is:
<pre>@keyframes <<keyframes-name>> { <<rule-list>> }</pre>
</div>
For rules that use <<declaration-list>>,
the spec for the rule must define which properties, descriptors, and/or at-rules are valid inside the rule;
this may be as simple as saying "The @foo rule accepts the properties/descriptors defined in this specification/section.",
and extension specs may simply say "The @foo rule additionally accepts the following properties/descriptors.".
Any declarations or at-rules found inside the block that are not defined as valid
must be removed from the rule's value.
Within a <<declaration-list>>,
<code>!important</code> is automatically invalid on any descriptors.
If the rule accepts properties,
the spec for the rule must define whether the properties interact with the cascade,
and with what specificity.
If they don't interact with the cascade,
properties containing <code>!important</code> are automatically invalid;
otherwise using <code>!important</code> is valid
and causes the declaration to be [=important=]
for the purposes of the [=cascade=].
See [[!CSS-CASCADE-3]].
<div class='example'>
For example, the grammar for ''@font-face'' in the previous example must,
in addition to what is written there,
define that the allowed declarations are the descriptors defined in the Fonts spec.
</div>
For rules that use <<rule-list>>,
the spec for the rule must define what types of rules are valid inside the rule,
same as <<declaration-list>>,
and unrecognized rules must similarly be removed from the rule's value.
<div class='example'>
For example, the grammar for ''@keyframes'' in the previous example must,
in addition to what is written there,
define that the only allowed rules are <<keyframe-rule>>s,
which are defined as: