Skip to content

Commit

Permalink
unlinking children within Node#content= to prevent freeing memory out…
Browse files Browse the repository at this point in the history
… from under living Ruby objects. Closes #203.
  • Loading branch information
flavorjones committed Dec 30, 2009
1 parent db8d7c5 commit 982a43f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,17 @@ static VALUE node_type(VALUE self)
*/
static VALUE set_content(VALUE self, VALUE content)
{
xmlNodePtr node;
xmlNodePtr node, child, next ;
Data_Get_Struct(self, xmlNode, node);

child = node->children;
while (NULL != child) {
next = child->next ;
xmlUnlinkNode(child) ;
NOKOGIRI_ROOT_NODE(child) ;
child = next ;
}

xmlNodeSetContent(node, (xmlChar *)StringValuePtr(content));
return content;
}
Expand Down
7 changes: 7 additions & 0 deletions test/xml/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,13 @@ def test_content
assert_equal('hello world!', node.content)
end

def test_set_content_should_unlink_existing_content
node = @xml.at_css("employee")
children = node.children
node.content = "hello"
children.each { |child| assert_nil child.parent }
end

def test_whitespace_nodes
doc = Nokogiri::XML.parse("<root><b>Foo</b>\n<i>Bar</i> <p>Bazz</p></root>")
children = doc.at('//root').children.collect{|j| j.to_s}
Expand Down

0 comments on commit 982a43f

Please sign in to comment.