You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the stream of Unicode <adata-link-type=dfnhref=#code-pointtitle="code points">code points</a> that comprises the input to the tokenization stage
426
-
may be initially seen by the user agent as a stream of bytes
426
+
might be initially seen by the user agent as a stream of bytes
427
427
(typically coming over the network or from the local file system).
428
-
The bytes encode the <adata-link-type=dfnhref=#code-pointtitle="code points">code points</a> according to a particular character encoding,
429
-
which the user agent must use to decode the bytes into <adata-link-type=dfnhref=#code-pointtitle="code points">code points</a>.
428
+
If so, the user agent must decode these bytes into <adata-link-type=dfnhref=#code-pointtitle="code points">code points</a> according to a particular character encoding.
430
429
431
430
<p> To decode the stream of bytes into a stream of <adata-link-type=dfnhref=#code-pointtitle="code points">code points</a>,
432
-
UAs must use the <ahref=http://encoding.spec.whatwg.org/#decode>decode</a> algorithm
431
+
UAs must use the <dfndata-dfn-type=dfndata-noexport="" id=decode><ahref=http://encoding.spec.whatwg.org/#decode>decode</a><aclass=self-linkhref=#decode></a></dfn> algorithm
433
432
defined in <adata-biblio-type=normativedata-link-type=bibliohref=#encodingtitle=encoding>[ENCODING]</a>,
434
433
with the fallback encoding determined as follows.
435
434
436
-
<pclass=note> Note: The <ahref=http://encoding.spec.whatwg.org/#decode>decode</a> algorithm
435
+
<pclass=note> Note: The <adata-link-type=dfnhref=#decodetitle=decode>decode</a> algorithm
If HTTP or equivalent protocol defines an encoding (e.g. via the charset parameter of the Content-Type header),
445
-
<ahref=http://encoding.spec.whatwg.org/#concept-encoding-get>get an encoding</a><adata-biblio-type=normativedata-link-type=bibliohref=#encodingtitle=encoding>[ENCODING]</a>
444
+
<dfndata-dfn-type=dfndata-export="" id=get-an-encoding><ahref=http://encoding.spec.whatwg.org/#concept-encoding-get>get an encoding</a><aclass=self-linkhref=#get-an-encoding></a></dfn><adata-biblio-type=normativedata-link-type=bibliohref=#encodingtitle=encoding>[ENCODING]</a>
446
445
for the specified value.
447
446
If that does not return failure,
448
447
use the return value as the fallback encoding.
449
448
450
449
<li>
451
-
Otherwise, check the byte stream. If the first several bytes match the hex sequence
450
+
Otherwise, check the byte stream. If the byte stream begins with the hex sequence
<p> then <ahref=http://encoding.spec.whatwg.org/#concept-encoding-get>get an encoding</a><adata-biblio-type=normativedata-link-type=bibliohref=#encodingtitle=encoding>[ENCODING]</a>
<p> where each <code>XX</code> byte is between 23<sub>16</sub> and 7E<sub>16</sub> inclusive,
454
+
then <adata-link-type=dfnhref=#get-an-encodingtitle="get an encoding">get an encoding</a>
455
+
for the sequence of <code>XX</code> bytes,
456
+
interpreted as <code>ASCII</code>.
457
457
458
-
<pclass=note> Note: Anything ASCII-compatible will do since valid labels are all ASCII,
459
-
so using <code>windows-1252</code> is fine.
458
+
</p><detailsclass=why>
459
+
<summary>What does that byte sequence mean?</summary>
460
460
461
+
<p> The byte sequence above,
462
+
when decoded as ASCII,
463
+
is the string "<code>@charset "…";</code>",
464
+
where the "…" is the sequence of bytes corresponding to the encoding’s label.
465
+
</details>
461
466
462
-
<pclass=note> Note: The byte sequence above,
463
-
when decoded as ASCII,
464
-
is the string "<code>@charset "…";</code>",
465
-
where the "…" is the sequence of bytes corresponding to the encoding’s label.
467
+
<p> UAs may impose an arbitrary limit upon the number of <code>XX</code> bytes scanned,
468
+
as long as it is large enough to encompass all of the <ahref=http://encoding.spec.whatwg.org/#label>labels</a> defined in <adata-biblio-type=normativedata-link-type=bibliohref=#encodingtitle=encoding>[ENCODING]</a>;
469
+
at the time of writing this specification these are all 19 or fewer bytes long.
466
470
467
471
<p> If the return value was <code>utf-16be</code> or <code>utf-16le</code>,
468
472
use <code>utf-8</code> as the fallback encoding;
469
473
if it was anything else except failure,
470
474
use the return value as the fallback encoding.
471
475
472
-
<pclass=note> Note: UTF-16BE and UTF-16LE are the only ASCII-incompatible encodings that
473
-
<ahref=http://encoding.spec.whatwg.org/#concept-encoding-get>get an encoding</a>
474
-
can return.
475
-
Using one of them to decode the ASCII <code>@charset "…";</code> byte sequence
476
-
would result in garbage.
477
-
This mimics HTML <code><meta></code> behavior.
476
+
</p><detailsclass=why>
477
+
<summary>Why use utf-8 when the declaration says utf-16?</summary>
478
+
479
+
<p> The bytes of the encoding declaration spell out “<code>@charset "…";</code>” in ASCII,
480
+
but UTF-16 is not ASCII-compatible.
481
+
Either you’ve typed in complete gibberish (like <code>䁣桡牳整•utf-16be∻</code>) to get the right bytes in the document,
482
+
which we don’t want to encourage,
483
+
or your document is actually in an ASCII-compatible encoding
484
+
and your encoding declaration is lying.
485
+
486
+
<p> Either way, defaulting to UTF-8 is a decent answer.
487
+
488
+
<p> As well, this mimics the behavior of HTML’s <code><meta charset></code> attribute.
489
+
</details>
490
+
491
+
<pclass=note> Note: Note that the syntax of an encoding declaration <em>looks like</em> the syntax of an <aclass=css-codedata-link-type=at-rulehref=#at-ruledef-charsettitle=@charset>@charset</a> rule,
492
+
but it’s actually much more restrictive.
493
+
A number of things you can do in CSS that would produce a valid <aclass=css-codedata-link-type=at-rulehref=#at-ruledef-charsettitle=@charset>@charset</a> rule,
494
+
such as using multiple spaces, comments, or single quotes,
495
+
will cause the encoding declaration to not be recognized.
496
+
This behavior keeps the encoding declaration as simple as possible,
497
+
and thus maximizes the likelihood of it being implemented correctly.
478
498
479
499
<li>
480
500
Otherwise, if an <adata-link-type=dfnhref=#environment-encoding0title="environment encoding">environment encoding</a> is provided by the referring document,
but qualified rules inside <spanclass=cssdata-link-type=maybetitle=@keyframes>@keyframes</span> rules are not <adata-biblio-type=informativedata-link-type=bibliohref=#css3-animationstitle=css3-animations>[CSS3-ANIMATIONS]</a>.
The <spanclass=cssdata-link-type=maybetitle=@charset>@charset</span> Rule</span><aclass=self-linkhref=#charset-rule></a></h3>
4523
+
The <aclass=cssdata-link-type=maybehref=#at-ruledef-charsettitle=@charset>@charset</a> Rule</span><aclass=self-linkhref=#charset-rule></a></h3>
4488
4524
4489
4525
<p> The @charset rule is an artifact of the algorithm used to <adata-link-type=dfnhref=#determine-the-fallback-encodingtitle="determine the fallback encoding">determine the fallback encoding</a> for the stylesheet.
4490
4526
That algorithm looks for a specific byte sequence as the very first few bytes in the file,
0 commit comments