1--TEST--
2Dom\Node::replaceChild() edge cases
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$dom = Dom\HTMLDocument::createFromString("<!DOCTYPE html><html></html>");
9$dom->documentElement->remove();
10$parent = $dom->createElement("parent");
11$child = $dom->createElement("child");
12$parent->appendChild($child);
13$dom->appendChild($parent);
14
15echo "--- Wrong parent node type ---\n";
16
17$comment = $dom->createComment('This is a comment');
18try {
19    $comment->replaceChild($comment, $dom->createElement("old-child"));
20} catch (DOMException $e) {
21    echo $e->getMessage(), "\n";
22}
23
24echo "--- Node is an inclusive ancestor of parent ---\n";
25
26try {
27    $parent->replaceChild($parent, $child);
28} catch (DOMException $e) {
29    echo $e->getMessage(), "\n";
30}
31
32try {
33    $parent->replaceChild($dom, $child);
34} catch (DOMException $e) {
35    echo $e->getMessage(), "\n";
36}
37
38echo "--- Child's parent is not parent ---\n";
39
40try {
41    $parent->replaceChild($dom->createElement("new-child"), $dom->createElement("old-child"));
42} catch (DOMException $e) {
43    echo $e->getMessage(), "\n";
44}
45
46echo "--- Invalid child to replace with ---\n";
47
48try {
49    $entityReference = $dom->importNode(Dom\XMLDocument::createEmpty()->createEntityReference("foo"));
50    $parent->replaceChild($entityReference, $child);
51} catch (DOMException $e) {
52    echo $e->getMessage(), "\n";
53}
54
55echo "--- Replace element with text in document root ---\n";
56
57try {
58    $dom->replaceChild($dom->createTextNode("text"), $parent);
59} catch (DOMException $e) {
60    echo $e->getMessage(), "\n";
61}
62
63echo "--- Replace child element with doctype inside element ---\n";
64
65try {
66    $parent->replaceChild($dom->doctype, $child);
67} catch (DOMException $e) {
68    echo $e->getMessage(), "\n";
69}
70
71echo "--- Replace element with fragment containing multiple elements ---\n";
72
73$fragment = $dom->createDocumentFragment();
74$fragment->appendChild($dom->createElement("new-child1"));
75$fragment->appendChild($dom->createElement("new-child2"));
76
77try {
78    $dom->replaceChild($fragment, $parent);
79} catch (DOMException $e) {
80    echo $e->getMessage(), "\n";
81}
82
83echo "--- Replace comment in document causing more than two elements ---\n";
84
85$comment = $dom->appendChild($dom->createComment("comment"));
86try {
87    $dom->replaceChild($dom->createElement("new-child"), $comment);
88} catch (DOMException $e) {
89    echo $e->getMessage(), "\n";
90}
91
92echo "--- Replace dtd with element ---\n";
93
94try {
95    $dom->replaceChild($dom->createElement("new-child"), $dom->doctype);
96} catch (DOMException $e) {
97    echo $e->getMessage(), "\n";
98}
99
100echo "--- Replace element with another dtd ---\n";
101
102try {
103    $dom->replaceChild($dom->doctype, $parent);
104} catch (DOMException $e) {
105    echo $e->getMessage(), "\n";
106}
107
108echo "--- Replace parent with itself ---\n";
109
110$dom->replaceChild($parent, $parent);
111echo $dom->saveHtml(), "\n";
112
113echo "--- Replace parent with single-child fragment ---\n";
114
115$fragment = $dom->createDocumentFragment();
116$fragment->appendChild($dom->createElement("new-child"));
117$dom->replaceChild($fragment, $parent);
118echo $dom->saveHtml(), "\n";
119
120?>
121--EXPECT--
122--- Wrong parent node type ---
123Hierarchy Request Error
124--- Node is an inclusive ancestor of parent ---
125Hierarchy Request Error
126Hierarchy Request Error
127--- Child's parent is not parent ---
128Not Found Error
129--- Invalid child to replace with ---
130Hierarchy Request Error
131--- Replace element with text in document root ---
132Cannot insert text as a child of a document
133--- Replace child element with doctype inside element ---
134Cannot insert a document type into anything other than a document
135--- Replace element with fragment containing multiple elements ---
136Cannot have more than one element child in a document
137--- Replace comment in document causing more than two elements ---
138Cannot have more than one element child in a document
139--- Replace dtd with element ---
140Cannot have more than one element child in a document
141--- Replace element with another dtd ---
142Document types must be the first child in a document
143--- Replace parent with itself ---
144<!DOCTYPE html><parent><child></child></parent><!--comment-->
145--- Replace parent with single-child fragment ---
146<!DOCTYPE html><new-child></new-child><!--comment-->
147