1--TEST--
2DOMDocument node list item cache invalidation
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8echo "-- Switch document test --\n";
9
10$doc = new DOMDocument();
11$doc->loadHTML('<p>hello</p><p>world</p>');
12
13$elements = $doc->getElementsByTagName('p');
14$elements->item(0); // Activate item cache
15$doc->loadHTML('<p>A</p><p>B</p><p>C</p>');
16var_dump($elements);
17var_dump($elements->item(0)->textContent); // First lookup
18var_dump($elements->item(2)->textContent); // Uses cache
19var_dump($elements->item(1)->textContent); // Does not use cache
20
21echo "-- Remove cached item test --\n";
22
23$doc = new DOMDocument();
24$doc->loadHTML('<p>hello</p><p>world</p><p>!</p>');
25
26$elements = $doc->getElementsByTagName('p');
27$item = $elements->item(0); // Activate item cache
28var_dump($item->textContent);
29$item->remove();
30// Now element 0 means "world", and 1 means "!"
31unset($item);
32$item = $elements->item(1);
33var_dump($item->textContent);
34
35echo "-- Removal of cached item in loop test --\n";
36
37$doc = new DOMDocument;
38$doc->loadXML( '<root><e i="1"/><e i="2"/><e i="3"/><e i="4"/><e i="5"/><e i="6"/><e i="7"/><e i="8"/><e i="9"/><e i="10"/><e i="11"/><e i="12"/><e i="13"/><e i="14"/><e i="15"/></root>' );
39$root = $doc->documentElement;
40
41$i = 0;
42$elements = $root->getElementsByTagName('e');
43for ($i = 0; $i < 11; $i++) {
44  $node = $elements->item($i);
45  print $node->getAttribute('i') . ' ';
46  if ($i++ % 2 == 0)
47    $root->removeChild( $node );
48}
49print "\n";
50
51?>
52--EXPECTF--
53-- Switch document test --
54object(DOMNodeList)#2 (1) {
55  ["length"]=>
56  int(3)
57}
58string(1) "A"
59string(1) "C"
60string(1) "B"
61-- Remove cached item test --
62string(5) "hello"
63string(1) "!"
64-- Removal of cached item in loop test --
651 4 7 10 13
66Fatal error: Uncaught Error: Call to a member function getAttribute() on null in %s:%d
67Stack trace:
68#0 {main}
69  thrown in %s on line %d
70