From 746c8052ddee59c22d07e1cf7127b394daf5eaaf Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sun, 14 Dec 2014 23:32:43 +0100 Subject: [PATCH] Remove all nodes when calling Element#inner_text= This fixes #64. --- doc/changelog.md | 10 ++++++++++ lib/oga/xml/element.rb | 7 ++----- spec/oga/xml/element_spec.rb | 6 ++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/doc/changelog.md b/doc/changelog.md index c1d6cf3..c9d0c82 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -3,6 +3,16 @@ This document contains details of the various releases and their release dates. Dates are in the format `yyyy-mm-dd`. +## 0.3.0 - Unreleased + +### Element Inner Text + +When setting the inner text of an element using `Oga::XML::Element#inner_text=` +_all_ child nodes of the element are now removed first, instead of only text +nodes being removed. + +See for more information. + ## 0.2.0 - 2014-11-17 ### CSS Selector Support diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 29a78f3..209d809 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -212,11 +212,8 @@ module Oga # @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) + text_node = XML::Text.new(:text => text) + @children = NodeSet.new([text_node]) end ## diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 5d35cad..26664e4 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -260,11 +260,13 @@ describe Oga::XML::Element do @element.inner_text.should == 'foo' end - example 'remove existing text nodes before inserting new nodes' do + example 'remove all existing nodes before inserting a new text node' do @element.children << Oga::XML::Text.new(:text => 'foo') + @element.children << Oga::XML::Element.new(:name => 'x') @element.inner_text = 'bar' - @element.inner_text.should == 'bar' + + @element.children.length.should == 1 end end