2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/treewalkers/base'
|
2007-06-05 23:34:49 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
2007-06-05 23:34:49 +02:00
|
|
|
module TreeWalkers
|
|
|
|
module SimpleTree
|
2007-07-05 00:36:59 +02:00
|
|
|
class TreeWalker < HTML5::TreeWalkers::Base
|
|
|
|
include HTML5::TreeBuilders::SimpleTree
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
def walk(node)
|
|
|
|
case node
|
|
|
|
when Document, DocumentFragment
|
|
|
|
return
|
|
|
|
|
|
|
|
when DocumentType
|
2007-08-30 19:19:10 +02:00
|
|
|
yield doctype(node.name, node.public_id, node.system_id)
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
when TextNode
|
|
|
|
text(node.value) {|token| yield token}
|
|
|
|
|
|
|
|
when Element
|
|
|
|
if VOID_ELEMENTS.include?(node.name)
|
2007-08-30 19:19:10 +02:00
|
|
|
yield empty_tag(node.name, node.attributes, node.hasContent())
|
2007-06-05 23:34:49 +02:00
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
yield start_tag(node.name, node.attributes)
|
2007-06-05 23:34:49 +02:00
|
|
|
for child in node.childNodes
|
|
|
|
walk(child) {|token| yield token}
|
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
yield end_tag(node.name)
|
2007-06-05 23:34:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
when CommentNode
|
|
|
|
yield comment(node.value)
|
|
|
|
|
|
|
|
else
|
|
|
|
puts '?'
|
|
|
|
yield unknown(node.class)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
for child in @tree.childNodes
|
|
|
|
walk(child) {|node| yield node}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|