Skip to content

Commit 2a87766

Browse files
committed
[css-syntax] Undefine @charset as a rule. Explicitly drop the first @charset rule when parsing a stylesheet, before grammar-checking.
1 parent 09c68de commit 2a87766

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

css-syntax/Overview.bs

+30-22
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,10 @@ The input byte stream</h3>
334334
As well, this mimics the behavior of HTML's <code>&lt;meta charset></code> attribute.
335335
</details>
336336

337-
Note: Note that the syntax of an encoding declaration <em>looks like</em> the syntax of an <a>@charset</a> rule,
338-
but it's actually much more restrictive.
339-
A number of things you can do in CSS that would produce a valid <a>@charset</a> rule,
337+
Note: Note that the syntax of an encoding declaration <em>looks like</em> the syntax of an <a>at-rule</a> named ''@charset'',
338+
but no such rule actually exists,
339+
and the rules for how you can write it are much more restrictive than they would normally be for recognizing such a rule.
340+
A number of things you can do in CSS that would produce a valid <a>@charset</a> rule (if one existed),
340341
such as using multiple spaces, comments, or single quotes,
341342
will cause the encoding declaration to not be recognized.
342343
This behavior keeps the encoding declaration as simple as possible,
@@ -2100,9 +2101,15 @@ Parse a stylesheet</h4>
21002101

21012102
<li>
21022103
<a>Consume a list of rules</a> from the stream of tokens, with the <var>top-level flag</var> set.
2104+
Let the return value be <var>rules</var>.
21032105

21042106
<li>
2105-
Assign the returned value to the stylesheet's value.
2107+
If the first rule in <var>rules</var> is an <a>at-rule</a> with a name
2108+
that is an <a>ASCII case-insensitive</a> match for "charset",
2109+
remove it from <var>rules</var>.
2110+
2111+
<li>
2112+
Assign <var>rules</var> to the stylesheet's value.
21062113

21072114
<li>
21082115
Return the stylesheet.
@@ -3208,24 +3215,25 @@ Style rules</h3>
32083215
<h3 id='charset-rule'>
32093216
The ''@charset'' Rule</h3>
32103217

3211-
The @charset rule is an artifact of the algorithm used to <a>determine the fallback encoding</a> for the stylesheet.
3212-
That algorithm looks for a specific byte sequence as the very first few bytes in the file,
3213-
which has the syntactic form of an @-rule.
3214-
Those bytes are not discarded from the input,
3215-
whether or not they influence the encoding actually used to process the stylesheet.
3216-
3217-
Therefore, the stylesheet parser recognizes an @-rule with the general syntax
3218-
3219-
<pre class='prod'><dfn>@charset</dfn> = @charset <<string>> ;</pre>
3220-
3221-
and, for backward compatibility, includes it in the object model for the stylesheet.
3222-
Modifying, adding, or removing an @charset rule via the object model has no effect
3223-
(in particular it does <strong>not</strong> cause the stylesheet to be rescanned in a different encoding).
3224-
The @charset rule is invalid if it is not the very first, top-level rule in the stylesheet,
3225-
but it is parsed according to the normal syntax for @-rules,
3226-
which are less restrictive than the algorithm that determines the fallback encoding.
3227-
Therefore, an @charset rule may appear in the object model even if it was ignored by that algorithm.
3228-
(For instance, if it was written with extra whitespace or with single rather than double quotes.)
3218+
The algorithm used to <a>determine the fallback encoding</a> for a stylesheet
3219+
looks for a specific byte sequence as the very first few bytes in the file,
3220+
which has the syntactic form of an <a>at-rule</a> named "@charset".
3221+
3222+
However, there is no actual <a>at-rule</a> named <dfn>@charset</dfn>.
3223+
When a stylesheet is actually parsed,
3224+
any occurrences of an ''@charset'' rule must be treated as an unrecognized rule,
3225+
and thus dropped as invalid when the stylesheet is grammar-checked.
3226+
3227+
Note: The algorithm to <a>parse a stylesheet</a> explicitly drops the first ''@charset'' rule from the document,
3228+
before the stylesheet is grammar-checked,
3229+
so valid rules that must appear first in the stylesheet,
3230+
such as ''@import'',
3231+
can still be preceded by an (invalid) ''@charset'' rule
3232+
without making themselves invalid.
3233+
3234+
Note: In CSS 2.1, ''@charset'' was a valid rule.
3235+
Some legacy specs may still refer to a ''@charset'' rule,
3236+
and explicitly talk about its presence in the stylesheet.
32293237

32303238
<!--
32313239
██████ ████████ ████████ ████ ███ ██

0 commit comments

Comments
 (0)