xref: /PHP-8.3/ext/dom/tests/bug28721.phpt (revision bd9f4fa6)
1--TEST--
2Bug #28721 (appendChild() and insertBefore() unset DOMText)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7function print_node(DomNode $node) {
8  echo "name (value): " . $node->nodeName . " (" . $node->nodeValue . ")\n";
9}
10
11function print_node_r(DomNode $node) {
12  static $indent = "";
13  echo "\n" . $indent;
14  print_node($node);
15
16  echo $indent . "parent: ";
17  if ( $node->parentNode )
18    print_node($node->parentNode);
19  else
20    echo "NULL\n";
21
22  echo $indent . "previousSibling: ";
23  if ( $node->previousSibling )
24    print_node($node->previousSibling);
25  else
26    echo "NULL\n";
27
28  echo $indent . "nextSibling: ";
29  if ( $node->nextSibling )
30    print_node($node->nextSibling);
31  else
32    echo "NULL\n";
33
34  if ( !$node->hasChildNodes() )
35    return;
36
37  foreach ($node->childNodes as $child) {
38
39    $old_indent  = $indent;
40    $indent .= "  ";
41    print_node_r($child);
42    $indent = $old_indent;
43  }
44}
45
46function err_handler($errno, $errstr, $errfile, $errline) {
47  echo "Error ($errno) on line $errline: $errstr\n";
48}
49
50// Record 'DocumentFragment is empty' warnings
51set_error_handler("err_handler", E_WARNING);
52
53$xml = new DomDocument();
54
55$p = $xml->createElement("p");
56
57$p->appendChild($t1 = $xml->createTextNode(" t1 "));
58$p->appendChild($b = $xml->createElement("b"));
59$b->appendChild($xml->createTextNode("X"));
60$p->appendChild($t2 = $xml->createTextNode(" t2 "));
61$p->appendChild($xml->createTextNode(" xxx "));
62
63print_node_r($p);
64
65echo "\nAppend t1 to p:\n";
66$ret = $p->appendChild($t1);
67
68print_node_r($p);
69echo "\n";
70
71echo "t1 == ret: ";
72var_dump( $t1 === $ret );
73
74
75$d = $xml->createElement("div");
76$d->appendChild($t3 = $xml->createTextNode(" t3 "));
77$d->appendChild($b = $xml->createElement("b"));
78$b->appendChild($xml->createElement("X"));
79$d->appendChild($t4 = $xml->createTextNode(" t4 "));
80$d->appendChild($xml->createTextNode(" xxx "));
81
82echo "\ndiv:\n";
83print_node_r($d);
84
85echo "\nInsert t4 before t3:\n";
86
87$ret = $d->insertBefore($t4, $t3);
88
89print_node_r($d);
90echo "\n";
91
92$frag = $xml->createDocumentFragment();
93
94$t5 = $frag->appendChild($xml->createTextNode(" t5 "));
95$frag->appendChild($i = $xml->createElement("i"));
96$i->appendChild($xml->createTextNode(" frob "));
97$frag->appendChild($xml->createTextNOde(" t6 "));
98
99echo "\np:\n";
100print_node_r($p);
101echo "\nFragment:\n";
102print_node_r($frag);
103
104echo "\nAppending fragment to p:\n";
105$p->appendChild($frag);
106
107print_node_r($p);
108echo "\nFragment:\n";
109print_node_r($frag);
110
111echo "\ndiv:\n";
112print_node_r($d);
113echo "\nInserting fragment before t4\n";
114$d->insertBefore($frag, $t4);
115print_node_r($d);
116
117echo "\np:\n";
118print_node_r($p);
119
120?>
121--EXPECT--
122name (value): p ( t1 X t2  xxx )
123parent: NULL
124previousSibling: NULL
125nextSibling: NULL
126
127  name (value): #text ( t1 )
128  parent: name (value): p ( t1 X t2  xxx )
129  previousSibling: NULL
130  nextSibling: name (value): b (X)
131
132  name (value): b (X)
133  parent: name (value): p ( t1 X t2  xxx )
134  previousSibling: name (value): #text ( t1 )
135  nextSibling: name (value): #text ( t2 )
136
137    name (value): #text (X)
138    parent: name (value): b (X)
139    previousSibling: NULL
140    nextSibling: NULL
141
142  name (value): #text ( t2 )
143  parent: name (value): p ( t1 X t2  xxx )
144  previousSibling: name (value): b (X)
145  nextSibling: name (value): #text ( xxx )
146
147  name (value): #text ( xxx )
148  parent: name (value): p ( t1 X t2  xxx )
149  previousSibling: name (value): #text ( t2 )
150  nextSibling: NULL
151
152Append t1 to p:
153
154name (value): p (X t2  xxx  t1 )
155parent: NULL
156previousSibling: NULL
157nextSibling: NULL
158
159  name (value): b (X)
160  parent: name (value): p (X t2  xxx  t1 )
161  previousSibling: NULL
162  nextSibling: name (value): #text ( t2 )
163
164    name (value): #text (X)
165    parent: name (value): b (X)
166    previousSibling: NULL
167    nextSibling: NULL
168
169  name (value): #text ( t2 )
170  parent: name (value): p (X t2  xxx  t1 )
171  previousSibling: name (value): b (X)
172  nextSibling: name (value): #text ( xxx )
173
174  name (value): #text ( xxx )
175  parent: name (value): p (X t2  xxx  t1 )
176  previousSibling: name (value): #text ( t2 )
177  nextSibling: name (value): #text ( t1 )
178
179  name (value): #text ( t1 )
180  parent: name (value): p (X t2  xxx  t1 )
181  previousSibling: name (value): #text ( xxx )
182  nextSibling: NULL
183
184t1 == ret: bool(true)
185
186div:
187
188name (value): div ( t3  t4  xxx )
189parent: NULL
190previousSibling: NULL
191nextSibling: NULL
192
193  name (value): #text ( t3 )
194  parent: name (value): div ( t3  t4  xxx )
195  previousSibling: NULL
196  nextSibling: name (value): b ()
197
198  name (value): b ()
199  parent: name (value): div ( t3  t4  xxx )
200  previousSibling: name (value): #text ( t3 )
201  nextSibling: name (value): #text ( t4 )
202
203    name (value): X ()
204    parent: name (value): b ()
205    previousSibling: NULL
206    nextSibling: NULL
207
208  name (value): #text ( t4 )
209  parent: name (value): div ( t3  t4  xxx )
210  previousSibling: name (value): b ()
211  nextSibling: name (value): #text ( xxx )
212
213  name (value): #text ( xxx )
214  parent: name (value): div ( t3  t4  xxx )
215  previousSibling: name (value): #text ( t4 )
216  nextSibling: NULL
217
218Insert t4 before t3:
219
220name (value): div ( t4  t3  xxx )
221parent: NULL
222previousSibling: NULL
223nextSibling: NULL
224
225  name (value): #text ( t4 )
226  parent: name (value): div ( t4  t3  xxx )
227  previousSibling: NULL
228  nextSibling: name (value): #text ( t3 )
229
230  name (value): #text ( t3 )
231  parent: name (value): div ( t4  t3  xxx )
232  previousSibling: name (value): #text ( t4 )
233  nextSibling: name (value): b ()
234
235  name (value): b ()
236  parent: name (value): div ( t4  t3  xxx )
237  previousSibling: name (value): #text ( t3 )
238  nextSibling: name (value): #text ( xxx )
239
240    name (value): X ()
241    parent: name (value): b ()
242    previousSibling: NULL
243    nextSibling: NULL
244
245  name (value): #text ( xxx )
246  parent: name (value): div ( t4  t3  xxx )
247  previousSibling: name (value): b ()
248  nextSibling: NULL
249
250
251p:
252
253name (value): p (X t2  xxx  t1 )
254parent: NULL
255previousSibling: NULL
256nextSibling: NULL
257
258  name (value): b (X)
259  parent: name (value): p (X t2  xxx  t1 )
260  previousSibling: NULL
261  nextSibling: name (value): #text ( t2 )
262
263    name (value): #text (X)
264    parent: name (value): b (X)
265    previousSibling: NULL
266    nextSibling: NULL
267
268  name (value): #text ( t2 )
269  parent: name (value): p (X t2  xxx  t1 )
270  previousSibling: name (value): b (X)
271  nextSibling: name (value): #text ( xxx )
272
273  name (value): #text ( xxx )
274  parent: name (value): p (X t2  xxx  t1 )
275  previousSibling: name (value): #text ( t2 )
276  nextSibling: name (value): #text ( t1 )
277
278  name (value): #text ( t1 )
279  parent: name (value): p (X t2  xxx  t1 )
280  previousSibling: name (value): #text ( xxx )
281  nextSibling: NULL
282
283Fragment:
284
285name (value): #document-fragment ()
286parent: NULL
287previousSibling: NULL
288nextSibling: NULL
289
290  name (value): #text ( t5 )
291  parent: name (value): #document-fragment ()
292  previousSibling: NULL
293  nextSibling: name (value): i ( frob )
294
295  name (value): i ( frob )
296  parent: name (value): #document-fragment ()
297  previousSibling: name (value): #text ( t5 )
298  nextSibling: name (value): #text ( t6 )
299
300    name (value): #text ( frob )
301    parent: name (value): i ( frob )
302    previousSibling: NULL
303    nextSibling: NULL
304
305  name (value): #text ( t6 )
306  parent: name (value): #document-fragment ()
307  previousSibling: name (value): i ( frob )
308  nextSibling: NULL
309
310Appending fragment to p:
311
312name (value): p (X t2  xxx  t1  t5  frob  t6 )
313parent: NULL
314previousSibling: NULL
315nextSibling: NULL
316
317  name (value): b (X)
318  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
319  previousSibling: NULL
320  nextSibling: name (value): #text ( t2 )
321
322    name (value): #text (X)
323    parent: name (value): b (X)
324    previousSibling: NULL
325    nextSibling: NULL
326
327  name (value): #text ( t2 )
328  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
329  previousSibling: name (value): b (X)
330  nextSibling: name (value): #text ( xxx )
331
332  name (value): #text ( xxx )
333  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
334  previousSibling: name (value): #text ( t2 )
335  nextSibling: name (value): #text ( t1 )
336
337  name (value): #text ( t1 )
338  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
339  previousSibling: name (value): #text ( xxx )
340  nextSibling: name (value): #text ( t5 )
341
342  name (value): #text ( t5 )
343  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
344  previousSibling: name (value): #text ( t1 )
345  nextSibling: name (value): i ( frob )
346
347  name (value): i ( frob )
348  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
349  previousSibling: name (value): #text ( t5 )
350  nextSibling: name (value): #text ( t6 )
351
352    name (value): #text ( frob )
353    parent: name (value): i ( frob )
354    previousSibling: NULL
355    nextSibling: NULL
356
357  name (value): #text ( t6 )
358  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
359  previousSibling: name (value): i ( frob )
360  nextSibling: NULL
361
362Fragment:
363
364name (value): #document-fragment ()
365parent: NULL
366previousSibling: NULL
367nextSibling: NULL
368
369div:
370
371name (value): div ( t4  t3  xxx )
372parent: NULL
373previousSibling: NULL
374nextSibling: NULL
375
376  name (value): #text ( t4 )
377  parent: name (value): div ( t4  t3  xxx )
378  previousSibling: NULL
379  nextSibling: name (value): #text ( t3 )
380
381  name (value): #text ( t3 )
382  parent: name (value): div ( t4  t3  xxx )
383  previousSibling: name (value): #text ( t4 )
384  nextSibling: name (value): b ()
385
386  name (value): b ()
387  parent: name (value): div ( t4  t3  xxx )
388  previousSibling: name (value): #text ( t3 )
389  nextSibling: name (value): #text ( xxx )
390
391    name (value): X ()
392    parent: name (value): b ()
393    previousSibling: NULL
394    nextSibling: NULL
395
396  name (value): #text ( xxx )
397  parent: name (value): div ( t4  t3  xxx )
398  previousSibling: name (value): b ()
399  nextSibling: NULL
400
401Inserting fragment before t4
402Error (2) on line 109: DOMNode::insertBefore(): Document Fragment is empty
403
404name (value): div ( t4  t3  xxx )
405parent: NULL
406previousSibling: NULL
407nextSibling: NULL
408
409  name (value): #text ( t4 )
410  parent: name (value): div ( t4  t3  xxx )
411  previousSibling: NULL
412  nextSibling: name (value): #text ( t3 )
413
414  name (value): #text ( t3 )
415  parent: name (value): div ( t4  t3  xxx )
416  previousSibling: name (value): #text ( t4 )
417  nextSibling: name (value): b ()
418
419  name (value): b ()
420  parent: name (value): div ( t4  t3  xxx )
421  previousSibling: name (value): #text ( t3 )
422  nextSibling: name (value): #text ( xxx )
423
424    name (value): X ()
425    parent: name (value): b ()
426    previousSibling: NULL
427    nextSibling: NULL
428
429  name (value): #text ( xxx )
430  parent: name (value): div ( t4  t3  xxx )
431  previousSibling: name (value): b ()
432  nextSibling: NULL
433
434p:
435
436name (value): p (X t2  xxx  t1  t5  frob  t6 )
437parent: NULL
438previousSibling: NULL
439nextSibling: NULL
440
441  name (value): b (X)
442  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
443  previousSibling: NULL
444  nextSibling: name (value): #text ( t2 )
445
446    name (value): #text (X)
447    parent: name (value): b (X)
448    previousSibling: NULL
449    nextSibling: NULL
450
451  name (value): #text ( t2 )
452  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
453  previousSibling: name (value): b (X)
454  nextSibling: name (value): #text ( xxx )
455
456  name (value): #text ( xxx )
457  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
458  previousSibling: name (value): #text ( t2 )
459  nextSibling: name (value): #text ( t1 )
460
461  name (value): #text ( t1 )
462  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
463  previousSibling: name (value): #text ( xxx )
464  nextSibling: name (value): #text ( t5 )
465
466  name (value): #text ( t5 )
467  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
468  previousSibling: name (value): #text ( t1 )
469  nextSibling: name (value): i ( frob )
470
471  name (value): i ( frob )
472  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
473  previousSibling: name (value): #text ( t5 )
474  nextSibling: name (value): #text ( t6 )
475
476    name (value): #text ( frob )
477    parent: name (value): i ( frob )
478    previousSibling: NULL
479    nextSibling: NULL
480
481  name (value): #text ( t6 )
482  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
483  previousSibling: name (value): i ( frob )
484  nextSibling: NULL
485