Take ownership when children nodes are assigned

This commit is contained in:
lulalala 2022-07-13 21:41:39 +08:00
parent 36c11b2712
commit 0a9d6302c3
4 changed files with 22 additions and 5 deletions

View File

@ -47,6 +47,7 @@ module Oga
def children=(nodes)
if nodes.is_a?(NodeSet)
nodes.owner = self
nodes.take_ownership_on_nodes
@children = nodes
else
@children = NodeSet.new(nodes, self)

View File

@ -50,6 +50,7 @@ module Oga
def children=(nodes)
if nodes.is_a?(NodeSet)
nodes.owner = self
nodes.take_ownership_on_nodes
@children = nodes
else
@children = NodeSet.new(nodes, self)

View File

@ -42,11 +42,7 @@ module Oga
@owner = owner
@existing = {}
@nodes.each_with_index do |node, index|
mark_existing(node)
take_ownership(node, index) if @owner
end
take_ownership_on_nodes
end
# Yields the supplied block for every node.
@ -289,6 +285,14 @@ module Oga
"NodeSet(#{values})"
end
def take_ownership_on_nodes
@nodes.each_with_index do |node, index|
mark_existing(node)
take_ownership(node, index) if @owner
end
end
private
# Takes ownership of the given node. This only occurs when the current

View File

@ -168,5 +168,16 @@ describe Oga::XML::Generator do
end
end
end
describe 'using an Element with replaced children' do
it 'returns a string' do
element = Oga::XML::Element.new(name: 'foo')
element.children = Oga::XML::Parser.new('<bar></bar>').parse.children
output = described_class.new(element).to_xml
expect(output).to eq('<foo><bar /></foo>')
end
end
end
end