Added XML::Element#inner_text.

This method can be used to retrieve the text of the given node only. In other
words, unlike Element#text it does not contain the text of any child nodes.
This commit is contained in:
Yorick Peterse 2014-07-04 09:50:05 +02:00
parent 8f2ecf62c6
commit 94965961ce
2 changed files with 33 additions and 0 deletions

View File

@ -55,6 +55,21 @@ module Oga
return children.text
end
##
# Returns the text of the current element only.
#
# @return [String]
#
def inner_text
text = ''
children.each do |child|
text << child.text if child.is_a?(Text)
end
return text
end
##
# Converts the element and its child elements to XML.
#

View File

@ -46,6 +46,24 @@ describe Oga::XML::Element do
end
end
context '#inner_text' do
before do
t1 = Oga::XML::Text.new(:text => 'Foo')
t2 = Oga::XML::Text.new(:text => 'Bar')
@n1 = described_class.new(:children => [t1])
@n2 = described_class.new(:children => [@n1, t2])
end
example 'return the inner text of the parent node' do
@n2.inner_text.should == 'Bar'
end
example 'return the inner text of the child node' do
@n1.inner_text.should == 'Foo'
end
end
context '#to_xml' do
example 'generate the corresponding XML' do
described_class.new(:name => 'p').to_xml.should == '<p></p>'