|
| 1 | +--TEST-- |
| 2 | +Cloning an attribute should retain its namespace 01 |
| 3 | +--EXTENSIONS-- |
| 4 | +dom |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | + |
| 8 | +function createTestDocument() { |
| 9 | + $dom = new DOMDocument; |
| 10 | + $dom->loadXML('<?xml version="1.0"?><container/>'); |
| 11 | + $dom->documentElement->setAttributeNs("some:ns", "foo:bar", "value"); |
| 12 | + |
| 13 | + $attr = $dom->documentElement->getAttributeNodeNs("some:ns", "bar"); |
| 14 | + $clone = $attr->cloneNode(true); |
| 15 | + |
| 16 | + return [$dom, $clone]; |
| 17 | +} |
| 18 | + |
| 19 | +[$dom, $clone] = createTestDocument(); |
| 20 | +var_dump($clone->prefix, $clone->namespaceURI); |
| 21 | + |
| 22 | +echo "--- Re-adding a namespaced attribute ---\n"; |
| 23 | + |
| 24 | +[$dom, $clone] = createTestDocument(); |
| 25 | +$dom->documentElement->removeAttributeNs("some:ns", "bar"); |
| 26 | +echo $dom->saveXML(); |
| 27 | +$dom->documentElement->setAttributeNodeNs($clone); |
| 28 | +echo $dom->saveXML(); |
| 29 | + |
| 30 | +echo "--- Re-adding a namespaced attribute, with the namespace deleted (setAttributeNodeNs variation) ---\n"; |
| 31 | + |
| 32 | +function readd_test(string $method) { |
| 33 | + [$dom, $clone] = createTestDocument(); |
| 34 | + $dom->documentElement->removeAttributeNs("some:ns", "bar"); |
| 35 | + $dom->documentElement->removeAttribute("xmlns:foo"); |
| 36 | + echo $dom->saveXML(); |
| 37 | + $child = $dom->documentElement->appendChild($dom->createElement("child")); |
| 38 | + $child->{$method}($clone); |
| 39 | + echo $dom->saveXML(); |
| 40 | +} |
| 41 | + |
| 42 | +readd_test("setAttributeNodeNs"); |
| 43 | + |
| 44 | +echo "--- Re-adding a namespaced attribute, with the namespace deleted (setAttributeNode variation) ---\n"; |
| 45 | + |
| 46 | +readd_test("setAttributeNode"); |
| 47 | + |
| 48 | +echo "--- Re-adding a namespaced attribute, with the namespace deleted (appendChild variation) ---\n"; |
| 49 | + |
| 50 | +readd_test("appendChild"); |
| 51 | + |
| 52 | +echo "--- Removing the document reference should not crash ---\n"; |
| 53 | + |
| 54 | +[$dom, $clone] = createTestDocument(); |
| 55 | +unset($dom); |
| 56 | +var_dump($clone->prefix, $clone->namespaceURI); |
| 57 | + |
| 58 | +?> |
| 59 | +--EXPECT-- |
| 60 | +string(3) "foo" |
| 61 | +string(7) "some:ns" |
| 62 | +--- Re-adding a namespaced attribute --- |
| 63 | +<?xml version="1.0"?> |
| 64 | +<container xmlns:foo="some:ns"/> |
| 65 | +<?xml version="1.0"?> |
| 66 | +<container xmlns:foo="some:ns" foo:bar="value"/> |
| 67 | +--- Re-adding a namespaced attribute, with the namespace deleted (setAttributeNodeNs variation) --- |
| 68 | +<?xml version="1.0"?> |
| 69 | +<container/> |
| 70 | +<?xml version="1.0"?> |
| 71 | +<container><child xmlns:foo="some:ns" foo:bar="value"/></container> |
| 72 | +--- Re-adding a namespaced attribute, with the namespace deleted (setAttributeNode variation) --- |
| 73 | +<?xml version="1.0"?> |
| 74 | +<container/> |
| 75 | +<?xml version="1.0"?> |
| 76 | +<container><child xmlns:foo="some:ns" foo:bar="value"/></container> |
| 77 | +--- Re-adding a namespaced attribute, with the namespace deleted (appendChild variation) --- |
| 78 | +<?xml version="1.0"?> |
| 79 | +<container/> |
| 80 | +<?xml version="1.0"?> |
| 81 | +<container><child xmlns:foo="some:ns" foo:bar="value"/></container> |
| 82 | +--- Removing the document reference should not crash --- |
| 83 | +string(3) "foo" |
| 84 | +string(7) "some:ns" |
0 commit comments