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:
parent
8f2ecf62c6
commit
94965961ce
|
@ -55,6 +55,21 @@ module Oga
|
||||||
return children.text
|
return children.text
|
||||||
end
|
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.
|
# Converts the element and its child elements to XML.
|
||||||
#
|
#
|
||||||
|
|
|
@ -46,6 +46,24 @@ describe Oga::XML::Element do
|
||||||
end
|
end
|
||||||
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
|
context '#to_xml' do
|
||||||
example 'generate the corresponding XML' do
|
example 'generate the corresponding XML' do
|
||||||
described_class.new(:name => 'p').to_xml.should == '<p></p>'
|
described_class.new(:name => 'p').to_xml.should == '<p></p>'
|
||||||
|
|
Loading…
Reference in New Issue