xref: /php-src/ext/dom/tests/modern/spec/bug47530.phpt (revision 6e7adb3c)
1--TEST--
2Bug #47530 (Importing objects into document fragments creates bogus "default" namespace)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8function test_document_fragment_with_import() {
9    $doc = Dom\XMLDocument::createFromString('<html xmlns="https://php.net/something" xmlns:ns="https://php.net/whatever"><element ns:foo="https://php.net/bar"/></html>');
10    $root = $doc->documentElement;
11    $frag = $doc->createDocumentFragment();
12    $frag->appendChild($doc->importNode($root->firstChild));
13    $root->appendChild($frag);
14    echo $doc->saveXml(), "\n";
15}
16
17function test_document_fragment_without_import() {
18    $doc = Dom\XMLDocument::createFromString('<html xmlns=""><element xmlns:foo="https://php.net/bar"/></html>');
19    $frag = $doc->createDocumentFragment();
20    $frag->appendChild($doc->createElementNS('https://php.net/bar', 'bar'));
21    $frag->appendChild($doc->createElementNS('', 'bar'));
22    $element = $doc->documentElement->firstChild;
23    $element->appendChild($frag);
24    unset($frag); // Free fragment, should not break getting the namespaceURI below
25    echo $doc->saveXml(), "\n";
26    unset($doc);
27    var_dump($element->firstChild->tagName);
28    var_dump($element->firstChild->namespaceURI);
29    var_dump($element->firstChild->nextSibling->tagName);
30    var_dump($element->firstChild->nextSibling->namespaceURI);
31}
32
33function test_document_import() {
34    $xml = <<<XML
35<?xml version="1.0" encoding="utf-8"?>
36<feed xmlns="http://www.w3.org/2005/Atom">
37<div xmlns="http://www.w3.org/1999/xhtml">
38    <p>Test-Text</p>
39</div>
40</feed>
41XML;
42
43    $dom = Dom\XMLDocument::createFromString($xml);
44
45    $dom2 = Dom\XMLDocument::createEmpty();
46    $importedNode = $dom2->importNode($dom->documentElement, true);
47    $dom2->appendChild($importedNode);
48
49    echo $dom2->saveXml(), "\n";
50}
51
52function test_partial_document_import() {
53    $xml = <<<XML
54<?xml version="1.0" encoding="utf-8"?>
55<feed xmlns="http://www.w3.org/1999/xhtml" xmlns:test="https://php.net/test" xmlns:example="https://php.net/example">
56<div>
57    <p>Test-Text</p>
58    <example:p>More test text</example:p>
59    <test:p>Even more test text</test:p>
60</div>
61</feed>
62XML;
63
64    $dom = Dom\XMLDocument::createFromString($xml);
65
66    $dom2 = Dom\XMLDocument::createFromString('<?xml version="1.0"?><container xmlns:test="https://php.net/test" xmlns="https://php.net/example"/>');
67    $importedNode = $dom2->importNode($dom->documentElement, true);
68    $dom2->documentElement->appendChild($importedNode);
69
70    // Freeing the original document shouldn't break the other document
71    unset($importedNode);
72    unset($dom);
73
74    echo $dom2->saveXml(), "\n";
75}
76
77function test_document_import_with_attributes() {
78    $dom = Dom\XMLDocument::createFromString('<?xml version="1.0"?><div xmlns="https://php.net/default" xmlns:example="https://php.net/example"><p example:test="test"/><i/></div>');
79    $dom2 = Dom\XMLDocument::createFromString('<?xml version="1.0"?><div xmlns:example="https://php.net/somethingelse"/>');
80    $dom2->documentElement->appendChild($dom2->importNode($dom->documentElement->firstChild));
81    echo $dom2->saveXml(), "\n";
82
83    $dom2->documentElement->firstChild->appendChild($dom2->importNode($dom->documentElement->firstChild->nextSibling));
84    echo $dom2->saveXml(), "\n";
85}
86
87function test_appendChild_with_shadowing() {
88    $dom = Dom\XMLDocument::createFromString('<?xml version="1.0"?><container xmlns:default="http://php.net/default"><a xmlns:foo="http://php.net/bar"/><b xmlns:foo="http://php.net/foo"><default:test foo:bar=""/><foo:test2/></b></container>');
89
90    $a = $dom->documentElement->firstElementChild;
91    $b = $a->nextSibling;
92    $b->remove();
93    $a->appendChild($b);
94
95    echo $dom->saveXml(), "\n";
96}
97
98echo "-- Test document fragment with import --\n";
99test_document_fragment_with_import();
100echo "-- Test document fragment without import --\n";
101test_document_fragment_without_import();
102echo "-- Test document import --\n";
103test_document_import();
104echo "-- Test partial document import --\n";
105test_partial_document_import();
106echo "-- Test document import with attributes --\n";
107test_document_import_with_attributes();
108echo "-- Test appendChild with shadowing --\n";
109test_appendChild_with_shadowing();
110
111?>
112--EXPECT--
113-- Test document fragment with import --
114<?xml version="1.0" encoding="UTF-8"?>
115<html xmlns="https://php.net/something" xmlns:ns="https://php.net/whatever"><element ns:foo="https://php.net/bar"/></html>
116-- Test document fragment without import --
117<?xml version="1.0" encoding="UTF-8"?>
118<html xmlns=""><element xmlns:foo="https://php.net/bar"><foo:bar/><bar/></element></html>
119string(3) "bar"
120string(19) "https://php.net/bar"
121string(3) "bar"
122NULL
123-- Test document import --
124<?xml version="1.0" encoding="UTF-8"?>
125<feed xmlns="http://www.w3.org/2005/Atom">
126<div xmlns="http://www.w3.org/1999/xhtml">
127    <p>Test-Text</p>
128</div>
129</feed>
130-- Test partial document import --
131<?xml version="1.0" encoding="UTF-8"?>
132<container xmlns:test="https://php.net/test" xmlns="https://php.net/example"><feed xmlns="http://www.w3.org/1999/xhtml" xmlns:example="https://php.net/example">
133<div>
134    <p>Test-Text</p>
135    <example:p>More test text</example:p>
136    <test:p>Even more test text</test:p>
137</div>
138</feed></container>
139-- Test document import with attributes --
140<?xml version="1.0" encoding="UTF-8"?>
141<div xmlns:example="https://php.net/somethingelse"><p xmlns="https://php.net/default" xmlns:example="https://php.net/example" example:test="test"/></div>
142<?xml version="1.0" encoding="UTF-8"?>
143<div xmlns:example="https://php.net/somethingelse"><p xmlns="https://php.net/default" xmlns:example="https://php.net/example" example:test="test"><i/></p></div>
144-- Test appendChild with shadowing --
145<?xml version="1.0" encoding="UTF-8"?>
146<container xmlns:default="http://php.net/default"><a xmlns:foo="http://php.net/bar"><b xmlns:foo="http://php.net/foo"><default:test foo:bar=""/><foo:test2/></b></a></container>
147