Track document types when parsing.
When parsing XML/HTML documents the corresponding document type (:html or :xml) is stored in Document#type.
This commit is contained in:
parent
bd322e8716
commit
6fc7e2e254
|
@ -12,11 +12,15 @@ module Oga
|
||||||
# The XML declaration of the document.
|
# The XML declaration of the document.
|
||||||
# @return [Oga::XML::XmlDeclaration]
|
# @return [Oga::XML::XmlDeclaration]
|
||||||
#
|
#
|
||||||
|
# @!attribute [rw] type
|
||||||
|
# The document type, either `:xml` or `:html`.
|
||||||
|
# @return [Symbol]
|
||||||
|
#
|
||||||
class Document
|
class Document
|
||||||
include Querying
|
include Querying
|
||||||
include Traversal
|
include Traversal
|
||||||
|
|
||||||
attr_accessor :doctype, :xml_declaration
|
attr_accessor :doctype, :xml_declaration, :type
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
|
@ -24,10 +28,12 @@ module Oga
|
||||||
# @option options [Oga::XML::NodeSet] :children
|
# @option options [Oga::XML::NodeSet] :children
|
||||||
# @option options [Oga::XML::Doctype] :doctype
|
# @option options [Oga::XML::Doctype] :doctype
|
||||||
# @option options [Oga::XML::XmlDeclaration] :xml_declaration
|
# @option options [Oga::XML::XmlDeclaration] :xml_declaration
|
||||||
|
# @option options [Symbol] :type
|
||||||
#
|
#
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@doctype = options[:doctype]
|
@doctype = options[:doctype]
|
||||||
@xml_declaration = options[:xml_declaration]
|
@xml_declaration = options[:xml_declaration]
|
||||||
|
@type = options[:type] || :xml
|
||||||
|
|
||||||
self.children = options[:children] if options[:children]
|
self.children = options[:children] if options[:children]
|
||||||
end
|
end
|
||||||
|
|
|
@ -296,7 +296,9 @@ Unexpected #{name} with value #{value.inspect} on line #{@line}:
|
||||||
# @return [Oga::XML::Document]
|
# @return [Oga::XML::Document]
|
||||||
#
|
#
|
||||||
def on_document(children = [])
|
def on_document(children = [])
|
||||||
document = Document.new
|
document = Document.new(
|
||||||
|
:type => @lexer.html ? :html : :xml
|
||||||
|
)
|
||||||
|
|
||||||
children.each do |child|
|
children.each do |child|
|
||||||
if child.is_a?(Doctype)
|
if child.is_a?(Doctype)
|
||||||
|
|
|
@ -8,6 +8,10 @@ describe Oga::XML::Document do
|
||||||
|
|
||||||
document.children[0].should == child
|
document.children[0].should == child
|
||||||
end
|
end
|
||||||
|
|
||||||
|
example 'set the document type' do
|
||||||
|
described_class.new(:type => :html).type.should == :html
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context '#children=' do
|
context '#children=' do
|
||||||
|
|
|
@ -11,6 +11,20 @@ describe Oga::XML::Parser do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'XML documents' do
|
||||||
|
before :all do
|
||||||
|
@document = parse('<foo></foo>')
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return a Document instance' do
|
||||||
|
@document.is_a?(Oga::XML::Document).should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the document type' do
|
||||||
|
@document.type.should == :xml
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'HTML documents' do
|
context 'HTML documents' do
|
||||||
before :all do
|
before :all do
|
||||||
html = <<-EOF.strip
|
html = <<-EOF.strip
|
||||||
|
@ -31,6 +45,10 @@ describe Oga::XML::Parser do
|
||||||
@document.is_a?(Oga::XML::Document).should == true
|
@document.is_a?(Oga::XML::Document).should == true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
example 'set the document type' do
|
||||||
|
@document.type.should == :html
|
||||||
|
end
|
||||||
|
|
||||||
example 'set the doctype of the document' do
|
example 'set the doctype of the document' do
|
||||||
@document.doctype.is_a?(Oga::XML::Doctype).should == true
|
@document.doctype.is_a?(Oga::XML::Doctype).should == true
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue