summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/data/css.js
blob: 790ad965897b90d8df6d6816b18204e434bae5c9 (plain)
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
//>>built
define("dojox/data/css", ["dojo/_base/lang", "dojo/_base/array"], 
  function(lang, array) {

var css = lang.getObject("dojox.data.css",true) 

css.rules = {};

css.rules.forEach = function(fn,ctx,context){
	if(context){
		var _processSS = function(styleSheet){
			//iterate across rules in the stylesheet
			array.forEach(styleSheet[styleSheet.cssRules?"cssRules":"rules"], function(rule){
				if(!rule.type || rule.type !== 3){// apply fn to current rule with approp ctx. rule is arg (all browsers)
					var href = "";
					if(styleSheet && styleSheet.href){
						href = styleSheet.href;
					}
					fn.call(ctx?ctx:this,rule, styleSheet, href);
				}
			});
			//process any child stylesheets
		};
		array.forEach(context,_processSS);
	}
};

css.findStyleSheets = function(sheets){
	// Takes an array of stylesheet paths and finds the currently loaded StyleSheet objects matching
	// those names
	var sheetObjects = [];
	var _processSS = function(styleSheet){
		var s = css.findStyleSheet(styleSheet);
		if(s){
			array.forEach(s, function(sheet){
				if(array.indexOf(sheetObjects, sheet) === -1){
					sheetObjects.push(sheet);
				}
			});
		}
	};
	array.forEach(sheets, _processSS);
	return sheetObjects;
};

css.findStyleSheet = function(sheet){
	// Takes a stylesheet path and finds the currently loaded StyleSheet objects matching
	// those names (and it's parent(s), if it is imported from another)
	var sheetObjects = [];
	if(sheet.charAt(0) === '.'){
		sheet = sheet.substring(1);
	}
	var _processSS = function(styleSheet){
		if(styleSheet.href && styleSheet.href.match(sheet)){
			sheetObjects.push(styleSheet);
			return true;
		}
		if(styleSheet.imports){
			return array.some(styleSheet.imports, function(importedSS){ //IE stylesheet has imports[] containing @import'ed rules
				//console.debug("Processing IE @import rule",importedSS);
				return _processSS(importedSS);
			});
		}
		//iterate across rules in the stylesheet
		return array.some(styleSheet[styleSheet.cssRules?"cssRules":"rules"], function(rule){
			if(rule.type && rule.type === 3 && _processSS(rule.styleSheet)){// CSSImportRule (firefox)
				//sheetObjects.push(styleSheet);
				return true;
			}
			return false;
		});
	};
	array.some(document.styleSheets, _processSS);
	return sheetObjects;
};

css.determineContext = function(initialStylesheets){
	// Takes an array of stylesheet paths and returns an array of all stylesheets that fall in the
	// given context.  If no paths are given, all stylesheets are returned.
	var ret = [];
	if(initialStylesheets && initialStylesheets.length > 0){
		initialStylesheets = css.findStyleSheets(initialStylesheets);
	}else{
		initialStylesheets = document.styleSheets;
	}
	var _processSS = function(styleSheet){
		ret.push(styleSheet);
		if(styleSheet.imports){
			array.forEach(styleSheet.imports, function(importedSS){ //IE stylesheet has imports[] containing @import'ed rules
				//console.debug("Processing IE @import rule",importedSS);
				_processSS(importedSS);
			});
		}
		//iterate across rules in the stylesheet
		array.forEach(styleSheet[styleSheet.cssRules?"cssRules":"rules"], function(rule){
			if(rule.type && rule.type === 3){// CSSImportRule (firefox)
				_processSS(rule.styleSheet);
			}
		});
	};
	array.forEach(initialStylesheets,_processSS);
	return ret;
};

return css;

});