4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
15 * Utility class for manipulating CSS rules
18 Roo.util.CSS = function(){
22 var camelRe = /(-[a-z])/gi;
23 var camelFn = function(m, a){ return a.charAt(1).toUpperCase(); };
27 * Very simple dynamic creation of stylesheets from a text blob of rules. The text will wrapped in a style
28 * tag and appended to the HEAD of the document.
29 * @param {String} cssText The text containing the css rules
30 * @param {String} id An id to add to the stylesheet for later removal
31 * @return {StyleSheet}
33 createStyleSheet : function(cssText, id){
35 var head = doc.getElementsByTagName("head")[0];
36 var rules = doc.createElement("style");
37 rules.setAttribute("type", "text/css");
39 rules.setAttribute("id", id);
42 head.appendChild(rules);
43 ss = rules.styleSheet;
47 rules.appendChild(doc.createTextNode(cssText));
49 rules.cssText = cssText;
51 head.appendChild(rules);
52 ss = rules.styleSheet ? rules.styleSheet : (rules.sheet || doc.styleSheets[doc.styleSheets.length-1]);
54 this.cacheStyleSheet(ss);
59 * Removes a style or link tag by id
60 * @param {String} id The id of the tag
62 removeStyleSheet : function(id){
63 var existing = doc.getElementById(id);
65 existing.parentNode.removeChild(existing);
70 * Dynamically swaps an existing stylesheet reference for a new one
71 * @param {String} id The id of an existing link tag to remove
72 * @param {String} url The href of the new stylesheet to include
74 swapStyleSheet : function(id, url){
75 this.removeStyleSheet(id);
76 var ss = doc.createElement("link");
77 ss.setAttribute("rel", "stylesheet");
78 ss.setAttribute("type", "text/css");
79 ss.setAttribute("id", id);
80 ss.setAttribute("href", url);
81 doc.getElementsByTagName("head")[0].appendChild(ss);
85 * Refresh the rule cache if you have dynamically added stylesheets
86 * @return {Object} An object (hash) of rules indexed by selector
88 refreshCache : function(){
89 return this.getRules(true);
93 cacheStyleSheet : function(ss){
97 try{// try catch for cross domain access issue
98 var ssRules = ss.cssRules || ss.rules;
99 for(var j = ssRules.length-1; j >= 0; --j){
100 rules[ssRules[j].selectorText] = ssRules[j];
106 * Gets all css rules for the document
107 * @param {Boolean} refreshCache true to refresh the internal cache
108 * @return {Object} An object (hash) of rules indexed by selector
110 getRules : function(refreshCache){
111 if(rules == null || refreshCache){
113 var ds = doc.styleSheets;
114 for(var i =0, len = ds.length; i < len; i++){
116 this.cacheStyleSheet(ds[i]);
124 * Gets an an individual CSS rule by selector(s)
125 * @param {String/Array} selector The CSS selector or an array of selectors to try. The first selector that is found is returned.
126 * @param {Boolean} refreshCache true to refresh the internal cache if you have recently updated any rules or added styles dynamically
127 * @return {CSSRule} The CSS rule or null if one is not found
129 getRule : function(selector, refreshCache){
130 var rs = this.getRules(refreshCache);
131 if(!(selector instanceof Array)){
134 for(var i = 0; i < selector.length; i++){
136 return rs[selector[i]];
144 * Updates a rule property
145 * @param {String/Array} selector If it's an array it tries each selector until it finds one. Stops immediately once one is found.
146 * @param {String} property The css property
147 * @param {String} value The new value for the property
148 * @return {Boolean} true If a rule was found and updated
150 updateRule : function(selector, property, value){
151 if(!(selector instanceof Array)){
152 var rule = this.getRule(selector);
154 rule.style[property.replace(camelRe, camelFn)] = value;
158 for(var i = 0; i < selector.length; i++){
159 if(this.updateRule(selector[i], property, value)){