2007-05-26 03:52:27 +02:00
|
|
|
# Warning: this module is experimental and subject to change and even removal
|
|
|
|
# at any time.
|
|
|
|
#
|
|
|
|
# For background/rationale, see:
|
|
|
|
# * http://www.intertwingly.net/blog/2007/01/08/Xhtml5lib
|
|
|
|
# * http://tinyurl.com/ylfj8k (and follow-ups)
|
|
|
|
#
|
|
|
|
# References:
|
|
|
|
# * http://googlereader.blogspot.com/2005/12/xml-errors-in-feeds.html
|
|
|
|
# * http://wiki.whatwg.org/wiki/HtmlVsXhtml
|
|
|
|
#
|
|
|
|
# @@TODO:
|
|
|
|
# * Selectively lowercase only XHTML, but not foreign markup
|
2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/html5parser'
|
|
|
|
require 'html5/constants'
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# liberal XML parser
|
|
|
|
class XMLParser < HTMLParser
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize(options = {})
|
|
|
|
super options
|
|
|
|
@phases[:initial] = XmlRootPhase.new(self, @tree)
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def normalize_token(token)
|
2007-07-05 00:36:59 +02:00
|
|
|
case token[:type]
|
|
|
|
when :StartTag, :EmptyTag
|
2007-05-30 17:45:52 +02:00
|
|
|
# We need to remove the duplicate attributes and convert attributes
|
2007-07-05 00:36:59 +02:00
|
|
|
# to a Hash so that [["x", "y"], ["x", "z"]] becomes {"x": "y"}
|
2007-05-30 17:45:52 +02:00
|
|
|
|
|
|
|
token[:data] = Hash[*token[:data].reverse.flatten]
|
|
|
|
|
|
|
|
# For EmptyTags, process both a Start and an End tag
|
|
|
|
if token[:type] == :EmptyTag
|
2007-08-30 19:19:10 +02:00
|
|
|
save = @tokenizer.content_model_flag
|
2007-05-30 17:45:52 +02:00
|
|
|
@phase.processStartTag(token[:name], token[:data])
|
2007-08-30 19:19:10 +02:00
|
|
|
@tokenizer.content_model_flag = save
|
2007-05-30 17:45:52 +02:00
|
|
|
token[:data] = {}
|
|
|
|
token[:type] = :EndTag
|
|
|
|
end
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
when :Characters
|
|
|
|
# un-escape RCDATA_ELEMENTS (e.g. style, script)
|
2007-08-30 19:19:10 +02:00
|
|
|
if @tokenizer.content_model_flag == :CDATA
|
2007-07-05 00:36:59 +02:00
|
|
|
token[:data] = token[:data].
|
|
|
|
gsub('<','<').gsub('>','>').gsub('&','&')
|
|
|
|
end
|
|
|
|
|
|
|
|
when :EndTag
|
2007-05-30 17:45:52 +02:00
|
|
|
if token[:data]
|
2007-08-30 19:19:10 +02:00
|
|
|
parse_error(_("End tag contains unexpected attributes."))
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
when :Comment
|
2007-05-30 17:45:52 +02:00
|
|
|
# Rescue CDATA from the comments
|
|
|
|
if token[:data][0..6] == "[CDATA[" and token[:data][-2..-1] == "]]"
|
|
|
|
token[:type] = :Characters
|
|
|
|
token[:data] = token[:data][7 ... -2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return token
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# liberal XMTHML parser
|
|
|
|
class XHTMLParser < XMLParser
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize(options = {})
|
|
|
|
super options
|
|
|
|
@phases[:initial] = InitialPhase.new(self, @tree)
|
|
|
|
@phases[:rootElement] = XhmlRootPhase.new(self, @tree)
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def normalize_token(token)
|
2007-05-30 17:45:52 +02:00
|
|
|
super(token)
|
|
|
|
|
|
|
|
# ensure that non-void XHTML elements have content so that separate
|
|
|
|
# open and close tags are emitted
|
2007-06-22 10:12:08 +02:00
|
|
|
if token[:type] == :EndTag
|
|
|
|
if VOID_ELEMENTS.include? token[:name]
|
2007-08-30 19:19:10 +02:00
|
|
|
if @tree.open_elements[-1].name != token["name"]:
|
2007-06-22 10:12:08 +02:00
|
|
|
token[:type] = :EmptyTag
|
|
|
|
token["data"] ||= {}
|
|
|
|
end
|
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
if token[:name] == @tree.open_elements[-1].name and \
|
|
|
|
not @tree.open_elements[-1].hasContent
|
2007-06-22 10:12:08 +02:00
|
|
|
@tree.insertText('') unless
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.any? {|e|
|
2007-06-22 10:12:08 +02:00
|
|
|
e.attributes.keys.include? 'xmlns' and
|
|
|
|
e.attributes['xmlns'] != 'http://www.w3.org/1999/xhtml'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return token
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class XhmlRootPhase < RootElementPhase
|
2007-08-30 19:19:10 +02:00
|
|
|
def insert_html_element
|
2007-05-30 17:45:52 +02:00
|
|
|
element = @tree.createElement("html", {'xmlns' => 'http://www.w3.org/1999/xhtml'})
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.push(element)
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.document.appendChild(element)
|
|
|
|
@parser.phase = @parser.phases[:beforeHead]
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class XmlRootPhase < Phase
|
2007-05-26 03:52:27 +02:00
|
|
|
# Prime the Xml parser
|
|
|
|
@start_tag_handlers = Hash.new(:startTagOther)
|
|
|
|
@end_tag_handlers = Hash.new(:endTagOther)
|
|
|
|
def startTagOther(name, attributes)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.push(@tree.document)
|
2007-05-30 17:45:52 +02:00
|
|
|
element = @tree.createElement(name, attributes)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements[-1].appendChild(element)
|
|
|
|
@tree.open_elements.push(element)
|
2007-05-30 17:45:52 +02:00
|
|
|
@parser.phase = XmlElementPhase.new(@parser,@tree)
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
def endTagOther(name)
|
2007-05-30 17:45:52 +02:00
|
|
|
super
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.pop
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class XmlElementPhase < Phase
|
2007-05-26 03:52:27 +02:00
|
|
|
# Generic handling for all XML elements
|
|
|
|
|
|
|
|
@start_tag_handlers = Hash.new(:startTagOther)
|
|
|
|
@end_tag_handlers = Hash.new(:endTagOther)
|
|
|
|
|
|
|
|
def startTagOther(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
element = @tree.createElement(name, attributes)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements[-1].appendChild(element)
|
|
|
|
@tree.open_elements.push(element)
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def endTagOther(name)
|
2007-08-30 19:19:10 +02:00
|
|
|
for node in @tree.open_elements.reverse
|
2007-05-30 17:45:52 +02:00
|
|
|
if node.name == name
|
2007-08-30 19:19:10 +02:00
|
|
|
{} while @tree.open_elements.pop != node
|
2007-05-30 17:45:52 +02:00
|
|
|
break
|
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
parse_error
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def processCharacters(data)
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.insertText(data)
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
|
|
|
end
|