Ignore comment nodes in NodeSet#text.

This commit is contained in:
Yorick Peterse 2014-08-22 11:05:39 +02:00
parent 99be3182ae
commit ff8a4ad3aa
2 changed files with 9 additions and 7 deletions

View File

@ -240,7 +240,7 @@ module Oga
alias_method :attr, :attribute 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] # @return [String]
# #
@ -248,7 +248,7 @@ module Oga
text = '' text = ''
@nodes.each do |node| @nodes.each do |node|
if node.respond_to?(:text) if node.respond_to?(:text) and !node.is_a?(Comment)
text << node.text text << node.text
end end
end end

View File

@ -310,19 +310,21 @@ describe Oga::XML::NodeSet do
context '#text' do context '#text' do
before 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( @el = Oga::XML::Element.new(
:name => 'a', :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, text])
@set = described_class.new([@el, @txt])
end end
example 'return the text of all nodes' do example 'return the text of all nodes' do
@set.text.should == "foo\nbar" @set.text.should == "foobaz\nbar"
end end
end end
end end