Dedicated handling of XML declaration nodes.
This commit is contained in:
parent
d6c0a1f3f3
commit
bdb76cefc5
|
@ -10,6 +10,7 @@ require_relative 'oga/xml/document'
|
||||||
require_relative 'oga/xml/text'
|
require_relative 'oga/xml/text'
|
||||||
require_relative 'oga/xml/comment'
|
require_relative 'oga/xml/comment'
|
||||||
require_relative 'oga/xml/cdata'
|
require_relative 'oga/xml/cdata'
|
||||||
|
require_relative 'oga/xml/xml_declaration'
|
||||||
require_relative 'oga/xml/tree_builder'
|
require_relative 'oga/xml/tree_builder'
|
||||||
|
|
||||||
require_relative 'oga/html/parser'
|
require_relative 'oga/html/parser'
|
||||||
|
|
|
@ -4,10 +4,16 @@ module Oga
|
||||||
# Class description
|
# Class description
|
||||||
#
|
#
|
||||||
class Document < Node
|
class Document < Node
|
||||||
attr_accessor :dtd, :xml_version, :encoding
|
attr_accessor :xml_declaration
|
||||||
|
|
||||||
def to_xml
|
def to_xml
|
||||||
return children.map(&:to_xml).join('')
|
xml = children.map(&:to_xml).join('')
|
||||||
|
|
||||||
|
if xml_declaration
|
||||||
|
xml = xml_declaration.to_xml + xml
|
||||||
|
end
|
||||||
|
|
||||||
|
return xml
|
||||||
end
|
end
|
||||||
end # Document
|
end # Document
|
||||||
end # XML
|
end # XML
|
||||||
|
|
|
@ -18,7 +18,14 @@ module Oga
|
||||||
#
|
#
|
||||||
def on_document(node)
|
def on_document(node)
|
||||||
document = Document.new
|
document = Document.new
|
||||||
document.children = process_all(node)
|
|
||||||
|
process_all(node).each do |child|
|
||||||
|
if child.is_a?(XmlDeclaration)
|
||||||
|
document.xml_declaration = child
|
||||||
|
else
|
||||||
|
document.children << child
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
document.children.each do |child|
|
document.children.each do |child|
|
||||||
child.parent = document
|
child.parent = document
|
||||||
|
@ -27,6 +34,16 @@ module Oga
|
||||||
return document
|
return document
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Oga::AST::Node] node
|
||||||
|
# @return [Oga::XML::XmlDeclaration]
|
||||||
|
#
|
||||||
|
def on_xml_decl(node)
|
||||||
|
attributes = process(node.children[0])
|
||||||
|
|
||||||
|
return XmlDeclaration.new(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Oga::AST::Node] node
|
# @param [Oga::AST::Node] node
|
||||||
# @return [Oga::XML::Comment]
|
# @return [Oga::XML::Comment]
|
||||||
|
@ -103,6 +120,10 @@ module Oga
|
||||||
return *node
|
return *node
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handler_missing(node)
|
||||||
|
raise "No handler for node type #{node.type.inspect}"
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
module Oga
|
||||||
|
module XML
|
||||||
|
##
|
||||||
|
#
|
||||||
|
#
|
||||||
|
class XmlDeclaration
|
||||||
|
attr_accessor :version, :encoding, :standalone
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Hash] options
|
||||||
|
#
|
||||||
|
# @option options [String] :version The XML version.
|
||||||
|
# @option options [String] :encoding The XML encoding.
|
||||||
|
# @option options [String] :standalone
|
||||||
|
#
|
||||||
|
def initialize(options = {})
|
||||||
|
options.each do |key, value|
|
||||||
|
instance_variable_set("@#{key}", value) if respond_to?(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
@version ||= '1.0'
|
||||||
|
@encoding ||= 'UTF-8'
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_xml
|
||||||
|
pairs = []
|
||||||
|
|
||||||
|
[:version, :encoding, :standalone].each do |getter|
|
||||||
|
value = send(getter)
|
||||||
|
|
||||||
|
pairs << %Q{#{getter}="#{value}"} if value
|
||||||
|
end
|
||||||
|
|
||||||
|
return "<?xml #{pairs.join(' ')} ?>"
|
||||||
|
end
|
||||||
|
end # XmlDeclaration
|
||||||
|
end # XML
|
||||||
|
end # Oga
|
|
@ -29,4 +29,21 @@ describe Oga::XML::Document do
|
||||||
@document.to_xml.should == '<!--foo-->'
|
@document.to_xml.should == '<!--foo-->'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#to_xml with XML declarations' do
|
||||||
|
before do
|
||||||
|
decl = Oga::XML::XmlDeclaration.new(:version => '5.0')
|
||||||
|
children = [Oga::XML::Comment.new(:text => 'foo')]
|
||||||
|
|
||||||
|
@document = described_class.new(
|
||||||
|
:xml_declaration => decl,
|
||||||
|
:children => children
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the XML of the declaration tag' do
|
||||||
|
@document.to_xml
|
||||||
|
.should == '<?xml version="5.0" encoding="UTF-8" ?><!--foo-->'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,6 +20,33 @@ describe Oga::XML::TreeBuilder do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#on_document with XML declarations' do
|
||||||
|
before do
|
||||||
|
decl = s(:xml_decl, s(:attributes, s(:attribute, 'encoding', 'UTF-8')))
|
||||||
|
node = s(:document, decl)
|
||||||
|
@tag = @builder.process(node)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the XML declaration of the document' do
|
||||||
|
@tag.xml_declaration.is_a?(Oga::XML::XmlDeclaration).should == true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context '#on_xml_decl' do
|
||||||
|
before do
|
||||||
|
node = s(:xml_decl, s(:attributes, s(:attribute, 'encoding', 'UTF-8')))
|
||||||
|
@tag = @builder.process(node)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return an XmlDeclaration node' do
|
||||||
|
@tag.is_a?(Oga::XML::XmlDeclaration).should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'include the encoding of the tag' do
|
||||||
|
@tag.encoding.should == 'UTF-8'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context '#on_element' do
|
context '#on_element' do
|
||||||
context 'simple elements' do
|
context 'simple elements' do
|
||||||
before do
|
before do
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Oga::XML::XmlDeclaration do
|
||||||
|
context 'setting attributes' do
|
||||||
|
example 'set the version via the constructor' do
|
||||||
|
described_class.new(:version => '1.0').version.should == '1.0'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the version via a setter' do
|
||||||
|
instance = described_class.new
|
||||||
|
instance.version = '1.0'
|
||||||
|
|
||||||
|
instance.version.should == '1.0'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'default attribute values' do
|
||||||
|
before do
|
||||||
|
@instance = described_class.new
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the default version' do
|
||||||
|
@instance.version.should == '1.0'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the default encoding' do
|
||||||
|
@instance.encoding.should == 'UTF-8'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context '#to_xml' do
|
||||||
|
before do
|
||||||
|
@instance = described_class.new(
|
||||||
|
:version => '1.0',
|
||||||
|
:encoding => 'UTF-8',
|
||||||
|
:standalone => 'yes'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'generate the corresponding XML' do
|
||||||
|
@instance.to_xml
|
||||||
|
.should == '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue