FFFF csswg-drafts/css-syntax/Overview.bs at e3cb7669e69dfe84431f8ad4d414702b2031dc0e · w3c/csswg-drafts · GitHub
Skip to content

Latest commit

 

History

History
3667 lines (2935 loc) · 125 KB

File metadata and controls

3667 lines (2935 loc) · 125 KB
use <code>utf-8</code> as the fallback encoding;
if it was anything else except failure,
use the return value as the fallback encoding.
<details class='why'>
<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 <a>@charset</a> 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,
use that as the fallback encoding.
<li>
Otherwise, use <code>utf-8</code> as the fallback encoding.
</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>
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="http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#link-type-stylesheet">the environment encoding for <code>&lt;link rel=stylesheet></code></a>.
Note: [[CSSOM]] defines <a href="http://dev.w3.org/csswg/cssom/#requirements-on-user-agents-implementing-the-xml-stylesheet-processing-instruction">the environment encoding for <code>&lt;xml-stylesheet?></code></a>.
Note: [[CSS3CASCADE]] defines <a href="http://dev.w3.org/csswg/css-cascade/#at-ruledef-import">the environment encoding for <code>@import</code></a>.
<h3 id="input-preprocessing">
Preprocessing the input stream</h3>
The input stream consists of the <a>code points</a>
pushed into it as the input byte stream is decoded.
Before sending the input stream to the tokenizer,
implementations must make the following <a>code point</a> substitutions:
<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),
by a single U+000A LINE FEED (LF) <a>code point</a>.
<li>
Replace any U+0000 NULL <a>code point</a> with U+FFFD REPLACEMENT CHARACTER (�).
</ul>
<h2 id="tokenization">
Tokenization</h2>
Implementations must act as if they used the following algorithms to tokenize CSS.
To transform a stream of <a>code points</a> into a stream of tokens,
repeatedly <a>consume a token</a>
until an <<EOF-token>> is reached,
collecting the returned tokens into a stream.
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 the 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;include-match-token></dfn>,
<dfn>&lt;dash-match-token></dfn>,
<dfn>&lt;prefix-match-token></dfn>,
<dfn>&lt;suffix-match-token></dfn>,
<dfn>&lt;substring-match-token></dfn>,
<dfn>&lt;column-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 representation composed of one or more <a>code points</a>, and 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="http://www.w3.org/TR/selectors/#id-selectors">ID selectors</a>.
Note: As a technical note,
the tokenizer defined here requires only three <a>code points</a> of look-ahead.
The tokens it produces are designed to allow Selectors to be parsed with one token of look-ahead,
and additional tokens may be added in the future to maintain this invariant.
<!--
████████ ███ ████ ██ ████████ ███████ ███ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██ ██ ██ ██ ████████ ██ ██ ██ ██ ██ ██
██ ██ █████████ ██ ██ ██ ██ ██ ██ █████████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████ ████████ ██ ██ ███████ ██ ██ ████████
-->
<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="include-match-token-diagram"><<include-match-token>>
<dd>
<pre class='railroad'>
T: ~=
</pre>
<dt id="dash-match-token-diagram"><<dash-match-token>>
<dd>
<pre class='railroad'>
T: |=
</pre>
<dt id="prefix-match-token-diagram"><<prefix-match-token>>
<dd>
<pre class='railroad'>
T: ^=
</pre>
<dt id="suffix-match-token-diagram"><<suffix-match-token>>
<dd>
<pre class='railroad'>
T: $=
</pre>
<dt id="substring-match-token-diagram"><<substring-match-token>>
<dd>
<pre class='railroad'>
T: *=
</pre>
<dt id="column-token-diagram"><<column-token>>
<dd>
<pre class='railroad'>
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>code point</dfn>
<dd>
A <a href="http://unicode.org/glossary/#code_point">Unicode code point</a>. [[!UNICODE]]
Any value in the Unicode codespace; that is, the range of integers from 0 to (hexadecimal) 10FFFF.
<dt><dfn>next input code point</dfn>
<dd>
The first <a>code point</a> in the input stream 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 input stream,
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 input stream.
Whenever the input stream 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).
<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),
or a <a>code point</a> between U+0061 LATIN SMALL LETTER A (a) and U+0066 LATIN SMALL LETTER F (f).
<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).
<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).
<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>name-start code point</dfn>
<dd>
A <a>letter</a>,
a <a>non-ASCII code point</a>,
or U+005F LOW LINE (_).
<dt><dfn export>name code point</dfn>
<dd>
A <a>name-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,
or U+000B LINE TABULATION,
or a <a>code point</a> between U+000E SHIFT OUT and U+001F INFORMATION SEPARATOR ONE,
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>surrogate code point</dfn>
<dd>
A <a>code point</a> between U+D800 and U+DFFF inclusive.
<dt><dfn export>maximum allowed code point</dfn>
<dd>The greatest <a>code point</a> defined by Unicode: U+10FFFF.
<dt><dfn export>identifier</dfn>
<dd>
A portion of the CSS source that has the same syntax as an <<ident-token>>.
Also appears in <<at-keyword-token>>,
<<function-token>>,
<<hash-token>> with the "id" type flag,
and the unit of <<dimension-token>>.
</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 a <a>name 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 a name</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+0024 DOLLAR SIGN ($)
<dd>
If the <a>next input code point</a> is
U+003D EQUALS SIGN (=),
consume it
and return a <<suffix-match-token>>.
Otherwise,
emit 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+002A ASTERISK (*)
<dd>
If the <a>next input code point</a> is
U+003D EQUALS SIGN (=),
consume it
and return a <<substring-match-token>>.
Otherwise,
return a <<delim-token>>
with its value set to the <a>current input code point</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>