2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/treewalkers/base'
|
2007-06-05 23:34:49 +02:00
|
|
|
require 'rexml/document'
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
2007-06-05 23:34:49 +02:00
|
|
|
module TreeWalkers
|
|
|
|
module Hpricot
|
2007-07-05 00:36:59 +02:00
|
|
|
class TreeWalker < HTML5::TreeWalkers::NonRecursiveTreeWalker
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
def node_details(node)
|
|
|
|
case node
|
|
|
|
when ::Hpricot::Elem
|
2007-06-22 10:12:08 +02:00
|
|
|
if node.name.empty?
|
2007-06-05 23:34:49 +02:00
|
|
|
[:DOCUMENT_FRAGMENT]
|
|
|
|
else
|
|
|
|
[:ELEMENT, node.name,
|
2007-08-30 19:19:10 +02:00
|
|
|
node.attributes.map {|name, value| [name, value]},
|
2007-06-05 23:34:49 +02:00
|
|
|
!node.empty?]
|
|
|
|
end
|
|
|
|
when ::Hpricot::Text
|
2007-08-30 19:19:10 +02:00
|
|
|
[:TEXT, node.content]
|
2007-06-05 23:34:49 +02:00
|
|
|
when ::Hpricot::Comment
|
|
|
|
[:COMMENT, node.content]
|
|
|
|
when ::Hpricot::Doc
|
|
|
|
[:DOCUMENT]
|
|
|
|
when ::Hpricot::DocType
|
2007-08-30 19:19:10 +02:00
|
|
|
[:DOCTYPE, node.target, node.public_id, node.system_id]
|
2007-06-05 23:34:49 +02:00
|
|
|
when ::Hpricot::XMLDecl
|
|
|
|
[nil]
|
|
|
|
else
|
|
|
|
[:UNKNOWN, node.class.inspect]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_child(node)
|
|
|
|
node.children.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_sibling(node)
|
|
|
|
node.next_node
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent(node)
|
|
|
|
node.parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|