1--TEST--
2Document::createAttributeNS()
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8require __DIR__ . '/dump_attr.inc';
9
10function testErrorCase($dom, $ns, $qname) {
11    try {
12        $dom->createAttributeNS($ns, $qname);
13    } catch (DOMException $e) {
14        $ns_readable = is_null($ns) ? 'null' : "\"$ns\"";
15        echo "($ns_readable, \"$qname\"): {$e->getMessage()}\n";
16    }
17}
18
19$dom = DOM\HTMLDocument::createEmpty();
20$attrs = [];
21$attrs[] = $dom->createAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:foo');
22$attrs[] = $dom->createAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:bar');
23$attrs[] = $dom->createAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns');
24$attrs[] = $dom->createAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:foo');
25$attrs[] = $dom->createAttributeNS('urn:a', 'foo:bar');
26$attrs[] = $dom->createAttributeNS('urn:a', 'bar:bar');
27$attrs[] = $dom->createAttributeNS('http://www.w3.org/2000/xmlns', 'foo:bar');
28
29echo "--- Error cases ---\n";
30
31testErrorCase($dom, '', 'bar:bar');
32testErrorCase($dom, null, 'bar:bar');
33testErrorCase($dom, 'urn:a', '@');
34testErrorCase($dom, 'urn:a', 'foo:bar:baz');
35testErrorCase($dom, 'http://www.w3.org/2000/xmlns', 'xmlns');
36testErrorCase($dom, 'http://www.w3.org/2000/xmlns', 'xmlns:bar');
37testErrorCase($dom, 'http://www.w3.org/2000/xmlns', 'xml:foo');
38
39echo "\n--- Normal cases ---\n";
40
41// Test after creating to make sure they cannot corrupt each other
42foreach ($attrs as $attr) {
43    dumpAttr($attr);
44}
45
46// Test in document
47$root = $dom->appendChild($dom->createElement('foo'));
48foreach ($attrs as $attr) {
49    $root->setAttributeNodeNS($attr);
50}
51
52echo $dom->saveHTML(), "\n";
53
54echo "\n--- NULL prefix cases ---\n";
55
56// Test multiple "null prefixes" after having a root
57$attrs = [];
58$attrs[] = $dom->createAttributeNS(null, 'baz1');
59$attrs[] = $dom->createAttributeNS(null, 'baz2');
60$attrs[] = $dom->createAttributeNS('', 'baz1');
61$attrs[] = $dom->createAttributeNS('', 'baz2');
62foreach ($attrs as $attr) {
63    dumpAttr($attr);
64    $root->setAttributeNodeNS($attr);
65}
66
67echo $dom->saveHTML(), "\n";
68?>
69--EXPECT--
70--- Error cases ---
71("", "bar:bar"): Namespace Error
72(null, "bar:bar"): Namespace Error
73("urn:a", "@"): Invalid Character Error
74("urn:a", "foo:bar:baz"): Invalid Character Error
75("http://www.w3.org/2000/xmlns", "xmlns"): Namespace Error
76("http://www.w3.org/2000/xmlns", "xmlns:bar"): Namespace Error
77("http://www.w3.org/2000/xmlns", "xml:foo"): Namespace Error
78
79--- Normal cases ---
80Attr: xmlns:foo
81string(5) "xmlns"
82string(9) "xmlns:foo"
83string(29) "http://www.w3.org/2000/xmlns/"
84Attr: xmlns:bar
85string(5) "xmlns"
86string(9) "xmlns:bar"
87string(29) "http://www.w3.org/2000/xmlns/"
88Attr: xmlns
89NULL
90string(5) "xmlns"
91string(29) "http://www.w3.org/2000/xmlns/"
92Attr: xml:foo
93string(3) "xml"
94string(7) "xml:foo"
95string(36) "http://www.w3.org/XML/1998/namespace"
96Attr: foo:bar
97string(3) "foo"
98string(7) "foo:bar"
99string(5) "urn:a"
100Attr: bar:bar
101string(3) "bar"
102string(7) "bar:bar"
103string(5) "urn:a"
104Attr: foo:bar
105string(3) "foo"
106string(7) "foo:bar"
107string(28) "http://www.w3.org/2000/xmlns"
108<foo xmlns:foo="" xmlns:bar="" xmlns="" xml:foo="" bar:bar="" foo:bar=""></foo>
109
110--- NULL prefix cases ---
111Attr: baz1
112NULL
113string(4) "baz1"
114NULL
115Attr: baz2
116NULL
117string(4) "baz2"
118NULL
119Attr: baz1
120NULL
121string(4) "baz1"
122NULL
123Attr: baz2
124NULL
125string(4) "baz2"
126NULL
127<foo xmlns:foo="" xmlns:bar="" xmlns="" xml:foo="" bar:bar="" foo:bar="" baz1="" baz2=""></foo>
128