水無月ばけらのえび日記

bakera.jp > 水無月ばけらのえび日記 > 文字符号化方式判定の優先順位

文字符号化方式判定の優先順位

2008年9月27日(土曜日)

文字符号化方式判定の優先順位

唐突に、ブラウザが文字符号化方式を判定する場合の優先順位についてメモ。この順序については、HTML 4.01 5.2.2 で規定されています。

To sum up, conforming user agents must observe the following priorities when determining a document's character encoding (from highest priority to lowest):

1. An HTTP "charset" parameter in a "Content-Type" field.

2. A META declaration with "http-equiv" set to "Content-Type" and a value set for "charset".

3. The charset attribute set on an element that designates an external resource.

以上、HTML4.01 5.2.2 Specifying the character encoding より

これは明確でしょう。HTTP応答ヘッダでの指定が優先、それがなければmeta要素の指定が参照され、それらがない場合に初めてリンク元のcharset属性が参照されます。"must observe the following priorities..." とあるので、この順番は必須遵守事項です。おまけに、6.9 にはこういう記述もあります。

User agents must follow the steps set out in the section on specifying character encodings in order to determine the character encoding of an external resource.

以上、HTML4.01 6.9 Character encodings より

こちらも must です。前述の優先順位が守られていない場合、そのブラウザは HTML4.01 の仕様に適合していないことになります。

ところで、5.2.2 の続きにはこうあって……。

In addition to this list of priorities, the user agent may use heuristics and user settings. For example, many user agents use a heuristic to distinguish the various encodings used for Japanese text.

以上、HTML4.01 5.2.2 Specifying the character encoding より

"In addition to this list of priorities" と言われても、上に足すのか下に足すのかは書かれていないという微妙な罠があります。ブラウザによる自動判別の優先度は不明としか言いようがありません。直感的には前述の3つより優先度が低くなるべきだと思うのですが、それは明確には規定されていないように思います。

関連する話題: HTML / メモ

最近の日記

関わった本など