W3CがホスティングしているNu Markup Checkerを使ってローカルのHTMLファイルをバリデーションにかけるNode.jsスクリプトを書き直していた。だいたい大丈夫そう。Gruntタスク……はやろうとすると遅すぎて死ぬのでやめよう。

Download: validate-html.js

利用にはrequestパッケージの別途インストールが必要。Browserifyを使って依存解決したスクリプトにしようとしたら、requestパッケージがまだBrowserifyに対応していなかった……。


ValidatorのAPIは本家と同じで、送信するHTTPヘッダとしてUser-Agentが必須なこと以外は特に注意は必要ない。

W3CでホスティングされているものはValidator.nuのものと少し違って、HTML5の仕様書に準拠したものになっている。わかりやすいところではhgroup要素が通らなかったりとか。他にも仕様書に従って様々な警告が発せられる。例えば見出し要素のないセクションがあったり、h1要素をそこかしこで使うと以下の様な警告が(大量に)吐かれる。

test.html:1024:7:Section lacks heading. Consider using “h2”-“h6” elements to add identifying headings to all sections.
test.html:84:9:Consider using the “h1” element as a top-level heading only (all “h1” elements are treated as top-level headings by many screen readers and other tools).
test.html:128:9:Consider using the “h1” element as a top-level heading only (all “h1” elements are treated as top-level headings by many screen readers and other tools).
test.html:232:9:Consider using the “h1” element as a top-level heading only (all “h1” elements are treated as top-level headings by many screen readers and other tools).

あっはい。っていう感じになるので、このスクリプトではエラーのみを吐くようにしている。APIからはこの辺の制御はできないようなので、本来はVimなどではGNUやTextで結果を返してもらってそのまま流せば良いはずなのだけど、そうもいかない。