Use a new base class for XML text nodes.
The classes Text, Cdata and Comment now extend CharacterData instead of Text.
This commit is contained in:
parent
24bc84e15e
commit
14aa420091
|
@ -18,6 +18,7 @@ end
|
|||
|
||||
require_relative 'oga/xml/node'
|
||||
require_relative 'oga/xml/document'
|
||||
require_relative 'oga/xml/character_node'
|
||||
require_relative 'oga/xml/text'
|
||||
require_relative 'oga/xml/comment'
|
||||
require_relative 'oga/xml/cdata'
|
||||
|
|
|
@ -3,7 +3,7 @@ module Oga
|
|||
##
|
||||
# Class used for storing information about CDATA tags.
|
||||
#
|
||||
class Cdata < Text
|
||||
class Cdata < CharacterNode
|
||||
##
|
||||
# Converts the node back to XML.
|
||||
#
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
module Oga
|
||||
module XML
|
||||
##
|
||||
# Base class for nodes that represent a text-like value such as Text and
|
||||
# Comment nodes.
|
||||
#
|
||||
# @!attribute [rw] text
|
||||
# @return [String]
|
||||
#
|
||||
class CharacterNode < Node
|
||||
attr_accessor :text
|
||||
|
||||
##
|
||||
# @param [Hash] options
|
||||
#
|
||||
# @option options [String] :text The text of the node.
|
||||
#
|
||||
def initialize(options = {})
|
||||
super
|
||||
|
||||
@text = options[:text]
|
||||
end
|
||||
|
||||
##
|
||||
# @return [String]
|
||||
#
|
||||
def to_xml
|
||||
return text.to_s
|
||||
end
|
||||
|
||||
##
|
||||
# @return [String]
|
||||
#
|
||||
def inspect
|
||||
return "#{self.class.to_s.split('::').last}(#{text.inspect})"
|
||||
end
|
||||
end # CharacterNode
|
||||
end # XML
|
||||
end # Oga
|
|
@ -3,7 +3,7 @@ module Oga
|
|||
##
|
||||
# Class used for storing information about XML comments.
|
||||
#
|
||||
class Comment < Text
|
||||
class Comment < CharacterNode
|
||||
##
|
||||
# Converts the node back to XML.
|
||||
#
|
||||
|
|
|
@ -4,37 +4,7 @@ module Oga
|
|||
# Class containing information about a single text node. Text nodes don't
|
||||
# have any children, attributes and the likes; just text.
|
||||
#
|
||||
# @!attribute [rw] text
|
||||
# @return [String]
|
||||
#
|
||||
class Text < Node
|
||||
attr_accessor :text
|
||||
|
||||
##
|
||||
# @param [Hash] options
|
||||
#
|
||||
# @option options [String] :text The text of the node.
|
||||
#
|
||||
def initialize(options = {})
|
||||
super
|
||||
|
||||
@text = options[:text]
|
||||
end
|
||||
|
||||
##
|
||||
# @return [String]
|
||||
#
|
||||
def to_xml
|
||||
return text.to_s
|
||||
end
|
||||
|
||||
##
|
||||
# @return [String]
|
||||
#
|
||||
def inspect
|
||||
return "#{self.class.to_s.split('::').last}(#{text.inspect})"
|
||||
end
|
||||
|
||||
class Text < CharacterNode
|
||||
##
|
||||
# @return [Symbol]
|
||||
#
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XML::CharacterNode do
|
||||
context '#initialize' do
|
||||
example 'set the text in the constructor' do
|
||||
described_class.new(:text => 'a').text.should == 'a'
|
||||
end
|
||||
|
||||
example 'set the text via an attribute' do
|
||||
node = described_class.new
|
||||
node.text = 'a'
|
||||
|
||||
node.text.should == 'a'
|
||||
end
|
||||
end
|
||||
|
||||
context '#to_xml' do
|
||||
example 'convert the node to XML' do
|
||||
described_class.new(:text => 'a').to_xml.should == 'a'
|
||||
end
|
||||
end
|
||||
|
||||
context '#inspect' do
|
||||
example 'return the inspect value' do
|
||||
described_class.new(:text => 'a').inspect.should == 'CharacterNode("a")'
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue