Skip to content

Commit 2dff867

Browse files
authored
Merge pull request thesoftwarefanatics#21 from thesoftwarefanatics/fix-replace-child
Fix replaceChild functionality
2 parents fba7a7c + 27dd9bf commit 2dff867

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

src/PHPHtmlParser/Dom/InnerNode.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,25 @@ public function isChild($id)
235235
*/
236236
public function replaceChild($childId, AbstractNode $newChild)
237237
{
238-
$oldChild = $this->getChild($childId);
239-
$keys = array_keys($this->children);
240-
$index = array_search($childId, $keys, true);
241-
$keys[$index] = $newChild->id();
242-
$this->children = array_combine($keys, $this->children);
243-
$this->children[$newChild->id()] = $newChild;
244-
unset($oldChild);
238+
// Replace key of old child
239+
$keys = array_keys($this->children);
240+
$index = array_search($childId, $keys, true);
241+
$keys[$index] = $newChild->id();
242+
$this->children = array_combine($keys, $this->children);
243+
244+
// Replace old child node with new one
245+
$this->children[$newChild->id()]['node'] = $newChild;
246+
247+
$child = $this->children[$newChild->id()];
248+
249+
// Update previous and next nodes
250+
if ($child['prev'] !== null) {
251+
$this->children[$child['prev']]['next'] = $newChild->id();
252+
}
253+
254+
if ($child['next'] !== null) {
255+
$this->children[$child['next']]['prev'] = $newChild->id();
256+
}
245257
}
246258

247259
/**
@@ -314,4 +326,4 @@ public function setParent(InnerNode $parent)
314326

315327
return parent::setParent($parent);
316328
}
317-
}
329+
}

tests/Node/ParentTest.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,62 @@ public function testLastChild()
142142
public function testReplaceChild()
143143
{
144144
$parent = new Node;
145-
$child = new Node;
145+
$child1 = new Node;
146146
$child2 = new Node;
147147
$child3 = new Node;
148-
$parent->addChild($child);
148+
$child4 = new Node;
149+
$parent->addChild($child1);
149150
$parent->addChild($child2);
150-
$parent->replaceChild($child->id(), $child3);
151+
$parent->addChild($child3);
151152

152-
$this->assertFalse($parent->isChild($child->id()));
153+
self::assertAttributeEquals(
154+
[
155+
$child1->id() => [
156+
'next' => $child2->id(),
157+
'prev' => null,
158+
'node' => $child1,
159+
],
160+
$child2->id() => [
161+
'next' => $child3->id(),
162+
'prev' => $child1->id(),
163+
'node' => $child2,
164+
],
165+
$child3->id() => [
166+
'next' => null,
167+
'prev' => $child2->id(),
168+
'node' => $child3,
169+
],
170+
],
171+
'children',
172+
$parent
173+
);
174+
175+
$parent->replaceChild($child2->id(), $child4);
176+
177+
$this->assertFalse($parent->isChild($child2->id()));
178+
$this->assertTrue($parent->isChild($child4->id()));
179+
180+
self::assertAttributeEquals(
181+
[
182+
$child1->id() => [
183+
'next' => $child4->id(),
184+
'prev' => null,
185+
'node' => $child1,
186+
],
187+
$child3->id() => [
188+
'next' => null,
189+
'prev' => $child4->id(),
190+
'node' => $child3,
191+
],
192+
$child4->id() => [
193+
'next' => $child3->id(),
194+
'prev' => $child1->id(),
195+
'node' => $child4,
196+
],
197+
],
198+
'children',
199+
$parent
200+
);
153201
}
154202

155203
public function testSetParentDescendantException()

0 commit comments

Comments
 (0)