stylesheet-debug.js revision 9706f76f9e56f413801eb2ff719cfbb6bdd3e841
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith p = d.createElement('p'), // Have to hold on to the node (see notes)
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith floatAttr = ('cssFloat' in style) ? 'cssFloat' : 'styleFloat',
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smithstyle.border = ''; // IE doesn't unset child properties
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (prop !== floatAttr && prop.toLowerCase().indexOf('float') != -1) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith case 'opacity':
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith case 'font' :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.font = style.fontStyle = style.fontVariant =
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith style.fontWeight = style.fontSize = style.lineHeight =
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith for (p in style) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (prop !== floatAttr && prop.toLowerCase().indexOf('float') != -1) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith factoryNode.innerHTML = '<style type="text/css">'+cssText+'</style>';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Factory or constructor
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (!(this instanceof Y.StyleSheet)) {
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 } else if (node && node.yuiSSID && sheets[node.yuiSSID]) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (seed.indexOf('{') != -1) { // create entire sheet from seed cssText
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith } else if (!name) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith if (!node || !/^(?:style|link)$/i.test(node.nodeName)) {
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 // IE stores StyleSheet under the "styleSheet" property
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // IE stores the rules collection under the "rules" property
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _rules = ('cssRules' in sheet) ? 'cssRules' : 'rules';
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // IE supports removeRule
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // IE supports addRule with different signature
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (sel,css,i) { sheet.insertRule(sel+' '+css,i); } :
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith function (sel,css,i) { sheet.addRule(sel,css,i); };
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 for (i = sheet[_rules].length - 1; i >= 0; --i) {
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith cssRules[sel].style.cssText += ';' + r.style.cssText;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Cache the sheet by the generated Id
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // Public API
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 disable : function () { sheet.disabled = true; return this; },
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith isEnabled : function () { return !sheet.disabled; },
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 // 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 rule.style.cssText = Y.StyleSheet.toCssText(css,rule.style.cssText);
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith _insertRule(sel,'{'+Y.StyleSheet.toCssText(css)+'}',idx);
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 return this;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith // remove rule properties or an entire rule
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith return this;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith var f = css.styleFloat || css.cssFloat || css['float'],prop;
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith delete css.styleFloat; delete css.cssFloat; delete css['float'];
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 return !!(name && sheet instanceof Y.StyleSheet &&
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 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 ??? - unsetting font-size-adjust has the same effect as unsetting font-size
9706f76f9e56f413801eb2ff719cfbb6bdd3e841Luke Smith}, '@VERSION@' );