Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.html.style");
14
dojo.require("dojo.html.common");
15
dojo.require("dojo.uri.Uri");
16
dojo.html.getClass = function (node) {
17
	node = dojo.byId(node);
18
	if (!node) {
19
		return "";
20
	}
21
	var cs = "";
22
	if (node.className) {
23
		cs = node.className;
24
	} else {
25
		if (dojo.html.hasAttribute(node, "class")) {
26
			cs = dojo.html.getAttribute(node, "class");
27
		}
28
	}
29
	return cs.replace(/^\s+|\s+$/g, "");
30
};
31
dojo.html.getClasses = function (node) {
32
	var c = dojo.html.getClass(node);
33
	return (c == "") ? [] : c.split(/\s+/g);
34
};
35
dojo.html.hasClass = function (node, classname) {
36
	return (new RegExp("(^|\\s+)" + classname + "(\\s+|$)")).test(dojo.html.getClass(node));
37
};
38
dojo.html.prependClass = function (node, classStr) {
39
	classStr += " " + dojo.html.getClass(node);
40
	return dojo.html.setClass(node, classStr);
41
};
42
dojo.html.addClass = function (node, classStr) {
43
	if (dojo.html.hasClass(node, classStr)) {
44
		return false;
45
	}
46
	classStr = (dojo.html.getClass(node) + " " + classStr).replace(/^\s+|\s+$/g, "");
47
	return dojo.html.setClass(node, classStr);
48
};
49
dojo.html.setClass = function (node, classStr) {
50
	node = dojo.byId(node);
51
	var cs = new String(classStr);
52
	try {
53
		if (typeof node.className == "string") {
54
			node.className = cs;
55
		} else {
56
			if (node.setAttribute) {
57
				node.setAttribute("class", classStr);
58
				node.className = cs;
59
			} else {
60
				return false;
61
			}
62
		}
63
	}
64
	catch (e) {
65
		dojo.debug("dojo.html.setClass() failed", e);
66
	}
67
	return true;
68
};
69
dojo.html.removeClass = function (node, classStr, allowPartialMatches) {
70
	try {
71
		if (!allowPartialMatches) {
72
			var newcs = dojo.html.getClass(node).replace(new RegExp("(^|\\s+)" + classStr + "(\\s+|$)"), "$1$2");
73
		} else {
74
			var newcs = dojo.html.getClass(node).replace(classStr, "");
75
		}
76
		dojo.html.setClass(node, newcs);
77
	}
78
	catch (e) {
79
		dojo.debug("dojo.html.removeClass() failed", e);
80
	}
81
	return true;
82
};
83
dojo.html.replaceClass = function (node, newClass, oldClass) {
84
	dojo.html.removeClass(node, oldClass);
85
	dojo.html.addClass(node, newClass);
86
};
87
dojo.html.classMatchType = {ContainsAll:0, ContainsAny:1, IsOnly:2};
88
dojo.html.getElementsByClass = function (classStr, parent, nodeType, classMatchType, useNonXpath) {
89
	useNonXpath = false;
90
	var _document = dojo.doc();
91
	parent = dojo.byId(parent) || _document;
92
	var classes = classStr.split(/\s+/g);
93
	var nodes = [];
94
	if (classMatchType != 1 && classMatchType != 2) {
95
		classMatchType = 0;
96
	}
97
	var reClass = new RegExp("(\\s|^)((" + classes.join(")|(") + "))(\\s|$)");
98
	var srtLength = classes.join(" ").length;
99
	var candidateNodes = [];
100
	if (!useNonXpath && _document.evaluate) {
101
		var xpath = ".//" + (nodeType || "*") + "[contains(";
102
		if (classMatchType != dojo.html.classMatchType.ContainsAny) {
103
			xpath += "concat(' ',@class,' '), ' " + classes.join(" ') and contains(concat(' ',@class,' '), ' ") + " ')";
104
			if (classMatchType == 2) {
105
				xpath += " and string-length(@class)=" + srtLength + "]";
106
			} else {
107
				xpath += "]";
108
			}
109
		} else {
110
			xpath += "concat(' ',@class,' '), ' " + classes.join(" ') or contains(concat(' ',@class,' '), ' ") + " ')]";
111
		}
112
		var xpathResult = _document.evaluate(xpath, parent, null, XPathResult.ANY_TYPE, null);
113
		var result = xpathResult.iterateNext();
114
		while (result) {
115
			try {
116
				candidateNodes.push(result);
117
				result = xpathResult.iterateNext();
118
			}
119
			catch (e) {
120
				break;
121
			}
122
		}
123
		return candidateNodes;
124
	} else {
125
		if (!nodeType) {
126
			nodeType = "*";
127
		}
128
		candidateNodes = parent.getElementsByTagName(nodeType);
129
		var node, i = 0;
130
	outer:
131
		while (node = candidateNodes[i++]) {
132
			var nodeClasses = dojo.html.getClasses(node);
133
			if (nodeClasses.length == 0) {
134
				continue outer;
135
			}
136
			var matches = 0;
137
			for (var j = 0; j < nodeClasses.length; j++) {
138
				if (reClass.test(nodeClasses[j])) {
139
					if (classMatchType == dojo.html.classMatchType.ContainsAny) {
140
						nodes.push(node);
141
						continue outer;
142
					} else {
143
						matches++;
144
					}
145
				} else {
146
					if (classMatchType == dojo.html.classMatchType.IsOnly) {
147
						continue outer;
148
					}
149
				}
150
			}
151
			if (matches == classes.length) {
152
				if ((classMatchType == dojo.html.classMatchType.IsOnly) && (matches == nodeClasses.length)) {
153
					nodes.push(node);
154
				} else {
155
					if (classMatchType == dojo.html.classMatchType.ContainsAll) {
156
						nodes.push(node);
157
					}
158
				}
159
			}
160
		}
161
		return nodes;
162
	}
163
};
164
dojo.html.getElementsByClassName = dojo.html.getElementsByClass;
165
dojo.html.toCamelCase = function (selector) {
166
	var arr = selector.split("-"), cc = arr[0];
167
	for (var i = 1; i < arr.length; i++) {
168
		cc += arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
169
	}
170
	return cc;
171
};
172
dojo.html.toSelectorCase = function (selector) {
173
	return selector.replace(/([A-Z])/g, "-$1").toLowerCase();
174
};
175
if (dojo.render.html.ie) {
176
	dojo.html.getComputedStyle = function (node, property, value) {
177
		node = dojo.byId(node);
1422 alexandre_ 178
		if (!node || !node.currentStyle) {
1318 alexandre_ 179
			return value;
180
		}
181
		return node.currentStyle[dojo.html.toCamelCase(property)];
182
	};
183
	dojo.html.getComputedStyles = function (node) {
184
		return node.currentStyle;
185
	};
186
} else {
187
	dojo.html.getComputedStyle = function (node, property, value) {
188
		node = dojo.byId(node);
189
		if (!node || !node.style) {
190
			return value;
191
		}
192
		var s = document.defaultView.getComputedStyle(node, null);
193
		return (s && s[dojo.html.toCamelCase(property)]) || "";
194
	};
195
	dojo.html.getComputedStyles = function (node) {
196
		return document.defaultView.getComputedStyle(node, null);
197
	};
198
}
199
dojo.html.getStyleProperty = function (node, cssSelector) {
200
	node = dojo.byId(node);
201
	return (node && node.style ? node.style[dojo.html.toCamelCase(cssSelector)] : undefined);
202
};
203
dojo.html.getStyle = function (node, cssSelector) {
204
	var value = dojo.html.getStyleProperty(node, cssSelector);
205
	return (value ? value : dojo.html.getComputedStyle(node, cssSelector));
206
};
207
dojo.html.setStyle = function (node, cssSelector, value) {
208
	node = dojo.byId(node);
209
	if (node && node.style) {
210
		var camelCased = dojo.html.toCamelCase(cssSelector);
211
		node.style[camelCased] = value;
212
	}
213
};
214
dojo.html.setStyleText = function (target, text) {
215
	try {
216
		target.style.cssText = text;
217
	}
218
	catch (e) {
219
		target.setAttribute("style", text);
220
	}
221
};
222
dojo.html.copyStyle = function (target, source) {
223
	if (!source.style.cssText) {
224
		target.setAttribute("style", source.getAttribute("style"));
225
	} else {
226
		target.style.cssText = source.style.cssText;
227
	}
228
	dojo.html.addClass(target, dojo.html.getClass(source));
229
};
230
dojo.html.getUnitValue = function (node, cssSelector, autoIsZero) {
231
	var s = dojo.html.getComputedStyle(node, cssSelector);
232
	if ((!s) || ((s == "auto") && (autoIsZero))) {
233
		return {value:0, units:"px"};
234
	}
235
	var match = s.match(/(\-?[\d.]+)([a-z%]*)/i);
236
	if (!match) {
237
		return dojo.html.getUnitValue.bad;
238
	}
239
	return {value:Number(match[1]), units:match[2].toLowerCase()};
240
};
241
dojo.html.getUnitValue.bad = {value:NaN, units:""};
242
if (dojo.render.html.ie) {
243
	dojo.html.toPixelValue = function (element, styleValue) {
244
		if (!styleValue) {
245
			return 0;
246
		}
247
		if (styleValue.slice(-2) == "px") {
248
			return parseFloat(styleValue);
249
		}
250
		var pixelValue = 0;
251
		with (element) {
252
			var sLeft = style.left;
253
			var rsLeft = runtimeStyle.left;
254
			runtimeStyle.left = currentStyle.left;
255
			try {
256
				style.left = styleValue || 0;
257
				pixelValue = style.pixelLeft;
258
				style.left = sLeft;
259
				runtimeStyle.left = rsLeft;
260
			}
261
			catch (e) {
262
			}
263
		}
264
		return pixelValue;
265
	};
266
} else {
267
	dojo.html.toPixelValue = function (element, styleValue) {
268
		return (styleValue && (styleValue.slice(-2) == "px") ? parseFloat(styleValue) : 0);
269
	};
270
}
271
dojo.html.getPixelValue = function (node, styleProperty, autoIsZero) {
272
	return dojo.html.toPixelValue(node, dojo.html.getComputedStyle(node, styleProperty));
273
};
274
dojo.html.setPositivePixelValue = function (node, selector, value) {
275
	if (isNaN(value)) {
276
		return false;
277
	}
278
	node.style[selector] = Math.max(0, value) + "px";
279
	return true;
280
};
281
dojo.html.styleSheet = null;
282
dojo.html.insertCssRule = function (selector, declaration, index) {
283
	if (!dojo.html.styleSheet) {
284
		if (document.createStyleSheet) {
285
			dojo.html.styleSheet = document.createStyleSheet();
286
		} else {
287
			if (document.styleSheets[0]) {
288
				dojo.html.styleSheet = document.styleSheets[0];
289
			} else {
290
				return null;
291
			}
292
		}
293
	}
294
	if (arguments.length < 3) {
295
		if (dojo.html.styleSheet.cssRules) {
296
			index = dojo.html.styleSheet.cssRules.length;
297
		} else {
298
			if (dojo.html.styleSheet.rules) {
299
				index = dojo.html.styleSheet.rules.length;
300
			} else {
301
				return null;
302
			}
303
		}
304
	}
305
	if (dojo.html.styleSheet.insertRule) {
306
		var rule = selector + " { " + declaration + " }";
307
		return dojo.html.styleSheet.insertRule(rule, index);
308
	} else {
309
		if (dojo.html.styleSheet.addRule) {
310
			return dojo.html.styleSheet.addRule(selector, declaration, index);
311
		} else {
312
			return null;
313
		}
314
	}
315
};
316
dojo.html.removeCssRule = function (index) {
317
	if (!dojo.html.styleSheet) {
318
		dojo.debug("no stylesheet defined for removing rules");
319
		return false;
320
	}
321
	if (dojo.render.html.ie) {
322
		if (!index) {
323
			index = dojo.html.styleSheet.rules.length;
324
			dojo.html.styleSheet.removeRule(index);
325
		}
326
	} else {
327
		if (document.styleSheets[0]) {
328
			if (!index) {
329
				index = dojo.html.styleSheet.cssRules.length;
330
			}
331
			dojo.html.styleSheet.deleteRule(index);
332
		}
333
	}
334
	return true;
335
};
336
dojo.html._insertedCssFiles = [];
337
dojo.html.insertCssFile = function (URI, doc, checkDuplicates, fail_ok) {
338
	if (!URI) {
339
		return;
340
	}
341
	if (!doc) {
342
		doc = document;
343
	}
344
	var cssStr = dojo.hostenv.getText(URI, false, fail_ok);
345
	if (cssStr === null) {
346
		return;
347
	}
348
	cssStr = dojo.html.fixPathsInCssText(cssStr, URI);
349
	if (checkDuplicates) {
350
		var idx = -1, node, ent = dojo.html._insertedCssFiles;
351
		for (var i = 0; i < ent.length; i++) {
352
			if ((ent[i].doc == doc) && (ent[i].cssText == cssStr)) {
353
				idx = i;
354
				node = ent[i].nodeRef;
355
				break;
356
			}
357
		}
358
		if (node) {
359
			var styles = doc.getElementsByTagName("style");
360
			for (var i = 0; i < styles.length; i++) {
361
				if (styles[i] == node) {
362
					return;
363
				}
364
			}
365
			dojo.html._insertedCssFiles.shift(idx, 1);
366
		}
367
	}
368
	var style = dojo.html.insertCssText(cssStr, doc);
369
	dojo.html._insertedCssFiles.push({"doc":doc, "cssText":cssStr, "nodeRef":style});
370
	if (style && djConfig.isDebug) {
371
		style.setAttribute("dbgHref", URI);
372
	}
373
	return style;
374
};
375
dojo.html.insertCssText = function (cssStr, doc, URI) {
376
	if (!cssStr) {
377
		return;
378
	}
379
	if (!doc) {
380
		doc = document;
381
	}
382
	if (URI) {
383
		cssStr = dojo.html.fixPathsInCssText(cssStr, URI);
384
	}
385
	var style = doc.createElement("style");
386
	style.setAttribute("type", "text/css");
387
	var head = doc.getElementsByTagName("head")[0];
388
	if (!head) {
389
		dojo.debug("No head tag in document, aborting styles");
390
		return;
391
	} else {
392
		head.appendChild(style);
393
	}
394
	if (style.styleSheet) {
395
		var setFunc = function () {
396
			try {
397
				style.styleSheet.cssText = cssStr;
398
			}
399
			catch (e) {
400
				dojo.debug(e);
401
			}
402
		};
403
		if (style.styleSheet.disabled) {
404
			setTimeout(setFunc, 10);
405
		} else {
406
			setFunc();
407
		}
408
	} else {
409
		var cssText = doc.createTextNode(cssStr);
410
		style.appendChild(cssText);
411
	}
412
	return style;
413
};
414
dojo.html.fixPathsInCssText = function (cssStr, URI) {
415
	if (!cssStr || !URI) {
416
		return;
417
	}
418
	var match, str = "", url = "", urlChrs = "[\\t\\s\\w\\(\\)\\/\\.\\\\'\"-:#=&?~]+";
419
	var regex = new RegExp("url\\(\\s*(" + urlChrs + ")\\s*\\)");
420
	var regexProtocol = /(file|https?|ftps?):\/\//;
421
	regexTrim = new RegExp("^[\\s]*(['\"]?)(" + urlChrs + ")\\1[\\s]*?$");
422
	if (dojo.render.html.ie55 || dojo.render.html.ie60) {
423
		var regexIe = new RegExp("AlphaImageLoader\\((.*)src=['\"](" + urlChrs + ")['\"]");
424
		while (match = regexIe.exec(cssStr)) {
425
			url = match[2].replace(regexTrim, "$2");
426
			if (!regexProtocol.exec(url)) {
427
				url = (new dojo.uri.Uri(URI, url).toString());
428
			}
429
			str += cssStr.substring(0, match.index) + "AlphaImageLoader(" + match[1] + "src='" + url + "'";
430
			cssStr = cssStr.substr(match.index + match[0].length);
431
		}
432
		cssStr = str + cssStr;
433
		str = "";
434
	}
435
	while (match = regex.exec(cssStr)) {
436
		url = match[1].replace(regexTrim, "$2");
437
		if (!regexProtocol.exec(url)) {
438
			url = (new dojo.uri.Uri(URI, url).toString());
439
		}
440
		str += cssStr.substring(0, match.index) + "url(" + url + ")";
441
		cssStr = cssStr.substr(match.index + match[0].length);
442
	}
443
	return str + cssStr;
444
};
445
dojo.html.setActiveStyleSheet = function (title) {
446
	var i = 0, a, els = dojo.doc().getElementsByTagName("link");
447
	while (a = els[i++]) {
448
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
449
			a.disabled = true;
450
			if (a.getAttribute("title") == title) {
451
				a.disabled = false;
452
			}
453
		}
454
	}
455
};
456
dojo.html.getActiveStyleSheet = function () {
457
	var i = 0, a, els = dojo.doc().getElementsByTagName("link");
458
	while (a = els[i++]) {
459
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
460
			return a.getAttribute("title");
461
		}
462
	}
463
	return null;
464
};
465
dojo.html.getPreferredStyleSheet = function () {
466
	var i = 0, a, els = dojo.doc().getElementsByTagName("link");
467
	while (a = els[i++]) {
468
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) {
469
			return a.getAttribute("title");
470
		}
471
	}
472
	return null;
473
};
474
dojo.html.applyBrowserClass = function (node) {
475
	var drh = dojo.render.html;
476
	var classes = {dj_ie:drh.ie, dj_ie55:drh.ie55, dj_ie6:drh.ie60, dj_ie7:drh.ie70, dj_iequirks:drh.ie && drh.quirks, dj_opera:drh.opera, dj_opera8:drh.opera && (Math.floor(dojo.render.version) == 8), dj_opera9:drh.opera && (Math.floor(dojo.render.version) == 9), dj_khtml:drh.khtml, dj_safari:drh.safari, dj_gecko:drh.mozilla};
477
	for (var p in classes) {
478
		if (classes[p]) {
479
			dojo.html.addClass(node, p);
480
		}
481
	}
482
};
483