stylesheet-debug.js revision 9706f76f9e56f413801eb2ff719cfbb6bdd3e841
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke SmithYUI.add('stylesheet', function(Y) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smithvar d = document,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith p = d.createElement('p'), // Have to hold on to the node (see notes)
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style = p.style, // worker style collection
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith sheets = {},
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith ssId = 0,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith floatAttr = ('cssFloat' in style) ? 'cssFloat' : 'styleFloat',
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _toCssText,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _unsetOpacity,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _unsetProperty,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _createSheet,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith factoryNode;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith_unsetOpacity = ('opacity' in style) ?
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (style) { style.opacity = ''; } :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (style) { style.filter = ''; };
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smithstyle.border = "1px solid red";
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smithstyle.border = ''; // IE doesn't unset child properties
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith_unsetProperty = style.borderLeft ?
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (style,prop) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith var p;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (prop !== floatAttr && prop.toLowerCase().indexOf('float') != -1) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith prop = floatAttr;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (typeof style[prop] === 'string') {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith switch (prop) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith case 'opacity':
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith case 'filter' : _unsetOpacity(style); break;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith case 'font' :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.font = style.fontStyle = style.fontVariant =
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.fontWeight = style.fontSize = style.lineHeight =
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.fontFamily = '';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith break;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith default :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith for (p in style) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (p.indexOf(prop) === 0) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style[p] = '';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (style,prop) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (prop !== floatAttr && prop.toLowerCase().indexOf('float') != -1) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith prop = floatAttr;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (typeof style[prop] === 'string') {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (prop === 'opacity') {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _unsetOpacity(style);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } else {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style[prop] = '';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith };
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke SmithfactoryNode = d.createElement('p');
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke SmithfactoryNode.innerHTML = '<style></style>';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith_createSheet = factoryNode.firstChild ?
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (cssText) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith factoryNode = d.createElement('p');
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith factoryNode.innerHTML = '<style type="text/css">'+cssText+'</style>';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return factoryNode.firstChild;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (cssText) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith factoryNode = d.createElement('style');
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith factoryNode.type = 'text/css';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (factoryNode.styleSheet) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith factoryNode.styleSheet.cssText = cssText;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return factoryNode;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith };
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke SmithY.StyleSheet = function (seed, name) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith var head,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith node,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith sheet,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith cssRules = {},
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _rules,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _insertRule,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _deleteRule;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Factory or constructor
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (!(this instanceof Y.StyleSheet)) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return new Y.StyleSheet(seed,name);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith head = d.getElementsByTagName('head')[0];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (!head) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // TODO: do something. Preferably something smart
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith Y.log('HEAD element not found to append STYLE node','error','Y.StyleSheet');
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith throw new Error('HEAD element not found to append STYLE node');
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith node = seed && (seed.nodeName ? seed :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith d.getElementById(seed.replace(/^#/,'')));
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (seed && sheets[seed]) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return sheets[seed];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } else if (node && node.yuiSSID && sheets[node.yuiSSID]) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return sheets[node.yuiSSID];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (typeof seed === 'string') {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (seed.indexOf('{') != -1) { // create entire sheet from seed cssText
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith node = _createSheet(seed);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } else if (!name) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith name = seed;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (!node || !/^(?:style|link)$/i.test(node.nodeName)) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith node = d.createElement('style');
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith node.type = 'text/css';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (node.parentNode !== head) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // styleSheet isn't available on the style node in FF2 until appended
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // to the head element. style nodes appended to body do not affect
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // change in Safari.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith head.appendChild(node);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // IE stores StyleSheet under the "styleSheet" property
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith sheet = node.sheet || node.styleSheet;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // IE stores the rules collection under the "rules" property
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _rules = ('cssRules' in sheet) ? 'cssRules' : 'rules';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // IE supports removeRule
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _deleteRule = ('deleteRule' in sheet) ?
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (i) { sheet.deleteRule(i); } :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (i) { sheet.removeRule(i); };
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // IE supports addRule with different signature
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _insertRule = ('insertRule' in sheet) ?
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (sel,css,i) { sheet.insertRule(sel+' '+css,i); } :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (sel,css,i) { sheet.addRule(sel,css,i); };
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Initialize the cssRules map from the node
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // TODO if xdomain link node, copy to a local style block and replace the
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // link node with the style node. CAVEAT: alternate stylesheet, @media
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith var i,r,sel;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith for (i = sheet[_rules].length - 1; i >= 0; --i) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith r = sheet[_rules][i];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith sel = r.selectorText;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (cssRules[sel]) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith cssRules[sel].style.cssText += ';' + r.style.cssText;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _deleteRule(i);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } else {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith cssRules[sel] = r;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Cache the sheet by the generated Id
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith node.yuiSSID = 'yui-stylesheet-' + (ssId++);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith Y.StyleSheet.register(node.yuiSSID,this);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (name) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith Y.StyleSheet.register(name,this);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Public API
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith Y.mix(this,{
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith getId : function () { return node.yuiSSID; },
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Enabling/disabling the stylesheet. Changes may be made to rules
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // while disabled.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith enable : function () { sheet.disabled = false; return this; },
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith disable : function () { sheet.disabled = true; return this; },
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith isEnabled : function () { return !sheet.disabled; },
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Update cssText for a rule. Add the rule if it's not present already
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // TODO: test for breakage in funny selectors ##i:go-..\\bo\om or valid
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // (i think) #i\:go\-\.\.\\\\bo\\om
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith setCSS : function (sel,css) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith var rule = cssRules[sel],
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith idx;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Opera throws an error if there's a syntax error in the cssText.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Should I do something about it, or let the error happen?
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (rule) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith rule.style.cssText = Y.StyleSheet.toCssText(css,rule.style.cssText);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } else {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith idx = sheet[_rules].length;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _insertRule(sel,'{'+Y.StyleSheet.toCssText(css)+'}',idx);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Safari replaces the rules collection, but maintains the rule
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // instances in the new collection when rules are added/removed
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith cssRules[sel] = sheet[_rules][idx];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return this;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith },
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // remove rule properties or an entire rule
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith unsetCSS : function (sel,css) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith var rule = cssRules[sel],
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith remove = !css,
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith rules, i;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (rule) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (!remove) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (!Y.Lang.isArray(css)) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith css = [css];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.cssText = rule.style.cssText;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith for (i = css.length - 1; i >= 0; --i) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _unsetProperty(style,css[i]);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (style.cssText) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith rule.style.cssText = style.cssText;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } else {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith remove = true;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (remove) { // remove the rule altogether
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith rules = sheet[_rules];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith for (i = rules.length - 1; i >= 0; --i) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (rules[i] === rule) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith delete cssRules[sel];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _deleteRule(i);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith break;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return this;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith },true);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith};
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith_toCssText = function (css,base) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith var f = css.styleFloat || css.cssFloat || css['float'],prop;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.cssText = base || '';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (f && !css[floatAttr]) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith css = Y.merge(css);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith delete css.styleFloat; delete css.cssFloat; delete css['float'];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith css[floatAttr] = f;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith for (prop in css) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (css.hasOwnProperty(prop)) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style[prop] = css[prop];
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return style.cssText;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith};
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith// Wrap IE's toCssText to catch opacity. The copy/merge is to preserve the
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith// input object's integrity, but if float and opacity are set, the input will
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith// be copied twice in IE. Is there a way to avoid this without increasing the
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith// byte count?
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke SmithY.StyleSheet.toCssText = ('opacity' in style) ? _toCssText :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (css, cssText) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if ('opacity' in css) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith css = Y.merge(css,{
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith filter: 'alpha(opacity='+(css.opacity*100)+')'
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith });
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith delete css.opacity;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith }
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return _toCssText(css,cssText);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith };
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke SmithY.StyleSheet.register = function (name,sheet) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return !!(name && sheet instanceof Y.StyleSheet &&
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith !sheets[name] && (sheets[name] = sheet));
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith};
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith/*
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke SmithNOTES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Style node must be added to the head element. Safari does not honor styles
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith applied to StyleSheet objects on style nodes in the body.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * StyleSheet object is created on the style node when the style node is added
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith to the head element in Firefox 2 (and maybe 3?)
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * The cssRules collection is replaced after insertRule/deleteRule calls in
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith Safari 3.1. Existing Rules are used in the new collection, so the collection
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith cannot be cached, but the rules can be.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Opera requires that the index be passed with insertRule.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Same-domain restrictions prevent modifying StyleSheet objects attached to
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith link elements with remote href (or "about:blank" or "javascript:false")
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * IE names StyleSheet related properties and methods differently (see code)
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * IE converts tag names to upper case in the Rule's selectorText
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * IE converts empty string assignment to complex properties to value settings
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith for all child properties. E.g. style.background = '' sets non-'' values on
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.backgroundPosition, style.backgroundColor, etc. All else clear
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.background and all child properties.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * IE assignment style.filter = '' will result in style.cssText == 'FILTER:'
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * All browsers support Rule.style.cssText as a read/write property, leaving
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith only opacity needing to be accounted for.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Benchmarks of style.property = value vs style.cssText += 'property: value'
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith indicate cssText is slightly slower for single property assignment. For
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith multiple property assignment, cssText speed stays relatively the same where
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.property speed decreases linearly by the number of properties set.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith Exception being Opera 9.27, where style.property is always faster than
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.cssText.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Opera 9.5b throws a syntax error when assigning cssText with a syntax error.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Opera 9.5 doesn't honor rule.style.cssText = ''. Previous style persists.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith You have to remove the rule altogether.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Stylesheet properties set with !important will trump inline style set on an
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith element or in el.style.property.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Creating a worker style collection like document.createElement('p').style;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith will fail after a time in FF (~5secs of inactivity). Property assignments
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith will not alter the property or cssText. It may be the generated node is
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith garbage collected and the style collection becomes inert (speculation).
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * When creating an entire sheet at once, only FF2/3 & Opera allow creating a
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style node, setting its innerHTML and appending to head.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * When creating an entire sheet at once, Safari requires the style node to be
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith created with content in innerHTML of another element.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * When creating an entire sheet at once, IE requires the style node content to
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith be set via node.styleSheet.cssText
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * When creating an entire sheet at once in IE, styleSheet.cssText can't be
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith written until node.type = 'text/css'; is performed.
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith * Setting complex properties in cssText will SOMETIMES allow child properties
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith to be unset
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith set unset FF2 FF3 S3.1 IE6 IE7 Op9.27 Op9.5
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith ---------- ----------------- --- --- ---- --- --- ------ -----
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith border -top NO NO YES YES YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -top-color NO NO YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -color NO NO NO NO NO
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith background -color NO NO YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -position NO NO YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -position-x NO NO NO NO NO
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith font line-height YES YES NO NO NO NO YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -style YES YES NO YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -size YES YES NO YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -size-adjust ??? ??? n/a n/a n/a ??? ???
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith padding -top NO NO YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith margin -top NO NO YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith list-style -type YES YES YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith -position YES YES YES YES YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith overflow -x NO NO YES n/a YES
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith ??? - unsetting font-size-adjust has the same effect as unsetting font-size
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith*/
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith}, '@VERSION@' );