Debian Codegraph @

closure-library/closure/goog/dom/xml_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.dom.xmlTest');
16 goog.setTestOnly('goog.dom.xmlTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.dom.xml');
21 goog.require('goog.testing.jsunit');
22 goog.require('goog.userAgent');
23
24 function testSerialize() {
25 var doc = goog.dom.xml.createDocument();
26 var node = doc.createElement('root');
27 doc.appendChild(node);
28
29 var serializedNode = goog.dom.xml.serialize(node);
30 assertTrue(/<root ?\/>/.test(serializedNode));
31
32 var serializedDoc = goog.dom.xml.serialize(doc);
33 assertTrue(/(<\?xml version="1.0"\?>)?<root ?\/>/.test(serializedDoc));
34 }
35
36 function testSerializeWithActiveX() {
37 // Prefer ActiveXObject if available.
38 var doc = goog.dom.xml.createDocument('', '', true);
39 var node = doc.createElement('root');
40 doc.appendChild(node);
41
42 var serializedNode = goog.dom.xml.serialize(node);
43 assertTrue(/<root ?\/>/.test(serializedNode));
44
45 var serializedDoc = goog.dom.xml.serialize(doc);
46 assertTrue(/(<\?xml version="1.0"\?>)?<root ?\/>/.test(serializedDoc));
47 }
48
49 function testBelowMaxDepthInIE() {
50 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
51 // This value is only effective in IE8 and below
52 goog.dom.xml.MAX_ELEMENT_DEPTH = 5;
53 var junk = '<a><b><c><d><e>Hello</e></d></c></b></a>';
54 var doc = goog.dom.xml.loadXml(junk);
55 assertEquals(
56 'Should not have caused a parse error', 0, Number(doc.parseError));
57 }
58 }
59
60 function testAboveMaxDepthInIE() {
61 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
62 // This value is only effective in IE8 and below
63 goog.dom.xml.MAX_ELEMENT_DEPTH = 4;
64 var junk = '<a><b><c><d><e>Hello</e></d></c></b></a>';
65 var doc = goog.dom.xml.loadXml(junk);
66 assertNotEquals(
67 'Should have caused a parse error', 0, Number(doc.parseError));
68 }
69 }
70
71 function testBelowMaxSizeInIE() {
72 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
73 // This value is only effective in IE8 and below
74 goog.dom.xml.MAX_XML_SIZE_KB = 1;
75 var junk = '<a>' + new Array(50).join('<b>junk</b>') + '</a>';
76 var doc = goog.dom.xml.loadXml(junk);
77 assertEquals(
78 'Should not have caused a parse error', 0, Number(doc.parseError));
79 }
80 }
81
82 function testMaxSizeInIE() {
83 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
84 // This value is only effective in IE8 and below
85 goog.dom.xml.MAX_XML_SIZE_KB = 1;
86 var junk = '<a>' + new Array(1000).join('<b>junk</b>') + '</a>';
87 var doc = goog.dom.xml.loadXml(junk);
88 assertNotEquals(
89 'Should have caused a parse error', 0, Number(doc.parseError));
90 }
91 }
92
93 function testSelectSingleNodeNoActiveX() {
94 if (goog.userAgent.IE) {
95 return;
96 }
97
98 var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b></a>');
99 var node = xml.firstChild;
100 var bNode = goog.dom.xml.selectSingleNode(node, 'b');
101 assertNotNull(bNode);
102 }
103
104 function testSelectSingleNodeWithActiveX() {
105 // Enable ActiveXObject so IE has xpath support.
106 var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b></a>', true);
107 var node = xml.firstChild;
108 var bNode = goog.dom.xml.selectSingleNode(node, 'b');
109 assertNotNull(bNode);
110 }
111
112 function testSelectNodesNoActiveX() {
113 if (goog.userAgent.IE) {
114 return;
115 }
116
117 var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b><b>foo</b></a>');
118 var node = xml.firstChild;
119 var bNodes = goog.dom.xml.selectNodes(node, 'b');
120 assertNotNull(bNodes);
121 assertEquals(2, bNodes.length);
122 }
123
124 function testSelectNodesWithActiveX() {
125 var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b><b>foo</b></a>', true);
126 var node = xml.firstChild;
127 var bNodes = goog.dom.xml.selectNodes(node, 'b');
128 assertNotNull(bNodes);
129 assertEquals(2, bNodes.length);
130 }
131
132 function testSetAttributes() {
133 var xmlElement = goog.dom.xml.createDocument().createElement('root');
134 var domElement = goog.dom.createElement(goog.dom.TagName.DIV);
135 var attrs =
136 {name: 'test3', title: 'A title', random: 'woop', cellpadding: '123'};
137
138 goog.dom.xml.setAttributes(xmlElement, attrs);
139 goog.dom.xml.setAttributes(domElement, attrs);
140
141 assertEquals('test3', xmlElement.getAttribute('name'));
142 assertEquals('test3', domElement.getAttribute('name'));
143
144 assertEquals('A title', xmlElement.getAttribute('title'));
145 assertEquals('A title', domElement.getAttribute('title'));
146
147 assertEquals('woop', xmlElement.getAttribute('random'));
148 assertEquals('woop', domElement.getAttribute('random'));
149
150 assertEquals('123', xmlElement.getAttribute('cellpadding'));
151 assertEquals('123', domElement.getAttribute('cellpadding'));
152 }