Added XML::Element#inner_text=

This method can be used to more easily set the text of an element, without
having to manually muck around with XML::Text instances.
This commit is contained in:
Yorick Peterse 2014-09-10 23:25:39 +02:00
parent cfbdf1bdb1
commit 6cb2d54875
2 changed files with 31 additions and 0 deletions

View File

@ -145,6 +145,19 @@ module Oga
return nodes
end
##
# Sets the inner text of the current element to the given String.
#
# @param [String] text
#
def inner_text=(text)
children.each do |child|
child.remove if child.is_a?(Text)
end
children << XML::Text.new(:text => text)
end
##
# Converts the element and its child elements to XML.
#

View File

@ -151,6 +151,24 @@ describe Oga::XML::Element do
end
end
context '#inner_text=' do
before do
@element = described_class.new
end
example 'set the inner text of an element' do
@element.inner_text = 'foo'
@element.inner_text.should == 'foo'
end
example 'remove existing text nodes before inserting new nodes' do
@element.children << Oga::XML::Text.new(:text => 'foo')
@element.inner_text = 'bar'
@element.inner_text.should == 'bar'
end
end
context '#text_nodes' do
before do
@t1 = Oga::XML::Text.new(:text => 'Foo')