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:
parent
cfbdf1bdb1
commit
6cb2d54875
|
@ -145,6 +145,19 @@ module Oga
|
||||||
return nodes
|
return nodes
|
||||||
end
|
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.
|
# Converts the element and its child elements to XML.
|
||||||
#
|
#
|
||||||
|
|
|
@ -151,6 +151,24 @@ describe Oga::XML::Element do
|
||||||
end
|
end
|
||||||
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
|
context '#text_nodes' do
|
||||||
before do
|
before do
|
||||||
@t1 = Oga::XML::Text.new(:text => 'Foo')
|
@t1 = Oga::XML::Text.new(:text => 'Foo')
|
||||||
|
|
Loading…
Reference in New Issue