File: Node_replaceChild_edge_cases.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (146 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--TEST--
Dom\Node::replaceChild() edge cases
--EXTENSIONS--
dom
--FILE--
<?php

$dom = Dom\HTMLDocument::createFromString("<!DOCTYPE html><html></html>");
$dom->documentElement->remove();
$parent = $dom->createElement("parent");
$child = $dom->createElement("child");
$parent->appendChild($child);
$dom->appendChild($parent);

echo "--- Wrong parent node type ---\n";

$comment = $dom->createComment('This is a comment');
try {
    $comment->replaceChild($comment, $dom->createElement("old-child"));
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Node is an inclusive ancestor of parent ---\n";

try {
    $parent->replaceChild($parent, $child);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

try {
    $parent->replaceChild($dom, $child);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Child's parent is not parent ---\n";

try {
    $parent->replaceChild($dom->createElement("new-child"), $dom->createElement("old-child"));
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Invalid child to replace with ---\n";

try {
    $entityReference = $dom->importNode(Dom\XMLDocument::createEmpty()->createEntityReference("foo"));
    $parent->replaceChild($entityReference, $child);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Replace element with text in document root ---\n";

try {
    $dom->replaceChild($dom->createTextNode("text"), $parent);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Replace child element with doctype inside element ---\n";

try {
    $parent->replaceChild($dom->doctype, $child);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Replace element with fragment containing multiple elements ---\n";

$fragment = $dom->createDocumentFragment();
$fragment->appendChild($dom->createElement("new-child1"));
$fragment->appendChild($dom->createElement("new-child2"));

try {
    $dom->replaceChild($fragment, $parent);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Replace comment in document causing more than two elements ---\n";

$comment = $dom->appendChild($dom->createComment("comment"));
try {
    $dom->replaceChild($dom->createElement("new-child"), $comment);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Replace dtd with element ---\n";

try {
    $dom->replaceChild($dom->createElement("new-child"), $dom->doctype);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Replace element with another dtd ---\n";

try {
    $dom->replaceChild($dom->doctype, $parent);
} catch (DOMException $e) {
    echo $e->getMessage(), "\n";
}

echo "--- Replace parent with itself ---\n";

$dom->replaceChild($parent, $parent);
echo $dom->saveHtml(), "\n";

echo "--- Replace parent with single-child fragment ---\n";

$fragment = $dom->createDocumentFragment();
$fragment->appendChild($dom->createElement("new-child"));
$dom->replaceChild($fragment, $parent);
echo $dom->saveHtml(), "\n";

?>
--EXPECT--
--- Wrong parent node type ---
Hierarchy Request Error
--- Node is an inclusive ancestor of parent ---
Hierarchy Request Error
Hierarchy Request Error
--- Child's parent is not parent ---
Not Found Error
--- Invalid child to replace with ---
Hierarchy Request Error
--- Replace element with text in document root ---
Cannot insert text as a child of a document
--- Replace child element with doctype inside element ---
Cannot insert a document type into anything other than a document
--- Replace element with fragment containing multiple elements ---
Cannot have more than one element child in a document
--- Replace comment in document causing more than two elements ---
Cannot have more than one element child in a document
--- Replace dtd with element ---
Cannot have more than one element child in a document
--- Replace element with another dtd ---
Document types must be the first child in a document
--- Replace parent with itself ---
<!DOCTYPE html><parent><child></child></parent><!--comment-->
--- Replace parent with single-child fragment ---
<!DOCTYPE html><new-child></new-child><!--comment-->