Take ownership when children nodes are assigned
This commit is contained in:
parent
36c11b2712
commit
0a9d6302c3
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue