From ff8a4ad3aa8845899f34c88c2f1fb422bd45fac1 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 22 Aug 2014 11:05:39 +0200 Subject: [PATCH] Ignore comment nodes in NodeSet#text. --- lib/oga/xml/node_set.rb | 4 ++-- spec/oga/xml/node_set_spec.rb | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/oga/xml/node_set.rb b/lib/oga/xml/node_set.rb index 04e1583..866e87a 100644 --- a/lib/oga/xml/node_set.rb +++ b/lib/oga/xml/node_set.rb @@ -240,7 +240,7 @@ module Oga alias_method :attr, :attribute ## - # Returns the text of all nodes in the set. + # Returns the text of all nodes in the set, ignoring comment nodes. # # @return [String] # @@ -248,7 +248,7 @@ module Oga text = '' @nodes.each do |node| - if node.respond_to?(:text) + if node.respond_to?(:text) and !node.is_a?(Comment) text << node.text end end diff --git a/spec/oga/xml/node_set_spec.rb b/spec/oga/xml/node_set_spec.rb index 2a2e704..e0a15b6 100644 --- a/spec/oga/xml/node_set_spec.rb +++ b/spec/oga/xml/node_set_spec.rb @@ -310,19 +310,21 @@ describe Oga::XML::NodeSet do context '#text' do before do - child = Oga::XML::Text.new(:text => 'foo') + child = Oga::XML::Text.new(:text => 'foo') + comment = Oga::XML::Comment.new(:text => 'bar') + cdata = Oga::XML::Cdata.new(:text => 'baz') + text = Oga::XML::Text.new(:text => "\nbar") @el = Oga::XML::Element.new( :name => 'a', - :children => described_class.new([child]) + :children => described_class.new([child, comment, cdata]) ) - @txt = Oga::XML::Text.new(:text => "\nbar") - @set = described_class.new([@el, @txt]) + @set = described_class.new([@el, text]) end example 'return the text of all nodes' do - @set.text.should == "foo\nbar" + @set.text.should == "foobaz\nbar" end end end