-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom_unittest.js
131 lines (107 loc) · 3.93 KB
/
dom_unittest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2005 Google Inc.
// All Rights Reserved
//
// Unit test for dom.js. This also uses DOMParser, so it only runs
// in firefox.
//
// Author: Steffen Meschkat <[email protected]>
// Junji Takagi <[email protected]>
//********************************************
// DGF BEWARE! You MUST update this function if you add tests!
//********************************************
function exposeTestFunctionNames() {
return ['testXmlParse', 'testXmlParseWeird', 'testXmlParseJapanese', 'testXmlResolveEntities'];
}
function testXmlParse() {
if (typeof(DOMParser) == 'undefined') return;
var xml = [
'<page>',
'<request>',
'<q id="q">new york</q>',
'</request>',
'<location lat="100" ', "lon='200'/>",
'</page>'
].join('');
dom = xmlParse(xml);
dom1 = (new DOMParser).parseFromString(xml, 'text/xml');
doTestXmlParse(dom, dom1);
dom = xmlParse('<?xml version="1.0"?>' + xml);
dom1 = xmlParse("<?xml version='1.1'?>" + xml);
doTestXmlParse(dom, dom1);
var tag = 'q';
var byTag = dom.getElementsByTagName(tag);
assertEquals(1, byTag.length);
assertEquals(tag, byTag[0].nodeName);
var id = 'q';
var byId = dom.getElementById(id);
assertNotNull(byId);
assertEquals(id, byId.getAttribute('id'));
}
function testXmlParseWeird() {
var xml = [
'<_>',
'<_.:->',
'<:>!"#$%&\'()*+,-./:;<=>?[\\]^_`{|}~</:>',
'</_.:->',
'<:-_. _=".-" :="-."/>',
'</_>'
].join('');
// DOMParser seems not supporting a tagname that starts with ':', so
// avoid comparing xmlParse() and DomParser.parseFromString() here.
dom = xmlParse('<?xml version="1.0"?>' + xml);
dom1 = xmlParse("<?xml version='1.1'?>" + xml);
doTestXmlParse(dom, dom1);
}
function testXmlParseJapanese() {
if (typeof(DOMParser) == 'undefined') return;
var xml = [
'<\u30da\u30fc\u30b8>',
'<\u30ea\u30af\u30a8\u30b9\u30c8>',
'<\u30af\u30a8\u30ea>\u6771\u4eac</\u30af\u30a8\u30ea>',
'</\u30ea\u30af\u30a8\u30b9\u30c8>',
'<\u4f4d\u7f6e \u7def\u5ea6="\u4e09\u5341\u4e94" ',
"\u7d4c\u5ea6='\u767e\u56db\u5341'/>",
'</\u30da\u30fc\u30b8>'
].join('');
dom = xmlParse(xml);
dom1 = (new DOMParser).parseFromString(xml, 'text/xml');
doTestXmlParse(dom, dom1);
dom = xmlParse('<?xml version="1.0"?>' + xml);
dom1 = xmlParse("<?xml version='1.1'?>" + xml);
doTestXmlParse(dom, dom1);
}
function doTestXmlParse(dom, dom1) {
assertEquals('xmlText', xmlText(dom), xmlText(dom1));
assertEquals('#document',
dom.nodeName,
dom1.nodeName);
assertEquals('documentElement', dom.documentElement, dom.firstChild);
assertEquals('documentElement', dom1.documentElement, dom1.firstChild);
assertEquals('parentNode', dom.parentNode, null);
assertEquals('parentNode', dom1.parentNode, null);
assertEquals('parentNode', dom.documentElement.parentNode, dom);
assertEquals('parentNode', dom1.documentElement.parentNode, dom1);
assertEquals('page',
dom.documentElement.nodeName,
dom1.documentElement.nodeName);
assertEquals('dom.childNodes.length',
dom.childNodes.length,
dom1.childNodes.length);
assertEquals('dom.childNodes.length',
dom.childNodes.length,
dom1.childNodes.length);
assertEquals('page.childNodes.length',
dom.firstChild.childNodes.length,
dom1.firstChild.childNodes.length);
assertEquals('page.childNodes.length',
dom.firstChild.childNodes.length,
dom1.firstChild.childNodes.length);
assertEquals('location.attributes.length',
dom.firstChild.childNodes[1].attributes.length,
dom1.firstChild.childNodes[1].attributes.length);
assertEquals('location.attributes.length',
dom.firstChild.childNodes[1].attributes.length, 2);
}
function testXmlResolveEntities() {
assertEquals('";"', xmlResolveEntities('";"'));
}