記事本文
大事なことを書き忘れていました
昨日書いた Firefox とマルチバイト文字列を含む URI という記事ですが、肝心なことを書いていないのでした。
例えば hxxk.jp 内のタグ "ビール" が設定された記事のように、 URI にマルチバイト文字列を含むリソースへのリンクを指し示したい場合、 hxxk.jp 内では
と書いていますが、そもそもマルチバイト文字列が URI に含まれるような場合、直接記述するのではなく UTF-8 エンコードあるいはエスケープを行う必要があります。
<a href="http://hxxk.jp/tag/ビール">hoge</a>
と記述してもリンク元もリンク先も UTF-8 であるので Firefox では無事に該当リソースに到達できます
仕様書に明記されています
Basic HTML data types - 6.4 URIs に URI についての定義が書かれていますが、
Please consult the appendix for information about non-ASCII characters in URI attribute values.
と、非 ASCII 文字列を URI の属性値に含める場合は Non-ASCII characters in URI attribute values を参照するように示されています。
Non-ASCII characters in URI attribute values では、
Although URIs do not contain non-ASCII values (see [URI], section 2.1) authors sometimes specify them in attribute values expecting URIs (i.e., defined with %URI; in the DTD). For instance, the following href value is illegal:
<A href="http://foo.org/Håkon">...</A>We recommend that user agents adopt the following convention for handling non-ASCII characters in such cases:
- Represent each character in UTF-8 (see [RFC2279]) as one or more bytes.
- Escape these bytes with the URI escaping mechanism (i.e., by converting each byte to %HH, where HH is the hexadecimal notation of the byte value).
This procedure results in a syntactically legal URI (as defined in [RFC1738], section 2.2 or [RFC2141], section 2) that is independent of the character encoding to which the HTML document carrying the URI may have been transcoded.
と、非 ASCII 文字列をそのまま記述することは不正であると強調して示されています。
ちなみに、
Note. The same conversion based on UTF-8 should be applied to values of the name attribute for the A element.
のように、 UTF-8 によるエンコードは a 要素における name 属性も同様であると示されています。
よって、もし Firefox とマルチバイト文字列を含む URI を読んで、「同じ文字コードのリソース間のリンクであれば、 URI に日本語を直接含めてもちゃんとリンクできるんだ」という理解をされた方がもしいらっしゃれば、文字コードが同じであろうがリソースに到達できようが、その URI は不正であるのだということを理解していただきますようお願いします。

