--TEST--
Bug #47530 (Importing objects into document fragments creates bogus "default" namespace)
--EXTENSIONS--
dom
--FILE--
loadXML('');
$root = $doc->documentElement;
$frag = $doc->createDocumentFragment();
$frag->appendChild($doc->importNode($root->firstChild));
$root->appendChild($frag);
echo $doc->saveXML();
}
function test_document_fragment_without_import() {
$doc = new DOMDocument;
$doc->loadXML('');
$frag = $doc->createDocumentFragment();
$frag->appendChild($doc->createElementNS('https://php.net/bar', 'bar'));
$frag->appendChild($doc->createElementNS('', 'bar'));
$element = $doc->documentElement->firstChild;
$element->appendChild($frag);
unset($frag); // Free fragment, should not break getting the namespaceURI below
echo $doc->saveXML();
unset($doc);
var_dump($element->firstChild->tagName);
var_dump($element->firstChild->namespaceURI);
}
function test_document_import() {
$xml = <<
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
$dom2 = new DOMDocument();
$importedNode = $dom2->importNode($dom->documentElement, true);
$dom2->appendChild($importedNode);
echo $dom2->saveXML();
}
function test_partial_document_import() {
$xml = <<
Test-Text
More test text
Even more test text
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
$dom2 = new DOMDocument();
$dom2->loadXML('');
$importedNode = $dom2->importNode($dom->documentElement, true);
$dom2->documentElement->appendChild($importedNode);
// Freeing the original document shouldn't break the other document
unset($importedNode);
unset($dom);
echo $dom2->saveXML();
}
function test_document_import_with_attributes() {
$dom = new DOMDocument();
$dom->loadXML('');
$dom2 = new DOMDocument();
$dom2->loadXML('');
$dom2->documentElement->appendChild($dom2->importNode($dom->documentElement->firstChild));
echo $dom2->saveXML(), "\n";
$dom2->documentElement->firstChild->appendChild($dom2->importNode($dom->documentElement->firstChild->nextSibling));
echo $dom2->saveXML(), "\n";
}
function test_appendChild_with_shadowing() {
$dom = new DOMDocument();
$dom->loadXML('');
$a = $dom->documentElement->firstElementChild;
$b = $a->nextSibling;
$b->remove();
$a->appendChild($b);
echo $dom->saveXML(), "\n";
}
echo "-- Test document fragment with import --\n";
test_document_fragment_with_import();
echo "-- Test document fragment without import --\n";
test_document_fragment_without_import();
echo "-- Test document import --\n";
test_document_import();
echo "-- Test partial document import --\n";
test_partial_document_import();
echo "-- Test document import with attributes --\n";
test_document_import_with_attributes();
echo "-- Test appendChild with shadowing --\n";
test_appendChild_with_shadowing();
?>
--EXPECT--
-- Test document fragment with import --
-- Test document fragment without import --
string(7) "foo:bar"
string(19) "https://php.net/bar"
-- Test document import --
Test-Text
-- Test partial document import --
Test-Text
More test text
Even more test text
-- Test document import with attributes --
-- Test appendChild with shadowing --