2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/constants'
|
|
|
|
require 'html5/tokenizer'
|
|
|
|
require 'html5/treebuilders/rexml'
|
|
|
|
|
|
|
|
Dir.glob(File.join(File.dirname(__FILE__), 'html5parser', '*_phase.rb')).each do |path|
|
|
|
|
require 'html5/html5parser/' + File.basename(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
module HTML5
|
|
|
|
|
|
|
|
# Error in parsed document
|
|
|
|
class ParseError < Exception; end
|
|
|
|
class AssertionError < Exception; end
|
|
|
|
|
|
|
|
# HTML parser. Generates a tree structure from a stream of (possibly malformed) HTML
|
|
|
|
#
|
|
|
|
class HTMLParser
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
attr_accessor :phase, :first_start_tag, :inner_html, :last_phase, :insert_from_table
|
2007-07-05 00:36:59 +02:00
|
|
|
|
|
|
|
attr_reader :phases, :tokenizer, :tree, :errors
|
|
|
|
|
|
|
|
def self.parse(stream, options = {})
|
|
|
|
encoding = options.delete(:encoding)
|
|
|
|
new(options).parse(stream,encoding)
|
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def self.parse_fragment(stream, options = {})
|
2007-07-05 00:36:59 +02:00
|
|
|
container = options.delete(:container) || 'div'
|
|
|
|
encoding = options.delete(:encoding)
|
2007-08-30 19:19:10 +02:00
|
|
|
new(options).parse_fragment(stream, container, encoding)
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@@phases = %w( initial rootElement beforeHead inHead afterHead inBody inTable inCaption
|
|
|
|
inColumnGroup inTableBody inRow inCell inSelect afterBody inFrameset afterFrameset trailingEnd )
|
|
|
|
|
|
|
|
# :strict - raise an exception when a parse error is encountered
|
|
|
|
# :tree - a treebuilder class controlling the type of tree that will be
|
|
|
|
# returned. Built in treebuilders can be accessed through
|
|
|
|
# HTML5::TreeBuilders[treeType]
|
|
|
|
def initialize(options = {})
|
|
|
|
@strict = false
|
|
|
|
@errors = []
|
|
|
|
|
|
|
|
@tokenizer = HTMLTokenizer
|
|
|
|
@tree = TreeBuilders::REXML::TreeBuilder
|
2007-08-30 19:19:10 +02:00
|
|
|
|
|
|
|
options.each {|name, value| instance_variable_set("@#{name}", value) }
|
|
|
|
@lowercase_attr_name = nil unless instance_variables.include?("@lowercase_attr_name")
|
|
|
|
@lowercase_element_name = nil unless instance_variables.include?("@lowercase_element_name")
|
2007-07-05 00:36:59 +02:00
|
|
|
|
|
|
|
@tree = @tree.new
|
|
|
|
|
|
|
|
@phases = @@phases.inject({}) do |phases, phase_name|
|
|
|
|
phase_class_name = phase_name.sub(/(.)/) { $1.upcase } + 'Phase'
|
|
|
|
phases[phase_name.to_sym] = HTML5.const_get(phase_class_name).new(self, @tree)
|
2007-08-30 19:19:10 +02:00
|
|
|
phases
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def _parse(stream, inner_html, encoding, container = 'div')
|
2007-07-05 00:36:59 +02:00
|
|
|
@tree.reset
|
2007-08-30 19:19:10 +02:00
|
|
|
@first_start_tag = false
|
2007-07-05 00:36:59 +02:00
|
|
|
@errors = []
|
|
|
|
|
|
|
|
@tokenizer = @tokenizer.class unless Class === @tokenizer
|
|
|
|
@tokenizer = @tokenizer.new(stream, :encoding => encoding,
|
2007-08-30 19:19:10 +02:00
|
|
|
:parseMeta => !inner_html, :lowercase_attr_name => @lowercase_attr_name, :lowercase_element_name => @lowercase_element_name)
|
2007-07-05 00:36:59 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
if inner_html
|
|
|
|
case @inner_html = container.downcase
|
2007-10-06 18:55:58 +02:00
|
|
|
when 'title', 'textarea'
|
|
|
|
@tokenizer.content_model_flag = :RCDATA
|
|
|
|
when 'style', 'script', 'xmp', 'iframe', 'noembed', 'noframes', 'noscript'
|
|
|
|
@tokenizer.content_model_flag = :CDATA
|
|
|
|
when 'plaintext'
|
|
|
|
@tokenizer.content_model_flag = :PLAINTEXT
|
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
# content_model_flag already is PCDATA
|
2007-10-06 18:55:58 +02:00
|
|
|
@tokenizer.content_model_flag = :PCDATA
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@phase = @phases[:rootElement]
|
2007-08-30 19:19:10 +02:00
|
|
|
@phase.insert_html_element
|
|
|
|
reset_insertion_mode
|
2007-07-05 00:36:59 +02:00
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
@inner_html = false
|
2007-07-05 00:36:59 +02:00
|
|
|
@phase = @phases[:initial]
|
|
|
|
end
|
|
|
|
|
|
|
|
# We only seem to have InBodyPhase testcases where the following is
|
|
|
|
# relevant ... need others too
|
2007-08-30 19:19:10 +02:00
|
|
|
@last_phase = nil
|
2007-07-05 00:36:59 +02:00
|
|
|
|
|
|
|
# XXX This is temporary for the moment so there isn't any other
|
|
|
|
# changes needed for the parser to work with the iterable tokenizer
|
|
|
|
@tokenizer.each do |token|
|
2007-08-30 19:19:10 +02:00
|
|
|
token = normalize_token(token)
|
2007-07-05 00:36:59 +02:00
|
|
|
|
|
|
|
method = 'process%s' % token[:type]
|
|
|
|
|
|
|
|
case token[:type]
|
2007-09-10 05:26:19 +02:00
|
|
|
when :Characters, :SpaceCharacters, :Comment
|
|
|
|
@phase.send method, token[:data]
|
|
|
|
when :StartTag
|
|
|
|
@phase.send method, token[:name], token[:data]
|
|
|
|
when :EndTag
|
|
|
|
@phase.send method, token[:name]
|
|
|
|
when :Doctype
|
|
|
|
@phase.send method, token[:name], token[:publicId],
|
|
|
|
token[:systemId], token[:correct]
|
|
|
|
else
|
|
|
|
parse_error(token[:data], token[:datavars])
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# When the loop finishes it's EOF
|
2007-08-30 19:19:10 +02:00
|
|
|
@phase.process_eof
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Parse a HTML document into a well-formed tree
|
|
|
|
#
|
|
|
|
# stream - a filelike object or string containing the HTML to be parsed
|
|
|
|
#
|
|
|
|
# The optional encoding parameter must be a string that indicates
|
|
|
|
# the encoding. If specified, that encoding will be used,
|
|
|
|
# regardless of any BOM or later declaration (such as in a meta
|
|
|
|
# element)
|
|
|
|
def parse(stream, encoding=nil)
|
|
|
|
_parse(stream, false, encoding)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.get_document
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
# Parse a HTML fragment into a well-formed tree fragment
|
2007-08-30 19:19:10 +02:00
|
|
|
|
|
|
|
# container - name of the element we're setting the inner_html property
|
2007-07-05 00:36:59 +02:00
|
|
|
# if set to nil, default to 'div'
|
|
|
|
#
|
|
|
|
# stream - a filelike object or string containing the HTML to be parsed
|
|
|
|
#
|
|
|
|
# The optional encoding parameter must be a string that indicates
|
|
|
|
# the encoding. If specified, that encoding will be used,
|
|
|
|
# regardless of any BOM or later declaration (such as in a meta
|
|
|
|
# element)
|
2007-08-30 19:19:10 +02:00
|
|
|
def parse_fragment(stream, container='div', encoding=nil)
|
2007-07-05 00:36:59 +02:00
|
|
|
_parse(stream, true, encoding, container)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.get_fragment
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
|
2007-09-10 05:26:19 +02:00
|
|
|
def parse_error(code = 'XXX-undefined-error', data = {})
|
2007-07-05 00:36:59 +02:00
|
|
|
# XXX The idea is to make data mandatory.
|
2007-09-10 05:26:19 +02:00
|
|
|
@errors.push([@tokenizer.stream.position, code, data])
|
2007-07-05 00:36:59 +02:00
|
|
|
raise ParseError if @strict
|
|
|
|
end
|
|
|
|
|
|
|
|
# HTML5 specific normalizations to the token stream
|
2007-08-30 19:19:10 +02:00
|
|
|
def normalize_token(token)
|
2007-07-05 00:36:59 +02:00
|
|
|
|
|
|
|
if token[:type] == :EmptyTag
|
|
|
|
# When a solidus (/) is encountered within a tag name what happens
|
|
|
|
# depends on whether the current tag name matches that of a void
|
|
|
|
# element. If it matches a void element atheists did the wrong
|
|
|
|
# thing and if it doesn't it's wrong for everyone.
|
|
|
|
|
|
|
|
unless VOID_ELEMENTS.include?(token[:name])
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("incorrectly-placed-solidus")
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
token[:type] = :StartTag
|
|
|
|
end
|
|
|
|
|
|
|
|
if token[:type] == :StartTag
|
2007-08-30 19:19:10 +02:00
|
|
|
token[:name] = token[:name].downcase
|
2007-07-05 00:36:59 +02:00
|
|
|
|
|
|
|
# We need to remove the duplicate attributes and convert attributes
|
|
|
|
# to a dict so that [["x", "y"], ["x", "z"]] becomes {"x": "y"}
|
|
|
|
|
|
|
|
unless token[:data].empty?
|
2007-08-30 19:19:10 +02:00
|
|
|
data = token[:data].reverse.map {|attr, value| [attr.downcase, value] }
|
2007-07-05 00:36:59 +02:00
|
|
|
token[:data] = Hash[*data.flatten]
|
|
|
|
end
|
|
|
|
|
|
|
|
elsif token[:type] == :EndTag
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("attributes-in-end-tag") unless token[:data].empty?
|
2007-07-05 00:36:59 +02:00
|
|
|
token[:name] = token[:name].downcase
|
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
token
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@@new_modes = {
|
2007-08-30 19:19:10 +02:00
|
|
|
'select' => :inSelect,
|
|
|
|
'td' => :inCell,
|
|
|
|
'th' => :inCell,
|
|
|
|
'tr' => :inRow,
|
|
|
|
'tbody' => :inTableBody,
|
|
|
|
'thead' => :inTableBody,
|
|
|
|
'tfoot' => :inTableBody,
|
|
|
|
'caption' => :inCaption,
|
2007-07-05 00:36:59 +02:00
|
|
|
'colgroup' => :inColumnGroup,
|
2007-08-30 19:19:10 +02:00
|
|
|
'table' => :inTable,
|
|
|
|
'head' => :inBody,
|
|
|
|
'body' => :inBody,
|
2007-07-05 00:36:59 +02:00
|
|
|
'frameset' => :inFrameset
|
|
|
|
}
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def reset_insertion_mode
|
2007-07-05 00:36:59 +02:00
|
|
|
# The name of this method is mostly historical. (It's also used in the
|
|
|
|
# specification.)
|
|
|
|
last = false
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.reverse.each do |node|
|
|
|
|
node_name = node.name
|
2007-07-05 00:36:59 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
if node == @tree.open_elements.first
|
2007-07-05 00:36:59 +02:00
|
|
|
last = true
|
2007-08-30 19:19:10 +02:00
|
|
|
unless ['td', 'th'].include?(node_name)
|
2007-07-05 00:36:59 +02:00
|
|
|
# XXX
|
2007-08-30 19:19:10 +02:00
|
|
|
# assert @inner_html
|
|
|
|
node_name = @inner_html
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
# Check for conditions that should only happen in the inner_html
|
2007-07-05 00:36:59 +02:00
|
|
|
# case
|
2007-08-30 19:19:10 +02:00
|
|
|
if ['select', 'colgroup', 'head', 'frameset'].include?(node_name)
|
2007-07-05 00:36:59 +02:00
|
|
|
# XXX
|
2007-08-30 19:19:10 +02:00
|
|
|
# assert @inner_html
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
if @@new_modes.has_key?(node_name)
|
|
|
|
@phase = @phases[@@new_modes[node_name]]
|
|
|
|
elsif node_name == 'html'
|
|
|
|
@phase = @phases[@tree.head_pointer.nil?? :beforeHead : :afterHead]
|
2007-07-05 00:36:59 +02:00
|
|
|
elsif last
|
|
|
|
@phase = @phases[:inBody]
|
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _(string); string; end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|