diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 1a56c39..0c824a2 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -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 diff --git a/lib/oga/xml/parser.y b/lib/oga/xml/parser.y index 41c9fa3..c627104 100644 --- a/lib/oga/xml/parser.y +++ b/lib/oga/xml/parser.y @@ -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) diff --git a/spec/oga/xml/document_spec.rb b/spec/oga/xml/document_spec.rb index 62df667..4f33f32 100644 --- a/spec/oga/xml/document_spec.rb +++ b/spec/oga/xml/document_spec.rb @@ -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 diff --git a/spec/oga/xml/parser/documents_spec.rb b/spec/oga/xml/parser/documents_spec.rb index 298a294..4479233 100644 --- a/spec/oga/xml/parser/documents_spec.rb +++ b/spec/oga/xml/parser/documents_spec.rb @@ -11,6 +11,20 @@ describe Oga::XML::Parser do end end + context 'XML documents' do + before :all do + @document = parse('') + 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