Subversion Repositories Applications.papyrus

Rev

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