diff --git a/lib/oga.rb b/lib/oga.rb index 3bd9081..a23a359 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -35,8 +35,8 @@ require 'oga/xml/character_node' require 'oga/xml/text' require 'oga/xml/comment' require 'oga/xml/cdata' -require 'oga/xml/xml_declaration' require 'oga/xml/processing_instruction' +require 'oga/xml/xml_declaration' require 'oga/xml/doctype' require 'oga/xml/namespace' require 'oga/xml/default_namespace' diff --git a/lib/oga/xml/generator.rb b/lib/oga/xml/generator.rb index 27d81bb..547b4d2 100644 --- a/lib/oga/xml/generator.rb +++ b/lib/oga/xml/generator.rb @@ -48,12 +48,14 @@ module Oga callback = :on_comment when Oga::XML::Attribute callback = :on_attribute + when Oga::XML::XmlDeclaration + # This must come before ProcessingInstruction since XmlDeclaration + # extends ProcessingInstruction. + callback = :on_xml_declaration when Oga::XML::ProcessingInstruction callback = :on_processing_instruction when Oga::XML::Doctype callback = :on_doctype - when Oga::XML::XmlDeclaration - callback = :on_xml_declaration when Oga::XML::Document callback = :on_document children = true diff --git a/lib/oga/xml/xml_declaration.rb b/lib/oga/xml/xml_declaration.rb index f5b6268..8ffc803 100644 --- a/lib/oga/xml/xml_declaration.rb +++ b/lib/oga/xml/xml_declaration.rb @@ -1,9 +1,7 @@ module Oga module XML # Class containing information about an XML declaration tag. - class XmlDeclaration - include ToXML - + class XmlDeclaration < ProcessingInstruction # @return [String] attr_accessor :version @@ -20,9 +18,12 @@ module Oga # @option options [String] :encoding # @option options [String] :standalone def initialize(options = {}) + super + @version = options[:version] || '1.0' @encoding = options[:encoding] || 'UTF-8' @standalone = options[:standalone] + @name = 'xml' end # @return [String] diff --git a/spec/oga/xml/parser/xml_declaration_spec.rb b/spec/oga/xml/parser/xml_declaration_spec.rb index 38c13fb..a410e02 100644 --- a/spec/oga/xml/parser/xml_declaration_spec.rb +++ b/spec/oga/xml/parser/xml_declaration_spec.rb @@ -36,4 +36,28 @@ describe Oga::XML::Parser do @node.encoding.should == 'foo' end end + + it 'parses an XML declaration inside an element' do + document = parse('') + + document.children[0].should be_an_instance_of(Oga::XML::Element) + document.children[0].children[0].should be_an_instance_of(Oga::XML::XmlDeclaration) + end + + it 'parses an XML declaration preceded by an element' do + document = parse('') + + document.xml_declaration.should be_an_instance_of(Oga::XML::XmlDeclaration) + document.children[0].should be_an_instance_of(Oga::XML::Element) + end + + it 'parses an XML declaration preceded by an element with a common ancestor' do + document = parse('') + root = document.children[0] + + root.children[0].should be_an_instance_of(Oga::XML::Element) + root.children[1].should be_an_instance_of(Oga::XML::XmlDeclaration) + + root.children[1].previous.should == root.children[0] + end end diff --git a/spec/oga/xml/xml_declaration_spec.rb b/spec/oga/xml/xml_declaration_spec.rb index ebdc802..094f287 100644 --- a/spec/oga/xml/xml_declaration_spec.rb +++ b/spec/oga/xml/xml_declaration_spec.rb @@ -28,6 +28,12 @@ describe Oga::XML::XmlDeclaration do end end + describe '#name' do + it 'returns the name of the node' do + described_class.new.name.should == 'xml' + end + end + describe '#to_xml' do before do @instance = described_class.new( diff --git a/spec/oga/xpath/compiler/type_tests/processing_instruction_spec.rb b/spec/oga/xpath/compiler/type_tests/processing_instruction_spec.rb index 83b49ba..6d9e4c6 100644 --- a/spec/oga/xpath/compiler/type_tests/processing_instruction_spec.rb +++ b/spec/oga/xpath/compiler/type_tests/processing_instruction_spec.rb @@ -1,24 +1,36 @@ require 'spec_helper' describe Oga::XPath::Compiler do - before do - @document = parse('') + describe 'using a regular processing instruction' do + before do + @document = parse('') - @proc_ins1 = @document.children[0].children[0] - @proc_ins2 = @document.children[0].children[1].children[0] - end - - describe 'relative to a document' do - describe 'a/processing-instruction()' do - it 'returns a NodeSet' do - evaluate_xpath(@document).should == node_set(@proc_ins1) - end + @proc_ins1 = @document.children[0].children[0] + @proc_ins2 = @document.children[0].children[1].children[0] end - describe 'a/b/processing-instruction()' do - it 'returns a NodeSet' do - evaluate_xpath(@document).should == node_set(@proc_ins2) + describe 'relative to a document' do + describe 'a/processing-instruction()' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@proc_ins1) + end end + + describe 'a/b/processing-instruction()' do + it 'returns a NodeSet' do + evaluate_xpath(@document).should == node_set(@proc_ins2) + end + end + end + end + + describe 'using an XML declaration' do + it 'returns a NodeSet containing the XmlDeclaration' do + document = parse('') + xml_decl = document.children[0].children[0] + + evaluate_xpath(document, 'root/processing-instruction()') + .should == node_set(xml_decl) end end end