4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-CSS'>/**
19 </span> * @class Ext.util.CSS
20 * Utility class for manipulating CSS rules
23 Ext.define('Ext.util.CSS', function() {
27 var camelRe = /(-[a-z])/gi;
28 var camelFn = function(m, a){ return a.charAt(1).toUpperCase(); };
34 constructor: function() {
36 this.initialized = false;
39 <span id='Ext-util-CSS-method-createStyleSheet'> /**
40 </span> * Creates a stylesheet from a text blob of rules.
41 * These rules will be wrapped in a STYLE tag and appended to the HEAD of the document.
42 * @param {String} cssText The text containing the css rules
43 * @param {String} id An id to add to the stylesheet for later removal
44 * @return {StyleSheet}
46 createStyleSheet : function(cssText, id) {
48 head = doc.getElementsByTagName("head")[0],
49 styleEl = doc.createElement("style");
51 styleEl.setAttribute("type", "text/css");
53 styleEl.setAttribute("id", id);
57 head.appendChild(styleEl);
58 ss = styleEl.styleSheet;
62 styleEl.appendChild(doc.createTextNode(cssText));
64 styleEl.cssText = cssText;
66 head.appendChild(styleEl);
67 ss = styleEl.styleSheet ? styleEl.styleSheet : (styleEl.sheet || doc.styleSheets[doc.styleSheets.length-1]);
69 this.cacheStyleSheet(ss);
73 <span id='Ext-util-CSS-method-removeStyleSheet'> /**
74 </span> * Removes a style or link tag by id
75 * @param {String} id The id of the tag
77 removeStyleSheet : function(id) {
78 var existing = document.getElementById(id);
80 existing.parentNode.removeChild(existing);
84 <span id='Ext-util-CSS-method-swapStyleSheet'> /**
85 </span> * Dynamically swaps an existing stylesheet reference for a new one
86 * @param {String} id The id of an existing link tag to remove
87 * @param {String} url The href of the new stylesheet to include
89 swapStyleSheet : function(id, url) {
91 this.removeStyleSheet(id);
92 var ss = doc.createElement("link");
93 ss.setAttribute("rel", "stylesheet");
94 ss.setAttribute("type", "text/css");
95 ss.setAttribute("id", id);
96 ss.setAttribute("href", url);
97 doc.getElementsByTagName("head")[0].appendChild(ss);
100 <span id='Ext-util-CSS-method-refreshCache'> /**
101 </span> * Refresh the rule cache if you have dynamically added stylesheets
102 * @return {Object} An object (hash) of rules indexed by selector
104 refreshCache : function() {
105 return this.getRules(true);
109 cacheStyleSheet : function(ss) {
113 try {// try catch for cross domain access issue
114 var ssRules = ss.cssRules || ss.rules,
116 i = ssRules.length - 1,
120 for (; i >= 0; --i) {
121 selectorText = ssRules[i].selectorText;
124 // Split in case there are multiple, comma-delimited selectors
125 selectorText = selectorText.split(',');
126 selectors = selectorText.length;
127 for (j = 0; j < selectors; j++) {
128 rules[Ext.String.trim(selectorText[j]).toLowerCase()] = ssRules[i];
135 <span id='Ext-util-CSS-method-getRules'> /**
136 </span> * Gets all css rules for the document
137 * @param {Boolean} refreshCache true to refresh the internal cache
138 * @return {Object} An object (hash) of rules indexed by selector
140 getRules : function(refreshCache) {
141 if (rules === null || refreshCache) {
143 var ds = doc.styleSheets,
147 for (; i < len; i++) {
149 if (!ds[i].disabled) {
150 this.cacheStyleSheet(ds[i]);
158 <span id='Ext-util-CSS-method-getRule'> /**
159 </span> * Gets an an individual CSS rule by selector(s)
160 * @param {String/Array} selector The CSS selector or an array of selectors to try. The first selector that is found is returned.
161 * @param {Boolean} refreshCache true to refresh the internal cache if you have recently updated any rules or added styles dynamically
162 * @return {CSSRule} The CSS rule or null if one is not found
164 getRule: function(selector, refreshCache) {
165 var rs = this.getRules(refreshCache);
166 if (!Ext.isArray(selector)) {
167 return rs[selector.toLowerCase()];
169 for (var i = 0; i < selector.length; i++) {
170 if (rs[selector[i]]) {
171 return rs[selector[i].toLowerCase()];
177 <span id='Ext-util-CSS-method-updateRule'> /**
178 </span> * Updates a rule property
179 * @param {String/Array} selector If it's an array it tries each selector until it finds one. Stops immediately once one is found.
180 * @param {String} property The css property
181 * @param {String} value The new value for the property
182 * @return {Boolean} true If a rule was found and updated
184 updateRule : function(selector, property, value){
185 if (!Ext.isArray(selector)) {
186 var rule = this.getRule(selector);
188 rule.style[property.replace(camelRe, camelFn)] = value;
192 for (var i = 0; i < selector.length; i++) {
193 if (this.updateRule(selector[i], property, value)) {