From 36c11b2712bf3afae50a953fec0ab1992b319e42 Mon Sep 17 00:00:00 2001 From: lulalala Date: Wed, 13 Jul 2022 21:24:00 +0800 Subject: [PATCH] Update owner when assign children --- lib/oga/xml/document.rb | 1 + lib/oga/xml/node.rb | 1 + spec/oga/xml/document_spec.rb | 2 ++ 3 files changed, 4 insertions(+) diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 78b81d8..1b62664 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -46,6 +46,7 @@ module Oga # @param [Oga::XML::NodeSet|Array] nodes def children=(nodes) if nodes.is_a?(NodeSet) + nodes.owner = self @children = nodes else @children = NodeSet.new(nodes, self) diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index 047a929..8330c2a 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -49,6 +49,7 @@ module Oga # @param [Oga::XML::NodeSet|Array] nodes def children=(nodes) if nodes.is_a?(NodeSet) + nodes.owner = self @children = nodes else @children = NodeSet.new(nodes, self) diff --git a/spec/oga/xml/document_spec.rb b/spec/oga/xml/document_spec.rb index 409d238..62f24a1 100644 --- a/spec/oga/xml/document_spec.rb +++ b/spec/oga/xml/document_spec.rb @@ -22,6 +22,7 @@ describe Oga::XML::Document do document.children = [child] expect(document.children[0]).to eq(child) + expect(document.children[0].parent).to eq(document) end it 'sets the child nodes using a NodeSet' do @@ -31,6 +32,7 @@ describe Oga::XML::Document do document.children = Oga::XML::NodeSet.new([child]) expect(document.children[0]).to eq(child) + expect(document.children[0].parent).to eq(document) end end