1--TEST-- 2GH-9628 (Implicitly removing nodes from \DOMDocument breaks existing references) - advanced variation 3--EXTENSIONS-- 4dom 5--FILE-- 6<?php 7$html = <<<'HTML' 8<p> 9 <span> 10 <strong> 11 <span> 12 Test 13 </span> 14 </strong> 15 </span> 16</p> 17HTML; 18 19$doc = new \DOMDocument('1.0', 'UTF-8'); 20$doc->loadHTML( 21 '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $html . '</body></html>' 22); 23$xpath = new \DOMXPath($doc); 24 25$spans = []; 26foreach ($xpath->query('//span') as $span) { 27 $spans[] = $span; 28} 29 30\assert(\count($spans) === 2); 31 32foreach ($spans as $span) { 33 \assert($span instanceof \DOMElement); 34 \assert($span->ownerDocument === $doc); 35} 36 37foreach ($spans as $span) { 38 \assert($span instanceof \DOMElement); 39 40 // This call will fail for the second `<span>`. It does not really 41 // matter what is accessed here, the error message will be the same. 42 $span->hasAttribute('test'); 43 44 while ($span->childNodes->length) { 45 $span->removeChild($span->childNodes[0]); 46 } 47 $span->appendChild( 48 $span->ownerDocument->createTextNode('Hello World') 49 ); 50} 51 52echo $spans[0]->textContent . "\n"; // Hello World 53?> 54--EXPECT-- 55Hello World 56