xref: /PHP-5.4/ext/dom/tests/bug28721.phpt (revision d3571ea2)
1--TEST--
2Bug #28721 (appendChild() and insertBefore() unset DOMText)
3--SKIPIF--
4<?php require_once('skipif.inc'); ?>
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--
122
123name (value): p ( t1 X t2  xxx )
124parent: NULL
125previousSibling: NULL
126nextSibling: NULL
127
128  name (value): #text ( t1 )
129  parent: name (value): p ( t1 X t2  xxx )
130  previousSibling: NULL
131  nextSibling: name (value): b (X)
132
133  name (value): b (X)
134  parent: name (value): p ( t1 X t2  xxx )
135  previousSibling: name (value): #text ( t1 )
136  nextSibling: name (value): #text ( t2 )
137
138    name (value): #text (X)
139    parent: name (value): b (X)
140    previousSibling: NULL
141    nextSibling: NULL
142
143  name (value): #text ( t2 )
144  parent: name (value): p ( t1 X t2  xxx )
145  previousSibling: name (value): b (X)
146  nextSibling: name (value): #text ( xxx )
147
148  name (value): #text ( xxx )
149  parent: name (value): p ( t1 X t2  xxx )
150  previousSibling: name (value): #text ( t2 )
151  nextSibling: NULL
152
153Append t1 to p:
154
155name (value): p (X t2  xxx  t1 )
156parent: NULL
157previousSibling: NULL
158nextSibling: NULL
159
160  name (value): b (X)
161  parent: name (value): p (X t2  xxx  t1 )
162  previousSibling: NULL
163  nextSibling: name (value): #text ( t2 )
164
165    name (value): #text (X)
166    parent: name (value): b (X)
167    previousSibling: NULL
168    nextSibling: NULL
169
170  name (value): #text ( t2 )
171  parent: name (value): p (X t2  xxx  t1 )
172  previousSibling: name (value): b (X)
173  nextSibling: name (value): #text ( xxx )
174
175  name (value): #text ( xxx )
176  parent: name (value): p (X t2  xxx  t1 )
177  previousSibling: name (value): #text ( t2 )
178  nextSibling: name (value): #text ( t1 )
179
180  name (value): #text ( t1 )
181  parent: name (value): p (X t2  xxx  t1 )
182  previousSibling: name (value): #text ( xxx )
183  nextSibling: NULL
184
185t1 == ret: bool(true)
186
187div:
188
189name (value): div ( t3  t4  xxx )
190parent: NULL
191previousSibling: NULL
192nextSibling: NULL
193
194  name (value): #text ( t3 )
195  parent: name (value): div ( t3  t4  xxx )
196  previousSibling: NULL
197  nextSibling: name (value): b ()
198
199  name (value): b ()
200  parent: name (value): div ( t3  t4  xxx )
201  previousSibling: name (value): #text ( t3 )
202  nextSibling: name (value): #text ( t4 )
203
204    name (value): X ()
205    parent: name (value): b ()
206    previousSibling: NULL
207    nextSibling: NULL
208
209  name (value): #text ( t4 )
210  parent: name (value): div ( t3  t4  xxx )
211  previousSibling: name (value): b ()
212  nextSibling: name (value): #text ( xxx )
213
214  name (value): #text ( xxx )
215  parent: name (value): div ( t3  t4  xxx )
216  previousSibling: name (value): #text ( t4 )
217  nextSibling: NULL
218
219Insert t4 before t3:
220
221name (value): div ( t4  t3  xxx )
222parent: NULL
223previousSibling: NULL
224nextSibling: NULL
225
226  name (value): #text ( t4 )
227  parent: name (value): div ( t4  t3  xxx )
228  previousSibling: NULL
229  nextSibling: name (value): #text ( t3 )
230
231  name (value): #text ( t3 )
232  parent: name (value): div ( t4  t3  xxx )
233  previousSibling: name (value): #text ( t4 )
234  nextSibling: name (value): b ()
235
236  name (value): b ()
237  parent: name (value): div ( t4  t3  xxx )
238  previousSibling: name (value): #text ( t3 )
239  nextSibling: name (value): #text ( xxx )
240
241    name (value): X ()
242    parent: name (value): b ()
243    previousSibling: NULL
244    nextSibling: NULL
245
246  name (value): #text ( xxx )
247  parent: name (value): div ( t4  t3  xxx )
248  previousSibling: name (value): b ()
249  nextSibling: NULL
250
251
252p:
253
254name (value): p (X t2  xxx  t1 )
255parent: NULL
256previousSibling: NULL
257nextSibling: NULL
258
259  name (value): b (X)
260  parent: name (value): p (X t2  xxx  t1 )
261  previousSibling: NULL
262  nextSibling: name (value): #text ( t2 )
263
264    name (value): #text (X)
265    parent: name (value): b (X)
266    previousSibling: NULL
267    nextSibling: NULL
268
269  name (value): #text ( t2 )
270  parent: name (value): p (X t2  xxx  t1 )
271  previousSibling: name (value): b (X)
272  nextSibling: name (value): #text ( xxx )
273
274  name (value): #text ( xxx )
275  parent: name (value): p (X t2  xxx  t1 )
276  previousSibling: name (value): #text ( t2 )
277  nextSibling: name (value): #text ( t1 )
278
279  name (value): #text ( t1 )
280  parent: name (value): p (X t2  xxx  t1 )
281  previousSibling: name (value): #text ( xxx )
282  nextSibling: NULL
283
284Fragment:
285
286name (value): #document-fragment ()
287parent: NULL
288previousSibling: NULL
289nextSibling: NULL
290
291  name (value): #text ( t5 )
292  parent: name (value): #document-fragment ()
293  previousSibling: NULL
294  nextSibling: name (value): i ( frob )
295
296  name (value): i ( frob )
297  parent: name (value): #document-fragment ()
298  previousSibling: name (value): #text ( t5 )
299  nextSibling: name (value): #text ( t6 )
300
301    name (value): #text ( frob )
302    parent: name (value): i ( frob )
303    previousSibling: NULL
304    nextSibling: NULL
305
306  name (value): #text ( t6 )
307  parent: name (value): #document-fragment ()
308  previousSibling: name (value): i ( frob )
309  nextSibling: NULL
310
311Appending fragment to p:
312
313name (value): p (X t2  xxx  t1  t5  frob  t6 )
314parent: NULL
315previousSibling: NULL
316nextSibling: NULL
317
318  name (value): b (X)
319  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
320  previousSibling: NULL
321  nextSibling: name (value): #text ( t2 )
322
323    name (value): #text (X)
324    parent: name (value): b (X)
325    previousSibling: NULL
326    nextSibling: NULL
327
328  name (value): #text ( t2 )
329  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
330  previousSibling: name (value): b (X)
331  nextSibling: name (value): #text ( xxx )
332
333  name (value): #text ( xxx )
334  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
335  previousSibling: name (value): #text ( t2 )
336  nextSibling: name (value): #text ( t1 )
337
338  name (value): #text ( t1 )
339  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
340  previousSibling: name (value): #text ( xxx )
341  nextSibling: name (value): #text ( t5 )
342
343  name (value): #text ( t5 )
344  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
345  previousSibling: name (value): #text ( t1 )
346  nextSibling: name (value): i ( frob )
347
348  name (value): i ( frob )
349  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
350  previousSibling: name (value): #text ( t5 )
351  nextSibling: name (value): #text ( t6 )
352
353    name (value): #text ( frob )
354    parent: name (value): i ( frob )
355    previousSibling: NULL
356    nextSibling: NULL
357
358  name (value): #text ( t6 )
359  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
360  previousSibling: name (value): i ( frob )
361  nextSibling: NULL
362
363Fragment:
364
365name (value): #document-fragment ()
366parent: NULL
367previousSibling: NULL
368nextSibling: NULL
369
370div:
371
372name (value): div ( t4  t3  xxx )
373parent: NULL
374previousSibling: NULL
375nextSibling: NULL
376
377  name (value): #text ( t4 )
378  parent: name (value): div ( t4  t3  xxx )
379  previousSibling: NULL
380  nextSibling: name (value): #text ( t3 )
381
382  name (value): #text ( t3 )
383  parent: name (value): div ( t4  t3  xxx )
384  previousSibling: name (value): #text ( t4 )
385  nextSibling: name (value): b ()
386
387  name (value): b ()
388  parent: name (value): div ( t4  t3  xxx )
389  previousSibling: name (value): #text ( t3 )
390  nextSibling: name (value): #text ( xxx )
391
392    name (value): X ()
393    parent: name (value): b ()
394    previousSibling: NULL
395    nextSibling: NULL
396
397  name (value): #text ( xxx )
398  parent: name (value): div ( t4  t3  xxx )
399  previousSibling: name (value): b ()
400  nextSibling: NULL
401
402Inserting fragment before t4
403Error (2) on line 109: DOMNode::insertBefore(): Document Fragment is empty
404
405name (value): div ( t4  t3  xxx )
406parent: NULL
407previousSibling: NULL
408nextSibling: NULL
409
410  name (value): #text ( t4 )
411  parent: name (value): div ( t4  t3  xxx )
412  previousSibling: NULL
413  nextSibling: name (value): #text ( t3 )
414
415  name (value): #text ( t3 )
416  parent: name (value): div ( t4  t3  xxx )
417  previousSibling: name (value): #text ( t4 )
418  nextSibling: name (value): b ()
419
420  name (value): b ()
421  parent: name (value): div ( t4  t3  xxx )
422  previousSibling: name (value): #text ( t3 )
423  nextSibling: name (value): #text ( xxx )
424
425    name (value): X ()
426    parent: name (value): b ()
427    previousSibling: NULL
428    nextSibling: NULL
429
430  name (value): #text ( xxx )
431  parent: name (value): div ( t4  t3  xxx )
432  previousSibling: name (value): b ()
433  nextSibling: NULL
434
435p:
436
437name (value): p (X t2  xxx  t1  t5  frob  t6 )
438parent: NULL
439previousSibling: NULL
440nextSibling: NULL
441
442  name (value): b (X)
443  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
444  previousSibling: NULL
445  nextSibling: name (value): #text ( t2 )
446
447    name (value): #text (X)
448    parent: name (value): b (X)
449    previousSibling: NULL
450    nextSibling: NULL
451
452  name (value): #text ( t2 )
453  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
454  previousSibling: name (value): b (X)
455  nextSibling: name (value): #text ( xxx )
456
457  name (value): #text ( xxx )
458  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
459  previousSibling: name (value): #text ( t2 )
460  nextSibling: name (value): #text ( t1 )
461
462  name (value): #text ( t1 )
463  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
464  previousSibling: name (value): #text ( xxx )
465  nextSibling: name (value): #text ( t5 )
466
467  name (value): #text ( t5 )
468  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
469  previousSibling: name (value): #text ( t1 )
470  nextSibling: name (value): i ( frob )
471
472  name (value): i ( frob )
473  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
474  previousSibling: name (value): #text ( t5 )
475  nextSibling: name (value): #text ( t6 )
476
477    name (value): #text ( frob )
478    parent: name (value): i ( frob )
479    previousSibling: NULL
480    nextSibling: NULL
481
482  name (value): #text ( t6 )
483  parent: name (value): p (X t2  xxx  t1  t5  frob  t6 )
484  previousSibling: name (value): i ( frob )
485  nextSibling: NULL
486