nekohtmlのパラメータを調べてみた

DOMParserの一種 nekohtml は色々なパラメータがありますが、その項目について調べてみました。

FacebookなんかにURLを貼り付けると、自動的にURL先のtitle,description,og:imageなどを取得してくれますが、そういう時に使うものになります。

Version 1.9.22 時点での情報です。

設定値

http://nekohtml.sourceforge.net/settings.html

http://xml.org/sax/features/namespaces

Specifies if the NekoHTML parser should perform namespace processing. If enabled, namespace binding attributes are processed and elements and attributes are bound to the defined namespaces.

“true”、”false”の値を設定します。

デフォルトは”true”のはずですが、動き的に”false”っぽいという話もあるので(バージョンによってデフォルト値が違うのかも)、気になるようであれば明示的に指定するといいかと思います。

HTMLパーサが名前空間処理を実行するかどうかを指定します。有効にした場合、名前空間バインディングの属性が処理され、要素と属性が定義された名前空間にバインドされます。

htmlが正しくないケースもなんとかパースさせたい場合は”false”にするとよさそう。

http://cyberneko.org/html/properties/names/elems

Specifies how the NekoHTML components should modify recognized element names. Names can be converted to upper-case, converted to lower-case, or left as-is. The value of “match” specifies that element names are to be left as-is but the end tag name will be modified to match the start tag name. This is required to ensure that the parser generates a well-formed XML document.

“upper”、”lower”、”match”の値を設定します。

デフォルトは”upper”

htmlタグの要素名

などを大文字、小文字などに変換します。”match”はそのままにする、という意味ではなく開始タグに合わせる、という意味なので、タグの開始と終了の対応がちゃんとしている必要があります。 “` <DIV>

→<DIV>

“`

ということでしょうか。

http://cyberneko.org/html/properties/names/attrs

Specifies how the NekoHTML components should modify attribute names of recognized elements. Names can be converted to upper-case, converted to lower-case, or left as-is.

“upper”、”lower”、”no-change”の値を設定します。

デフォルトは”lower”

タグのattributeの大文字/小文字の変換を行います。”no-change”はそのまま。

http://cyberneko.org/html/features/balance-tags

Specifies if the NekoHTML parser should attempt to balance the tags in the parsed document. Balancing the tags fixes up many common mistakes by adding missing parent elements, automatically closing elements with optional end tags, and correcting unbalanced inline element tags. In order to process HTML documents as XML, this feature should not be turned off. This feature is provided as a performance enhancement for applications that only care about the appearance of specific elements, attributes, and/or content regardless of the document’s ill-formed structure.

“true”、”false”の値を設定します。

デフォルトは”true”

NekoHTMLパーサーが解析された文書内のタグのバランスを取るしようとするかどうかを指定します。タグをバランスすると、自動的に、不足している親要素を追加するオプションの終了タグで要素を閉じて、アンバランスインライン要素タグを補正することにより、多くの一般的な間違いを修正します。

htmlを整形するかどうかということのようです。だめなhtmlも認めてあげる場合はfalseにしておきます。

http://cyberneko.org/html/features/balance-tags/ignore-outside-content

Version 1.9.17 以降から指定可能

Specifies if the NekoHTML parser should ignore content after the end of the document root element. If this feature is set to true, all elements and character content appearing outside of the document body is consumed. If set to false, the end elements for the and are ignored, allowing content appearing outside of the document to be parsed and communicated to the application.

“true”、”false”の値を設定します。

デフォルトは”false”

htmlタグ、bodyタグ以外の要素を無視するかどうかになります。”true”にすると変なタグがhtml,bodyタグの外にあっても影響されなくなります。

http://cyberneko.org/html/features/balance-tags/document-fragment

“true”、”false”の値を設定します。

デフォルトは”false”

htmlがぶっ壊れている時にhtmlの足りない要素を追加しよとするかどうかです。trueにすると余計なことをしようとしなくなります。ただしいずれに設定してもbodyタグの中の要素は上手いこと整形しようとするようです。

http://cyberneko.org/html/properties/filters

デフォルトは”null”

特定バージョン以降で設定値が使えるようになることがあるので、注意が必要です。

Change History

http://nekohtml.sourceforge.net/changes.html

エラーを含んだXMLをルーズにパースする

http://d.hatena.ne.jp/kaiseh/20090219/1235058388

cybernekoを使ってなるべく原型を留めてHTMLをDOMにパースするときのメモ

http://qiita.com/ak_nishiumi/items/56ce3c78868acfdcfb46

https://gist.github.com/aknishiumi/1714866