1--TEST--
2DOMElement::toggleAttribute()
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$html = new DOMDocument();
9$html->loadHTML('<!DOCTYPE HTML><html id="test"></html>');
10$xml = new DOMDocument();
11$xml->loadXML('<?xml version="1.0"?><html id="test"></html>');
12
13try {
14    var_dump($html->documentElement->toggleAttribute("\0"));
15} catch (DOMException $e) {
16    echo $e->getMessage(), "\n";
17}
18
19echo "--- Selected attribute tests (HTML) ---\n";
20
21var_dump($html->documentElement->toggleAttribute("SELECTED", false));
22echo $html->saveHTML();
23var_dump($html->documentElement->toggleAttribute("SELECTED"));
24echo $html->saveHTML();
25var_dump($html->documentElement->toggleAttribute("selected", true));
26echo $html->saveHTML();
27var_dump($html->documentElement->toggleAttribute("selected"));
28echo $html->saveHTML();
29
30echo "--- Selected attribute tests (XML) ---\n";
31
32var_dump($xml->documentElement->toggleAttribute("SELECTED", false));
33echo $xml->saveXML();
34var_dump($xml->documentElement->toggleAttribute("SELECTED"));
35echo $xml->saveXML();
36var_dump($xml->documentElement->toggleAttribute("selected", true));
37echo $xml->saveXML();
38var_dump($xml->documentElement->toggleAttribute("selected"));
39echo $xml->saveXML();
40
41echo "--- id attribute tests ---\n";
42
43var_dump($html->getElementById("test") === NULL);
44var_dump($html->documentElement->toggleAttribute("id"));
45var_dump($html->getElementById("test") === NULL);
46
47echo "--- Namespace tests ---\n";
48
49$dom = new DOMDocument();
50$dom->loadXML("<?xml version='1.0'?><container xmlns='some:ns' xmlns:foo='some:ns2' xmlns:anotherone='some:ns3'><foo:bar/><baz/></container>");
51
52echo "Toggling namespaces:\n";
53var_dump($dom->documentElement->toggleAttribute('xmlns'));
54echo $dom->saveXML();
55var_dump($dom->documentElement->toggleAttribute('xmlns:anotherone'));
56echo $dom->saveXML();
57var_dump($dom->documentElement->toggleAttribute('xmlns:anotherone'));
58echo $dom->saveXML();
59var_dump($dom->documentElement->toggleAttribute('xmlns:foo'));
60echo $dom->saveXML();
61var_dump($dom->documentElement->toggleAttribute('xmlns:nope', false));
62echo $dom->saveXML();
63
64echo "Toggling namespaced attributes:\n";
65var_dump($dom->documentElement->toggleAttribute('test:test'));
66var_dump($dom->documentElement->firstElementChild->toggleAttribute('foo:test'));
67var_dump($dom->documentElement->firstElementChild->toggleAttribute('doesnotexist:test'));
68var_dump($dom->documentElement->firstElementChild->toggleAttribute('doesnotexist:test2', false));
69echo $dom->saveXML();
70
71echo "namespace of test:test = ";
72var_dump($dom->documentElement->getAttributeNode('test:test')->namespaceURI);
73echo "namespace of foo:test = ";
74var_dump($dom->documentElement->firstElementChild->getAttributeNode('foo:test')->namespaceURI);
75echo "namespace of doesnotexist:test = ";
76var_dump($dom->documentElement->firstElementChild->getAttributeNode('doesnotexist:test')->namespaceURI);
77
78echo "Toggling namespaced attributes:\n";
79var_dump($dom->documentElement->toggleAttribute('test:test'));
80var_dump($dom->documentElement->firstElementChild->toggleAttribute('foo:test'));
81var_dump($dom->documentElement->firstElementChild->toggleAttribute('doesnotexist:test'));
82var_dump($dom->documentElement->firstElementChild->toggleAttribute('doesnotexist:test2', true));
83var_dump($dom->documentElement->firstElementChild->toggleAttribute('doesnotexist:test3', false));
84echo $dom->saveXML();
85
86echo "Checking toggled namespace:\n";
87var_dump($dom->documentElement->getAttribute('xmlns:anotheron'));
88
89?>
90--EXPECT--
91Invalid Character Error
92--- Selected attribute tests (HTML) ---
93bool(false)
94<!DOCTYPE HTML>
95<html id="test"></html>
96bool(true)
97<!DOCTYPE HTML>
98<html id="test" selected></html>
99bool(true)
100<!DOCTYPE HTML>
101<html id="test" selected></html>
102bool(false)
103<!DOCTYPE HTML>
104<html id="test"></html>
105--- Selected attribute tests (XML) ---
106bool(false)
107<?xml version="1.0"?>
108<html id="test"/>
109bool(true)
110<?xml version="1.0"?>
111<html id="test" SELECTED=""/>
112bool(true)
113<?xml version="1.0"?>
114<html id="test" SELECTED="" selected=""/>
115bool(false)
116<?xml version="1.0"?>
117<html id="test" SELECTED=""/>
118--- id attribute tests ---
119bool(false)
120bool(false)
121bool(true)
122--- Namespace tests ---
123Toggling namespaces:
124bool(false)
125<?xml version="1.0"?>
126<container xmlns:foo="some:ns2" xmlns:anotherone="some:ns3" xmlns="some:ns"><foo:bar/><baz/></container>
127bool(false)
128<?xml version="1.0"?>
129<container xmlns:foo="some:ns2" xmlns="some:ns"><foo:bar/><baz/></container>
130bool(true)
131<?xml version="1.0"?>
132<container xmlns:foo="some:ns2" xmlns="some:ns" xmlns:anotherone=""><foo:bar/><baz/></container>
133bool(false)
134<?xml version="1.0"?>
135<container xmlns="some:ns" xmlns:anotherone=""><foo:bar xmlns:foo="some:ns2"/><baz/></container>
136bool(false)
137<?xml version="1.0"?>
138<container xmlns="some:ns" xmlns:anotherone=""><foo:bar xmlns:foo="some:ns2"/><baz/></container>
139Toggling namespaced attributes:
140bool(true)
141bool(true)
142bool(true)
143bool(false)
144<?xml version="1.0"?>
145<container xmlns="some:ns" xmlns:anotherone="" test:test=""><foo:bar xmlns:foo="some:ns2" foo:test="" doesnotexist:test=""/><baz/></container>
146namespace of test:test = NULL
147namespace of foo:test = string(8) "some:ns2"
148namespace of doesnotexist:test = NULL
149Toggling namespaced attributes:
150bool(false)
151bool(false)
152bool(false)
153bool(true)
154bool(false)
155<?xml version="1.0"?>
156<container xmlns="some:ns" xmlns:anotherone=""><foo:bar xmlns:foo="some:ns2" doesnotexist:test2=""/><baz/></container>
157Checking toggled namespace:
158string(0) ""
159