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