LoginSignup
2

More than 5 years have passed since last update.

HTML Standard と処理命令

Posted at

はじめに

その昔 xml-stylesheet 処理命令というものがありましたが、あれの HTML Living Standard での扱いがどうなってるのか気になったので少し調べました。

本稿は通常の HTML5 を記述する上では何の参考にもなりませんので、昔を懐かしみたい方のみお読み下さい。

参照仕様

2.2.2 Dependencies

This specification also references the <?xml-stylesheet?> processing instruction, defined in the Associating Style Sheets with XML documents specification. [XMLSSPI]

とあり、とりあえず忘れられてはいないらしい。

DOM

同じ章に

In particular, the following features are defined in the WHATWG DOM standard: [DOM]
...
ProcessingInstruction interface

とあるように、DOM の ProcessingInstruction インタフェースもちゃんと参照されている。

HTML 構文

12.1 Writing HTML documents には処理命令に関する記載はない。つまり、HTML 構文で処理命令を書くことはできない。

12.2 Parsing HTML documents - 12.2.4.6 Tag open state には

Consume the next input character:
...
U+003F QUESTION MARK (?)
Parse error. Create a comment token whose data is the empty string. Reconsume in the bogus comment state.

とあり、パーサは (通常のタグが現れる文脈で) 文字列 "<?" が出現するとパースエラーと見なし、">" が現れるまでを偽のコメントとして処理することになっている (コメントノードのデータには冒頭の "?" が含まれる)。

一方、12.3 Serializing HTML fragments には

If current node is a ProcessingInstruction
Append the literal string "<?" (U+003C LESS-THAN SIGN, U+003F QUESTION MARK), followed by the value of current node's target IDL attribute, followed by a single U+0020 SPACE character, followed by the value of current node's data IDL attribute, followed by a single U+003E GREATER-THAN SIGN character (>).

とあり、ProcessingInstruction ノードは特にエラーなくシリアライズすることになっている (処理命令閉じが XML の "?>" ではなく SGML 規格参照の ">" なのが味わい深い)。

この辺は不整合のような気がするのだが、よくわからない。1.8 HTML vs XHTML でわざわざ例に挙げられている文字列 "-->" を含むコメントノードについても同様の不整合があるので、そういうものなのかもしれない。

もしくは documents と fragments の扱いの違いとかも関係するのかも知れないが、そこまでは面倒なので調べなかった。

XHTML構文

13.1 Writing XHTML documents には

This specification does not define any syntax-level requirements beyond those defined for XML proper.

という程度のことしか書かれていないので、おそらく XML 仕様で許される場所には処理命令を書いて問題ないと思われる。

13.2 Parsing XHTML documents にはこれといった規定はない。

13.3 Serializing XHTML fragments には、処理命令絡みでエラーとなるいくつかのケースが記載されている。

If any of the following error cases are found in the DOM subtree being serialized, then the algorithm must throw an "InvalidStateError" DOMException instead of returning a string:
...
A ProcessingInstruction node whose target name is an ASCII case-insensitive match for the string "xml".
A ProcessingInstruction node whose target name contains a U+003A COLON (:).
A ProcessingInstruction node whose data contains the string "?>".

これらはそのままシリアライズしようとすると XML 仕様ないし XML 名前空間仕様で整形式エラーとなるものである。

# XHTML 構文ではこのような規定があるので、HTML 構文でも ProcessingInstruction のシリアライズ自体エラーにしてしまえばいいような気がしてしまうのだが。

13.4 Parsing XHTML fragments にもこれといった規定はない。

おわりに

こうしてまとめてみると、わざわざ xml-stylesheet 処理命令の仕様を直接的に参照している意味があまりない気がするんですが、まあ歴史的なあれやこれやそれでこういう感じなんでしょうね。オワリ。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2