1
|
// Copyright 2005 Google Inc.
|
2
|
// All Rights Reserved
|
3
|
//
|
4
|
// Miscellania that support the ajaxslt implementation.
|
5
|
//
|
6
|
// Author: Steffen Meschkat <mesch@google.com>
|
7
|
//
|
8
|
|
9
|
function el(i) {
|
10
|
return document.getElementById(i);
|
11
|
}
|
12
|
|
13
|
function px(x) {
|
14
|
return x + 'px';
|
15
|
}
|
16
|
|
17
|
// Split a string s at all occurrences of character c. This is like
|
18
|
// the split() method of the string object, but IE omits empty
|
19
|
// strings, which violates the invariant (s.split(x).join(x) == s).
|
20
|
function stringSplit(s, c) {
|
21
|
var a = s.indexOf(c);
|
22
|
if (a == -1) {
|
23
|
return [ s ];
|
24
|
}
|
25
|
|
26
|
var parts = [];
|
27
|
parts.push(s.substr(0,a));
|
28
|
while (a != -1) {
|
29
|
var a1 = s.indexOf(c, a + 1);
|
30
|
if (a1 != -1) {
|
31
|
parts.push(s.substr(a + 1, a1 - a - 1));
|
32
|
} else {
|
33
|
parts.push(s.substr(a + 1));
|
34
|
}
|
35
|
a = a1;
|
36
|
}
|
37
|
|
38
|
return parts;
|
39
|
}
|
40
|
|
41
|
// Returns the text value if a node; for nodes without children this
|
42
|
// is the nodeValue, for nodes with children this is the concatenation
|
43
|
// of the value of all children.
|
44
|
function xmlValue(node) {
|
45
|
if (!node) {
|
46
|
return '';
|
47
|
}
|
48
|
|
49
|
var ret = '';
|
50
|
if (node.nodeType == DOM_TEXT_NODE ||
|
51
|
node.nodeType == DOM_CDATA_SECTION_NODE ||
|
52
|
node.nodeType == DOM_ATTRIBUTE_NODE) {
|
53
|
ret += node.nodeValue;
|
54
|
|
55
|
} else if (node.nodeType == DOM_ELEMENT_NODE ||
|
56
|
node.nodeType == DOM_DOCUMENT_NODE ||
|
57
|
node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
|
58
|
for (var i = 0; i < node.childNodes.length; ++i) {
|
59
|
ret += arguments.callee(node.childNodes[i]);
|
60
|
}
|
61
|
}
|
62
|
return ret;
|
63
|
}
|
64
|
|
65
|
// Returns the representation of a node as XML text.
|
66
|
function xmlText(node) {
|
67
|
var ret = '';
|
68
|
if (node.nodeType == DOM_TEXT_NODE) {
|
69
|
ret += xmlEscapeText(node.nodeValue);
|
70
|
|
71
|
} else if (node.nodeType == DOM_ELEMENT_NODE) {
|
72
|
ret += '<' + node.nodeName;
|
73
|
for (var i = 0; i < node.attributes.length; ++i) {
|
74
|
var a = node.attributes[i];
|
75
|
if (a && a.nodeName && a.nodeValue) {
|
76
|
ret += ' ' + a.nodeName;
|
77
|
ret += '="' + xmlEscapeAttr(a.nodeValue) + '"';
|
78
|
}
|
79
|
}
|
80
|
|
81
|
if (node.childNodes.length == 0) {
|
82
|
ret += '/>';
|
83
|
|
84
|
} else {
|
85
|
ret += '>';
|
86
|
for (var i = 0; i < node.childNodes.length; ++i) {
|
87
|
ret += arguments.callee(node.childNodes[i]);
|
88
|
}
|
89
|
ret += '</' + node.nodeName + '>';
|
90
|
}
|
91
|
|
92
|
} else if (node.nodeType == DOM_DOCUMENT_NODE ||
|
93
|
node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
|
94
|
for (var i = 0; i < node.childNodes.length; ++i) {
|
95
|
ret += arguments.callee(node.childNodes[i]);
|
96
|
}
|
97
|
}
|
98
|
|
99
|
return ret;
|
100
|
}
|
101
|
|
102
|
// Applies the given function to each element of the array.
|
103
|
function mapExec(array, func) {
|
104
|
for (var i = 0; i < array.length; ++i) {
|
105
|
func(array[i]);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
// Returns an array that contains the return value of the given
|
110
|
// function applied to every element of the input array.
|
111
|
function mapExpr(array, func) {
|
112
|
var ret = [];
|
113
|
for (var i = 0; i < array.length; ++i) {
|
114
|
ret.push(func(array[i]));
|
115
|
}
|
116
|
return ret;
|
117
|
};
|
118
|
|
119
|
// Reverses the given array in place.
|
120
|
function reverseInplace(array) {
|
121
|
for (var i = 0; i < array.length / 2; ++i) {
|
122
|
var h = array[i];
|
123
|
var ii = array.length - i - 1;
|
124
|
array[i] = array[ii];
|
125
|
array[ii] = h;
|
126
|
}
|
127
|
}
|
128
|
|
129
|
// Shallow-copies an array.
|
130
|
function copyArray(dst, src) {
|
131
|
for (var i = 0; i < src.length; ++i) {
|
132
|
dst.push(src[i]);
|
133
|
}
|
134
|
}
|
135
|
|
136
|
function assert(b) {
|
137
|
if (!b) {
|
138
|
throw 'assertion failed';
|
139
|
}
|
140
|
}
|
141
|
|
142
|
// Based on
|
143
|
// <http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247>
|
144
|
var DOM_ELEMENT_NODE = 1;
|
145
|
var DOM_ATTRIBUTE_NODE = 2;
|
146
|
var DOM_TEXT_NODE = 3;
|
147
|
var DOM_CDATA_SECTION_NODE = 4;
|
148
|
var DOM_ENTITY_REFERENCE_NODE = 5;
|
149
|
var DOM_ENTITY_NODE = 6;
|
150
|
var DOM_PROCESSING_INSTRUCTION_NODE = 7;
|
151
|
var DOM_COMMENT_NODE = 8;
|
152
|
var DOM_DOCUMENT_NODE = 9;
|
153
|
var DOM_DOCUMENT_TYPE_NODE = 10;
|
154
|
var DOM_DOCUMENT_FRAGMENT_NODE = 11;
|
155
|
var DOM_NOTATION_NODE = 12;
|
156
|
|
157
|
|
158
|
var xpathdebug = false; // trace xpath parsing
|
159
|
var xsltdebug = false; // trace xslt processing
|
160
|
|
161
|
|
162
|
// Escape XML special markup chracters: tag delimiter < > and entity
|
163
|
// reference start delimiter &. The escaped string can be used in XML
|
164
|
// text portions (i.e. between tags).
|
165
|
function xmlEscapeText(s) {
|
166
|
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
167
|
}
|
168
|
|
169
|
// Escape XML special markup characters: tag delimiter < > entity
|
170
|
// reference start delimiter & and quotes ". The escaped string can be
|
171
|
// used in double quoted XML attribute value portions (i.e. in
|
172
|
// attributes within start tags).
|
173
|
function xmlEscapeAttr(s) {
|
174
|
return xmlEscapeText(s).replace(/\"/g, '"');
|
175
|
}
|
176
|
|
177
|
// Escape markup in XML text, but don't touch entity references. The
|
178
|
// escaped string can be used as XML text (i.e. between tags).
|
179
|
function xmlEscapeTags(s) {
|
180
|
return s.replace(/</g, '<').replace(/>/g, '>');
|
181
|
}
|
182
|
|
183
|
// An implementation of the debug log.
|
184
|
|
185
|
var logging__ = false;
|
186
|
|
187
|
function Log() {};
|
188
|
|
189
|
Log.lines = [];
|
190
|
|
191
|
Log.write = function(s) {
|
192
|
if (logging__) {
|
193
|
this.lines.push(xmlEscapeText(s));
|
194
|
this.show();
|
195
|
}
|
196
|
};
|
197
|
|
198
|
// Writes the given XML with every tag on a new line.
|
199
|
Log.writeXML = function(xml) {
|
200
|
if (logging__) {
|
201
|
var s0 = xml.replace(/</g, '\n<');
|
202
|
var s1 = xmlEscapeText(s0);
|
203
|
var s2 = s1.replace(/\s*\n(\s|\n)*/g, '<br/>');
|
204
|
this.lines.push(s2);
|
205
|
this.show();
|
206
|
}
|
207
|
}
|
208
|
|
209
|
// Writes without any escaping
|
210
|
Log.writeRaw = function(s) {
|
211
|
if (logging__) {
|
212
|
this.lines.push(s);
|
213
|
this.show();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
Log.clear = function() {
|
218
|
if (logging__) {
|
219
|
var l = this.div();
|
220
|
l.innerHTML = '';
|
221
|
this.lines = [];
|
222
|
}
|
223
|
}
|
224
|
|
225
|
Log.show = function() {
|
226
|
var l = this.div();
|
227
|
l.innerHTML += this.lines.join('<br/>') + '<br/>';
|
228
|
this.lines = [];
|
229
|
l.scrollTop = l.scrollHeight;
|
230
|
}
|
231
|
|
232
|
Log.div = function() {
|
233
|
var l = document.getElementById('log');
|
234
|
if (!l) {
|
235
|
l = document.createElement('div');
|
236
|
l.id = 'log';
|
237
|
l.style.position = 'absolute';
|
238
|
l.style.right = '5px';
|
239
|
l.style.top = '5px';
|
240
|
l.style.width = '250px';
|
241
|
l.style.height = '150px';
|
242
|
l.style.overflow = 'auto';
|
243
|
l.style.backgroundColor = '#f0f0f0';
|
244
|
l.style.border = '1px solid gray';
|
245
|
l.style.fontSize = '10px';
|
246
|
l.style.padding = '5px';
|
247
|
document.body.appendChild(l);
|
248
|
}
|
249
|
return l;
|
250
|
}
|
251
|
|
252
|
|
253
|
function Timer() {}
|
254
|
Timer.start = function() {}
|
255
|
Timer.end = function() {}
|