1 /**
  2 * YCss utilities Not instanciable.
  3 * @constructor
  4 */
  5 var YCss={
  6 	prefix_obj:[],
  7 	prefix_str:[],
  8 	cleanedStyle:function(target){
  9 		var tmpobj=YDom.get(target);	var rst={};
 10 		if(tmpobj&&tmpobj.style){
 11 			for(var i=0;i<tmpobj.style.length;i++){
 12 				rst[tmpobj.style[i]] = tmpobj.style[tmpobj.style[i]];	}
 13 		}return rst;
 14 	},
 15 	merge:function(target,source){
 16 		var gso = function(stylobj){
 17 			var robj = {
 18 				obj		: typeof(stylobj)=="object" ? stylobj : YDom.get(stylobj),
 19 				isdom	: YDom.get(stylobj)?true:false
 20 			};
 21 			if(robj.obj){
 22 				if(robj.isdom)		robj.obj						= YCss.cleanedStyle(robj.obj);
 23 				else if(typeof(robj.obj.style)=="object")robj.obj	= robj.obj.style;
 24 				return robj;
 25 			}else return null;
 26 		};
 27 		var tgt = gso(target);
 28 		var src = gso(source);
 29 		if(tgt.obj&&src.obj){
 30 			for(var i in src.obj){
 31 				if(tgt.isdom){
 32 					 YDom.get(target).style[i]=src.obj[i];
 33 				}else{
 34 					tgt.obj[i]=src.obj[i];
 35 				}
 36 			}
 37 		}
 38 	},
 39 	/** get all css rules as text.
 40 	* @param {function} Function function invoked when all css text collected (of type Function(string:csstext,object:datas) ).
 41 	* @param {object} datas .
 42 	*/
 43 	collectCssText:function(Function,datas){
 44 		var slinks = YDom.geElements(['link'],{rel:"stylesheet"});
 45 		var urls = [];
 46 		for(var i in slinks)urls.push(slinks[i].href);
 47 		new YAsync.Loader('text',function(strl,ldr,dat){
 48 			var clist = YDom.collectionToList(document.getElementsByTagName('style'));
 49 			var ctx = strl.join("\n");
 50 			for(var i in clist){ctx+="\n"+clist[i].innerHTML;}
 51 			dat.Function(ctx,dat.datas);
 52 		},function(){},function(){},{datas:datas,Function:Function}).loadList(urls);
 53 	},
 54 	objToStr:function(style){// must be cleaned if real style
 55 		var rstr = "";
 56 		for(var i in style){
 57 			rstr+= YStr.replace(YCss.prefix_obj,YCss.prefix_str,i)+":"+tyle[i]+";";
 58 		}return rstr;
 59 	},
 60 	strToObj:function(string){
 61 		var robj={};
 62 		var starr = YStr.replace(["	","\r","\n"],["","",""],string).split(";");
 63 		for(var i=0;i<starr.length;i++){
 64 			if(starr[i].indexOf(":")){
 65 				var parr = starr[i].split(":");
 66 				var nam = YStr.replace(YCss.prefix_str,YCss.prefix_obj,parr[0]);
 67 				nam = YStr.replace([" "],[""],nam);
 68 				robj[nam] = parr[1];
 69 			}
 70 		}return robj;
 71 	},
 72 	getLeftTop:function(node){
 73 		var tmpobj=YDom.get(node);
 74 		return tmpobj?[parseInt(tmpobj.style.left),parseInt(tmpobj.style.top)]:[0,0];
 75 	},
 76 	setLeftTop:function(node,i2d){
 77 		var tmpobj=YDom.get(node);
 78 		tmpobj.style.left = i2d[0]+"px";
 79 		tmpobj.style.top = i2d[1]+"px";
 80 	},
 81 	setWidthHeight:function(node,i2d){
 82 		var tmpobj=YDom.get(node);
 83 		tmpobj.style.width = i2d[0]+"px";
 84 		tmpobj.style.height = i2d[1]+"px";
 85 	},
 86 	classes:{
 87 		getLocation :function(className){
 88 			for(var i=0;i<document.styleSheets.length;i++){
 89 				var pointer = document.styleSheets[i].cssRules ? "cssRules" : "rules";
 90 				for(var j=0;j<document.styleSheets[i][pointer].length;j++){
 91 					var cunit = document.styleSheets[i][pointer][j];
 92 					if(cunit.selectorText==className)	return [i,j];
 93 				}
 94 			}return null;
 95 		},
 96 		getAt:function(styleSheetIndex,ruleIndex){
 97 			var pointer = document.styleSheets[styleSheetIndex].cssRules ? "cssRules" : "rules";
 98 			return document.styleSheets[styleSheetIndex][pointer][ruleIndex];
 99 		},
100 		getList:function(){
101 			var rar = {};
102 			for(var i=0;i<document.styleSheets.length;i++){
103 				var pointer = document.styleSheets[i].cssRules ? "cssRules" : "rules";
104 				for(var j=0;j<document.styleSheets[i][pointer].length;j++){
105 					var cunit = document.styleSheets[i][pointer][j];
106 					if(cunit.selectorText)	rar[cunit.selectorText]=cunit;
107 				}
108 			}
109 			return rar;
110 		},
111 		get:function(className){
112 			var loc = YCss.classes.getLocation(className);
113 			if(!loc)return null;
114 			return YCss.classes.getAt(loc[0],loc[1]);
115 		},
116 		set:function(className,datas){
117 			if(document.styleSheets.length==0)document.body.appendChild(YDom.create("STYLE",{},""));
118 			YCss.classes.remove(className);
119 			var dobj = typeof(datas)=="object" ? YCss.objToStr(datas) : datas ;
120 			if(document.styleSheets[0].cssRules)
121 				document.styleSheets[0].insertRule(className+"{"+dobj+"}", document.styleSheets[0].cssRules.length);
122 			else	document.styleSheets[0].addRule(className,dobj);
123 		},
124 		remove:function(className){
125 			var loc = YCss.classes.getLocation();
126 			if(!loc)return null;
127 			var pointer = document.styleSheets[styleSheetIndex].cssRules ? "deleteRule" : "removeRule";
128 			document.styleSheets[loc[0]][pointer](loc[1]);
129 			return true;
130 		}
131 	},
132 	init:function(){
133 		YCss.prefix_obj=[];YCss.prefix_str=[];
134 		for(var i=0;i<26;i++){
135 			YCss.prefix_obj.push(String.fromCharCode(i+65)); 
136 			YCss.prefix_str.push("-"+String.fromCharCode(i+97)); 
137 		}
138 	}
139 };
140 YCss.init();
141