Dedicated handling of XML declaration nodes.

This commit is contained in:
Yorick Peterse committed 2014-04-02 22:30:45 +02:00
1 parent d6c0a1f3f3
commit bdb76cefc5
7 files changed
+158 -3

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ require_relative 'oga/xml/document'
require_relative 'oga/xml/text'
require_relative 'oga/xml/comment'
require_relative 'oga/xml/cdata'
require_relative 'oga/xml/xml_declaration'
require_relative 'oga/xml/tree_builder'
require_relative 'oga/html/parser'
+8 -2
View File
@@ -4,10 +4,16 @@ module Oga
# Class description
#
class Document < Node
attr_accessor :dtd, :xml_version, :encoding
attr_accessor :xml_declaration
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 # Document
end # XML
+22 -1
View File
@@ -18,7 +18,14 @@ module Oga
#
def on_document(node)
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|
child.parent = document
@@ -27,6 +34,16 @@ module Oga
return document
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
# @return [Oga::XML::Comment]
@@ -103,6 +120,10 @@ module Oga
return *node
end
def handler_missing(node)
raise "No handler for node type #{node.type.inspect}"
end
private
##
+38
View File
@@ -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