1--TEST--
2Tests DOMDocument::adoptNode()
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$doc1 = new DOMDocument();
9$doc1->loadXML("<p><b>hi<i attrib=\"1\">x</i></b>world</p>");
10$doc2 = new DOMDocument();
11$doc2->loadXML("<div/>");
12
13$b_tag_element = $doc1->firstChild->firstChild;
14$i_tag_element = $b_tag_element->lastChild;
15
16echo "-- Owner document check before adopting --\n";
17var_dump($i_tag_element->ownerDocument === $doc1);
18var_dump($i_tag_element->ownerDocument === $doc2);
19
20echo "-- Trying to append child from other document --\n";
21try {
22    $doc2->firstChild->appendChild($b_tag_element); // Should fail because it's another document
23} catch (\DOMException $e) {
24    echo $e->getMessage(), "\n";
25}
26
27echo "-- Adopting --\n";
28$adopted = $doc2->adoptNode($b_tag_element);
29var_dump($adopted->textContent);
30var_dump($doc1->saveXML());
31var_dump($doc2->saveXML());
32
33echo "-- Appending the adopted node --\n";
34
35$doc2->firstChild->appendChild($adopted);
36var_dump($doc2->saveXML());
37var_dump($i_tag_element->ownerDocument === $doc1);
38var_dump($i_tag_element->ownerDocument === $doc2);
39
40echo "-- Adopt node to the original document --\n";
41
42$adopted = $doc1->adoptNode($doc1->firstChild->firstChild);
43var_dump($adopted->textContent);
44var_dump($doc1->saveXML());
45
46echo "-- Adopt a document (strict error on) --\n";
47
48try {
49    $doc1->adoptNode($doc1);
50} catch (\DOMException $e) {
51    echo $e->getMessage(), "\n";
52}
53
54echo "-- Adopt a document (strict error off) --\n";
55
56$doc1->strictErrorChecking = false;
57try {
58    $doc1->adoptNode($doc1);
59} catch (\DOMException $e) {
60    echo $e->getMessage(), "\n";
61}
62$doc1->strictErrorChecking = true;
63
64echo "-- Adopt an attribute --\n";
65
66$doc3 = new DOMDocument();
67$doc3->loadXML('<p align="center">hi</p>');
68$attribute = $doc3->firstChild->attributes->item(0);
69var_dump($attribute->parentNode !== NULL);
70$adopted = $doc3->adoptNode($attribute);
71var_dump($adopted->parentNode === NULL);
72echo $doc3->saveXML();
73
74echo "-- Append an attribute from another document --\n";
75
76$doc4 = new DOMDocument();
77$doc4->appendChild($doc4->createElement('container'));
78$doc4->documentElement->appendChild($doc4->adoptNode($adopted));
79echo $doc4->saveXML();
80
81echo "-- Adopt an entity reference --\n";
82
83$doc4 = new DOMDocument();
84$doc4->loadXML(<<<'XML'
85<?xml version='1.0' encoding='utf-8' ?>
86<!DOCTYPE set PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN" "http://www.docbook.org/xml/5.0/dtd/docbook.dtd" [
87<!ENTITY my_entity '<p>hi</p>'> ]>
88<p/>
89XML, LIBXML_NOENT);
90$p_tag_element = $doc4->firstChild->nextSibling;
91$entity_reference = $doc4->createEntityReference('my_entity');
92$p_tag_element->appendChild($entity_reference);
93var_dump($doc4->saveXML());
94$doc3->adoptNode($entity_reference);
95var_dump($doc4->saveXML());
96$doc3->firstChild->appendChild($entity_reference);
97var_dump($doc3->saveXML());
98
99echo "-- Adopt a node and destroy the new document --\n";
100
101$doc1 = new DOMDocument();
102$doc1->appendChild($doc1->createElement('child'));
103$doc2 = new DOMDocument();
104$doc2->appendChild($doc2->createElement('container'));
105$doc2->documentElement->appendChild($child = $doc2->adoptNode($doc1->documentElement));
106echo $doc1->saveXML();
107echo $doc2->saveXML();
108// Try to trigger a use-after-free
109unset($doc2);
110var_dump($child->nodeName);
111unset($doc1);
112var_dump($child->nodeName);
113
114?>
115--EXPECTF--
116-- Owner document check before adopting --
117bool(true)
118bool(false)
119-- Trying to append child from other document --
120Wrong Document Error
121-- Adopting --
122string(3) "hix"
123string(35) "<?xml version="1.0"?>
124<p>world</p>
125"
126string(29) "<?xml version="1.0"?>
127<div/>
128"
129-- Appending the adopted node --
130string(62) "<?xml version="1.0"?>
131<div><b>hi<i attrib="1">x</i></b></div>
132"
133bool(false)
134bool(true)
135-- Adopt node to the original document --
136string(5) "world"
137string(27) "<?xml version="1.0"?>
138<p/>
139"
140-- Adopt a document (strict error on) --
141Not Supported Error
142-- Adopt a document (strict error off) --
143
144Warning: DOMDocument::adoptNode(): Not Supported Error in %s on line %d
145-- Adopt an attribute --
146bool(true)
147bool(true)
148<?xml version="1.0"?>
149<p>hi</p>
150-- Append an attribute from another document --
151<?xml version="1.0"?>
152<container align="center"/>
153-- Adopt an entity reference --
154string(202) "<?xml version="1.0" encoding="utf-8"?>
155<!DOCTYPE set PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN" "http://www.docbook.org/xml/5.0/dtd/docbook.dtd" [
156<!ENTITY my_entity "<p>hi</p>">
157]>
158<p>&my_entity;</p>
159"
160string(188) "<?xml version="1.0" encoding="utf-8"?>
161<!DOCTYPE set PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN" "http://www.docbook.org/xml/5.0/dtd/docbook.dtd" [
162<!ENTITY my_entity "<p>hi</p>">
163]>
164<p/>
165"
166string(43) "<?xml version="1.0"?>
167<p>hi&my_entity;</p>
168"
169-- Adopt a node and destroy the new document --
170<?xml version="1.0"?>
171<?xml version="1.0"?>
172<container><child/></container>
173string(5) "child"
174string(5) "child"
175