diff --git a/lib/oga.rb b/lib/oga.rb index f7e8fa4..9a5b786 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -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' diff --git a/lib/oga/xml/cdata.rb b/lib/oga/xml/cdata.rb index e3bdb79..055bce2 100644 --- a/lib/oga/xml/cdata.rb +++ b/lib/oga/xml/cdata.rb @@ -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. # diff --git a/lib/oga/xml/character_node.rb b/lib/oga/xml/character_node.rb new file mode 100644 index 0000000..2c80aab --- /dev/null +++ b/lib/oga/xml/character_node.rb @@ -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 diff --git a/lib/oga/xml/comment.rb b/lib/oga/xml/comment.rb index e6eb478..472c7ff 100644 --- a/lib/oga/xml/comment.rb +++ b/lib/oga/xml/comment.rb @@ -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. # diff --git a/lib/oga/xml/text.rb b/lib/oga/xml/text.rb index 94a3607..5560acd 100644 --- a/lib/oga/xml/text.rb +++ b/lib/oga/xml/text.rb @@ -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] # diff --git a/spec/oga/xml/character_node_spec.rb b/spec/oga/xml/character_node_spec.rb new file mode 100644 index 0000000..05ffe7b --- /dev/null +++ b/spec/oga/xml/character_node_spec.rb @@ -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