[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 YUI.add('yui2-stylesheet', function(Y) { 2 var YAHOO = Y.YUI2; 3 /* 4 Copyright (c) 2011, Yahoo! Inc. All rights reserved. 5 Code licensed under the BSD License: 6 http://developer.yahoo.com/yui/license.html 7 version: 2.9.0 8 */ 9 /** 10 * The StyleSheet component is a utility for managing css rules at the 11 * stylesheet level 12 * 13 * @module stylesheet 14 * @namespace YAHOO.util 15 * @requires yahoo 16 */ 17 (function () { 18 19 var d = document, 20 p = d.createElement('p'), // Have to hold the node (see notes) 21 workerStyle = p.style, // worker style collection 22 lang = YAHOO.lang, 23 selectors = {}, 24 sheets = {}, 25 ssId = 0, 26 floatAttr = ('cssFloat' in workerStyle) ? 'cssFloat' : 'styleFloat', 27 _toCssText, 28 _unsetOpacity, 29 _unsetProperty; 30 31 /* 32 * Normalizes the removal of an assigned style for opacity. IE uses the filter property. 33 */ 34 _unsetOpacity = ('opacity' in workerStyle) ? 35 function (style) { style.opacity = ''; } : 36 function (style) { style.filter = ''; }; 37 38 /* 39 * Normalizes the removal of an assigned style for a given property. Expands 40 * shortcut properties if necessary and handles the various names for the float property. 41 */ 42 workerStyle.border = "1px solid red"; 43 workerStyle.border = ''; // IE doesn't unset child properties 44 _unsetProperty = workerStyle.borderLeft ? 45 function (style,prop) { 46 var p; 47 if (prop !== floatAttr && prop.toLowerCase().indexOf('float') != -1) { 48 prop = floatAttr; 49 } 50 if (typeof style[prop] === 'string') { 51 switch (prop) { 52 case 'opacity': 53 case 'filter' : _unsetOpacity(style); break; 54 case 'font' : 55 style.font = style.fontStyle = style.fontVariant = 56 style.fontWeight = style.fontSize = style.lineHeight = 57 style.fontFamily = ''; 58 break; 59 default : 60 for (p in style) { 61 if (p.indexOf(prop) === 0) { 62 style[p] = ''; 63 } 64 } 65 } 66 } 67 } : 68 function (style,prop) { 69 if (prop !== floatAttr && prop.toLowerCase().indexOf('float') != -1) { 70 prop = floatAttr; 71 } 72 if (lang.isString(style[prop])) { 73 if (prop === 'opacity') { 74 _unsetOpacity(style); 75 } else { 76 style[prop] = ''; 77 } 78 } 79 }; 80 81 /** 82 * Create an instance of YAHOO.util.StyleSheet to encapsulate a css stylesheet. 83 * The constructor can be called using function or constructor syntax. 84 * <pre><code>var sheet = YAHOO.util.StyleSheet(..);</pre></code> 85 * or 86 * <pre><code>var sheet = new YAHOO.util.StyleSheet(..);</pre></code> 87 * 88 * The first parameter passed can be any of the following things: 89 * <ul> 90 * <li>The desired string name to register a new empty sheet</li> 91 * <li>The string name of an existing YAHOO.util.StyleSheet instance</li> 92 * <li>The unique yuiSSID generated for an existing YAHOO.util.StyleSheet instance</li> 93 * <li>The id of an existing <code><link></code> or <code><style></code> node</li> 94 * <li>The node reference for an existing <code><link></code> or <code><style></code> node</li> 95 * <li>A chunk of css text to create a new stylesheet from</li> 96 * </ul> 97 * 98 * <p>If a string is passed, StyleSheet will first look in its static name 99 * registry for an existing sheet, then in the DOM for an element with that id. 100 * If neither are found and the string contains the { character, it will be 101 * used as a the initial cssText for a new StyleSheet. Otherwise, a new empty 102 * StyleSheet is created, assigned the string value as a name, and registered 103 * statically by that name.</p> 104 * 105 * <p>The optional second parameter is a string name to register the sheet as. 106 * This param is largely useful when providing a node id/ref or chunk of css 107 * text to create a populated instance.</p> 108 * 109 * @class StyleSheet 110 * @constructor 111 * @param seed {String|<style> element} a style or link node, its id, or a name or 112 * yuiSSID of a StyleSheet, or a string of css text (see above) 113 * @param name {String} OPTIONAL name to register instance for future static 114 * access 115 */ 116 function StyleSheet(seed, name) { 117 var head, 118 node, 119 sheet, 120 cssRules = {}, 121 _rules, 122 _insertRule, 123 _deleteRule, 124 i,r,sel; 125 126 // Factory or constructor 127 if (!(this instanceof StyleSheet)) { 128 return new StyleSheet(seed,name); 129 } 130 131 // capture the DOM node if the string is an id 132 node = seed && (seed.nodeName ? seed : d.getElementById(seed)); 133 134 // Check for the StyleSheet in the static registry 135 if (seed && sheets[seed]) { 136 return sheets[seed]; 137 } else if (node && node.yuiSSID && sheets[node.yuiSSID]) { 138 return sheets[node.yuiSSID]; 139 } 140 141 // Create a style node if necessary 142 if (!node || !/^(?:style|link)$/i.test(node.nodeName)) { 143 node = d.createElement('style'); 144 node.type = 'text/css'; 145 } 146 147 if (lang.isString(seed)) { 148 // Create entire sheet from seed cssText 149 if (seed.indexOf('{') != -1) { 150 // Not a load-time fork because low run-time impact and IE fails 151 // test for s.styleSheet at page load time (oddly) 152 if (node.styleSheet) { 153 node.styleSheet.cssText = seed; 154 } else { 155 node.appendChild(d.createTextNode(seed)); 156 } 157 } else if (!name) { 158 name = seed; 159 } 160 } 161 162 if (!node.parentNode || node.parentNode.nodeName.toLowerCase() !== 'head') { 163 head = (node.ownerDocument || d).getElementsByTagName('head')[0]; 164 // styleSheet isn't available on the style node in FF2 until appended 165 // to the head element. style nodes appended to body do not affect 166 // change in Safari. 167 head.appendChild(node); 168 } 169 170 // Begin setting up private aliases to the important moving parts 171 // 1. The stylesheet object 172 // IE stores StyleSheet under the "styleSheet" property 173 // Safari doesn't populate sheet for xdomain link elements 174 sheet = node.sheet || node.styleSheet; 175 176 // 2. The style rules collection 177 // IE stores the rules collection under the "rules" property 178 _rules = sheet && ('cssRules' in sheet) ? 'cssRules' : 'rules'; 179 180 // 3. The method to remove a rule from the stylesheet 181 // IE supports removeRule 182 _deleteRule = ('deleteRule' in sheet) ? 183 function (i) { sheet.deleteRule(i); } : 184 function (i) { sheet.removeRule(i); }; 185 186 // 4. The method to add a new rule to the stylesheet 187 // IE supports addRule with different signature 188 _insertRule = ('insertRule' in sheet) ? 189 function (sel,css,i) { sheet.insertRule(sel+' {'+css+'}',i); } : 190 function (sel,css,i) { sheet.addRule(sel,css,i); }; 191 192 // 5. Initialize the cssRules map from the node 193 // xdomain link nodes forbid access to the cssRules collection, so this 194 // will throw an error. 195 // TODO: research alternate stylesheet, @media 196 for (i = sheet[_rules].length - 1; i >= 0; --i) { 197 r = sheet[_rules][i]; 198 sel = r.selectorText; 199 200 if (cssRules[sel]) { 201 cssRules[sel].style.cssText += ';' + r.style.cssText; 202 _deleteRule(i); 203 } else { 204 cssRules[sel] = r; 205 } 206 } 207 208 // Cache the instance by the generated Id 209 node.yuiSSID = 'yui-stylesheet-' + (ssId++); 210 StyleSheet.register(node.yuiSSID,this); 211 212 // Register the instance by name if provided or defaulted from seed 213 if (name) { 214 StyleSheet.register(name,this); 215 } 216 217 // Public API 218 lang.augmentObject(this,{ 219 /** 220 * Get the unique yuiSSID for this StyleSheet instance 221 * 222 * @method getId 223 * @return {Number} the static id 224 */ 225 getId : function () { return node.yuiSSID; }, 226 227 /** 228 * The <style> element that this instance encapsulates 229 * 230 * @property node 231 * @type HTMLElement 232 */ 233 node : node, 234 235 /** 236 * Enable all the rules in the sheet 237 * 238 * @method enable 239 * @return {StyleSheet} the instance 240 * @chainable 241 */ 242 // Enabling/disabling the stylesheet. Changes may be made to rules 243 // while disabled. 244 enable : function () { sheet.disabled = false; return this; }, 245 246 /** 247 * Disable all the rules in the sheet. Rules may be changed while the 248 * StyleSheet is disabled. 249 * 250 * @method disable 251 * @return {StyleSheet} the instance 252 * @chainable 253 */ 254 disable : function () { sheet.disabled = true; return this; }, 255 256 /** 257 * Returns boolean indicating whether the StyleSheet is enabled 258 * 259 * @method isEnabled 260 * @return {Boolean} is it enabled? 261 */ 262 isEnabled : function () { return !sheet.disabled; }, 263 264 /** 265 * <p>Set style properties for a provided selector string. 266 * If the selector includes commas, it will be split into individual 267 * selectors and applied accordingly. If the selector string does not 268 * have a corresponding rule in the sheet, it will be added.</p> 269 * 270 * <p>The second parameter can be either a string of CSS text, 271 * formatted as CSS ("font-size: 10px;"), or an object collection of 272 * properties and their new values. Object properties must be in 273 * JavaScript format ({ fontSize: "10px" }).</p> 274 * 275 * <p>The float style property will be set by any of "float", 276 * "styleFloat", or "cssFloat" if passed in the 277 * object map. Use "float: left;" format when passing a CSS text 278 * string.</p> 279 * 280 * @method set 281 * @param sel {String} the selector string to apply the changes to 282 * @param css {Object|String} Object literal of style properties and 283 * new values, or a string of cssText 284 * @return {StyleSheet} the StyleSheet instance 285 * @chainable 286 */ 287 set : function (sel,css) { 288 var rule = cssRules[sel], 289 multi = sel.split(/\s*,\s*/),i, 290 idx; 291 292 // IE's addRule doesn't support multiple comma delimited selectors 293 if (multi.length > 1) { 294 for (i = multi.length - 1; i >= 0; --i) { 295 this.set(multi[i], css); 296 } 297 return this; 298 } 299 300 // Some selector values can cause IE to hang 301 if (!StyleSheet.isValidSelector(sel)) { 302 return this; 303 } 304 305 // Opera throws an error if there's a syntax error in assigned 306 // cssText. Avoid this using a worker style collection, then 307 // assigning the resulting cssText. 308 if (rule) { 309 rule.style.cssText = StyleSheet.toCssText(css,rule.style.cssText); 310 } else { 311 idx = sheet[_rules].length; 312 css = StyleSheet.toCssText(css); 313 314 // IE throws an error when attempting to addRule(sel,'',n) 315 // which would crop up if no, or only invalid values are used 316 if (css) { 317 _insertRule(sel, css, idx); 318 319 // Safari replaces the rules collection, but maintains the 320 // rule instances in the new collection when rules are 321 // added/removed 322 cssRules[sel] = sheet[_rules][idx]; 323 } 324 } 325 return this; 326 }, 327 328 /** 329 * <p>Unset style properties for a provided selector string, removing 330 * their effect from the style cascade.</p> 331 * 332 * <p>If the selector includes commas, it will be split into individual 333 * selectors and applied accordingly. If there are no properties 334 * remaining in the rule after unsetting, the rule is removed.</p> 335 * 336 * <p>The style property or properties in the second parameter must be the 337 * <p>JavaScript style property names. E.g. fontSize rather than font-size.</p> 338 * 339 * <p>The float style property will be unset by any of "float", 340 * "styleFloat", or "cssFloat".</p> 341 * 342 * @method unset 343 * @param sel {String} the selector string to apply the changes to 344 * @param css {String|Array} style property name or Array of names 345 * @return {StyleSheet} the StyleSheet instance 346 * @chainable 347 */ 348 unset : function (sel,css) { 349 var rule = cssRules[sel], 350 multi = sel.split(/\s*,\s*/), 351 remove = !css, 352 rules, i; 353 354 // IE's addRule doesn't support multiple comma delimited selectors 355 // so rules are mapped internally by atomic selectors 356 if (multi.length > 1) { 357 for (i = multi.length - 1; i >= 0; --i) { 358 this.unset(multi[i], css); 359 } 360 return this; 361 } 362 363 if (rule) { 364 if (!remove) { 365 if (!lang.isArray(css)) { 366 css = [css]; 367 } 368 369 workerStyle.cssText = rule.style.cssText; 370 for (i = css.length - 1; i >= 0; --i) { 371 _unsetProperty(workerStyle,css[i]); 372 } 373 374 if (workerStyle.cssText) { 375 rule.style.cssText = workerStyle.cssText; 376 } else { 377 remove = true; 378 } 379 } 380 381 if (remove) { // remove the rule altogether 382 rules = sheet[_rules]; 383 for (i = rules.length - 1; i >= 0; --i) { 384 if (rules[i] === rule) { 385 delete cssRules[sel]; 386 _deleteRule(i); 387 break; 388 } 389 } 390 } 391 } 392 return this; 393 }, 394 395 /** 396 * Get the current cssText for a rule or the entire sheet. If the 397 * selector param is supplied, only the cssText for that rule will be 398 * returned, if found. If the selector string targets multiple 399 * selectors separated by commas, the cssText of the first rule only 400 * will be returned. If no selector string, the stylesheet's full 401 * cssText will be returned. 402 * 403 * @method getCssText 404 * @param sel {String} Selector string 405 * @return {String} 406 */ 407 getCssText : function (sel) { 408 var rule, css, selector; 409 410 if (lang.isString(sel)) { 411 // IE's addRule doesn't support multiple comma delimited 412 // selectors so rules are mapped internally by atomic selectors 413 rule = cssRules[sel.split(/\s*,\s*/)[0]]; 414 415 return rule ? rule.style.cssText : null; 416 } else { 417 css = []; 418 for (selector in cssRules) { 419 if (cssRules.hasOwnProperty(selector)) { 420 rule = cssRules[selector]; 421 css.push(rule.selectorText+" {"+rule.style.cssText+"}"); 422 } 423 } 424 return css.join("\n"); 425 } 426 } 427 },true); 428 429 } 430 431 _toCssText = function (css,base) { 432 var f = css.styleFloat || css.cssFloat || css['float'], 433 prop; 434 435 // A very difficult to repro/isolate IE 9 beta (and Platform Preview 7) bug 436 // was reduced to this line throwing the error: 437 // "Invalid this pointer used as target for method call" 438 // It appears that the style collection is corrupted. The error is 439 // catchable, so in a best effort to work around it, replace the 440 // p and workerStyle and try the assignment again. 441 try { 442 workerStyle.cssText = base || ''; 443 } catch (ex) { 444 p = d.createElement('p'); 445 workerStyle = p.style; 446 workerStyle.cssText = base || ''; 447 } 448 449 if (lang.isString(css)) { 450 // There is a danger here of incremental memory consumption in Opera 451 workerStyle.cssText += ';' + css; 452 } else { 453 if (f && !css[floatAttr]) { 454 css = lang.merge(css); 455 delete css.styleFloat; delete css.cssFloat; delete css['float']; 456 css[floatAttr] = f; 457 } 458 459 for (prop in css) { 460 if (css.hasOwnProperty(prop)) { 461 try { 462 // IE throws Invalid Value errors and doesn't like whitespace 463 // in values ala ' red' or 'red ' 464 workerStyle[prop] = lang.trim(css[prop]); 465 } 466 catch (e) { 467 } 468 } 469 } 470 } 471 472 return workerStyle.cssText; 473 }; 474 475 lang.augmentObject(StyleSheet, { 476 /** 477 * <p>Converts an object literal of style properties and values into a string 478 * of css text. This can then be assigned to el.style.cssText.</p> 479 * 480 * <p>The optional second parameter is a cssText string representing the 481 * starting state of the style prior to alterations. This is most often 482 * extracted from the eventual target's current el.style.cssText.</p> 483 * 484 * @method StyleSheet.toCssText 485 * @param css {Object} object literal of style properties and values 486 * @param cssText {String} OPTIONAL starting cssText value 487 * @return {String} the resulting cssText string 488 * @static 489 */ 490 toCssText : (('opacity' in workerStyle) ? _toCssText : 491 // Wrap IE's toCssText to catch opacity. The copy/merge is to preserve 492 // the input object's integrity, but if float and opacity are set, the 493 // input will be copied twice in IE. Is there a way to avoid this 494 // without increasing the byte count? 495 function (css, cssText) { 496 if (lang.isObject(css) && 'opacity' in css) { 497 css = lang.merge(css,{ 498 filter: 'alpha(opacity='+(css.opacity*100)+')' 499 }); 500 delete css.opacity; 501 } 502 return _toCssText(css,cssText); 503 }), 504 505 /** 506 * Registers a StyleSheet instance in the static registry by the given name 507 * 508 * @method StyleSheet.register 509 * @param name {String} the name to assign the StyleSheet in the registry 510 * @param sheet {StyleSheet} The StyleSheet instance 511 * @return {Boolean} false if no name or sheet is not a StyleSheet 512 * instance. true otherwise. 513 * @static 514 */ 515 register : function (name,sheet) { 516 return !!(name && sheet instanceof StyleSheet && 517 !sheets[name] && (sheets[name] = sheet)); 518 }, 519 520 /** 521 * <p>Determines if a selector string is safe to use. Used internally 522 * in set to prevent IE from locking up when attempting to add a rule for a 523 * "bad selector".</p> 524 * 525 * <p>Bad selectors are considered to be any string containing unescaped 526 * `~!@$%^&()+=|{}[];'"?< or space. Also forbidden are . or # followed by 527 * anything other than an alphanumeric. Additionally -abc or .-abc or 528 * #_abc or '# ' all fail. There are likely more failure cases, so 529 * please file a bug if you encounter one.</p> 530 * 531 * @method StyleSheet.isValidSelector 532 * @param sel {String} the selector string 533 * @return {Boolean} 534 * @static 535 */ 536 isValidSelector : function (sel) { 537 var valid = false; 538 539 if (sel && lang.isString(sel)) { 540 541 if (!selectors.hasOwnProperty(sel)) { 542 // TEST: there should be nothing but white-space left after 543 // these destructive regexs 544 selectors[sel] = !/\S/.test( 545 // combinators 546 sel.replace(/\s+|\s*[+~>]\s*/g,' '). 547 // attribute selectors (contents not validated) 548 replace(/([^ ])\[.*?\]/g,'$1'). 549 // pseudo-class|element selectors (contents of parens 550 // such as :nth-of-type(2) or :not(...) not validated) 551 replace(/([^ ])::?[a-z][a-z\-]+[a-z](?:\(.*?\))?/ig,'$1'). 552 // element tags 553 replace(/(?:^| )[a-z0-6]+/ig,' '). 554 // escaped characters 555 replace(/\\./g,''). 556 // class and id identifiers 557 replace(/[.#]\w[\w\-]*/g,'')); 558 } 559 560 valid = selectors[sel]; 561 } 562 563 return valid; 564 } 565 },true); 566 567 YAHOO.util.StyleSheet = StyleSheet; 568 569 })(); 570 571 /* 572 573 NOTES 574 * Style node must be added to the head element. Safari does not honor styles 575 applied to StyleSheet objects on style nodes in the body. 576 * StyleSheet object is created on the style node when the style node is added 577 to the head element in Firefox 2 (and maybe 3?) 578 * The cssRules collection is replaced after insertRule/deleteRule calls in 579 Safari 3.1. Existing Rules are used in the new collection, so the collection 580 cannot be cached, but the rules can be. 581 * Opera requires that the index be passed with insertRule. 582 * Same-domain restrictions prevent modifying StyleSheet objects attached to 583 link elements with remote href (or "about:blank" or "javascript:false") 584 * Same-domain restrictions prevent reading StyleSheet cssRules/rules 585 collection of link elements with remote href (or "about:blank" or 586 "javascript:false") 587 * Same-domain restrictions result in Safari not populating node.sheet property 588 for link elements with remote href (et.al) 589 * IE names StyleSheet related properties and methods differently (see code) 590 * IE converts tag names to upper case in the Rule's selectorText 591 * IE converts empty string assignment to complex properties to value settings 592 for all child properties. E.g. style.background = '' sets non-'' values on 593 style.backgroundPosition, style.backgroundColor, etc. All else clear 594 style.background and all child properties. 595 * IE assignment style.filter = '' will result in style.cssText == 'FILTER:' 596 * All browsers support Rule.style.cssText as a read/write property, leaving 597 only opacity needing to be accounted for. 598 * Benchmarks of style.property = value vs style.cssText += 'property: value' 599 indicate cssText is slightly slower for single property assignment. For 600 multiple property assignment, cssText speed stays relatively the same where 601 style.property speed decreases linearly by the number of properties set. 602 Exception being Opera 9.27, where style.property is always faster than 603 style.cssText. 604 * Opera 9.5b throws a syntax error when assigning cssText with a syntax error. 605 * Opera 9.5 doesn't honor rule.style.cssText = ''. Previous style persists. 606 You have to remove the rule altogether. 607 * Stylesheet properties set with !important will trump inline style set on an 608 element or in el.style.property. 609 * Creating a worker style collection like document.createElement('p').style; 610 will fail after a time in FF (~5secs of inactivity). Property assignments 611 will not alter the property or cssText. It may be the generated node is 612 garbage collected and the style collection becomes inert (speculation). 613 * IE locks up when attempting to add a rule with a selector including at least 614 characters {[]}~`!@%^&*()+=|? (unescaped) and leading _ or - 615 such as addRule('-foo','{ color: red }') or addRule('._abc','{...}') 616 * IE's addRule doesn't support comma separated selectors such as 617 addRule('.foo, .bar','{..}') 618 * IE throws an error on valid values with leading/trailing white space. 619 * When creating an entire sheet at once, only FF2/3 & Opera allow creating a 620 style node, setting its innerHTML and appending to head. 621 * When creating an entire sheet at once, Safari requires the style node to be 622 created with content in innerHTML of another element. 623 * When creating an entire sheet at once, IE requires the style node content to 624 be set via node.styleSheet.cssText 625 * When creating an entire sheet at once in IE, styleSheet.cssText can't be 626 written until node.type = 'text/css'; is performed. 627 * When creating an entire sheet at once in IE, load-time fork on 628 var styleNode = d.createElement('style'); _method = styleNode.styleSheet ?.. 629 fails (falsey). During run-time, the test for .styleSheet works fine 630 * Setting complex properties in cssText will SOMETIMES allow child properties 631 to be unset 632 set unset FF2 FF3 S3.1 IE6 IE7 Op9.27 Op9.5 633 ---------- ----------------- --- --- ---- --- --- ------ ----- 634 border -top NO NO YES YES YES YES YES 635 -top-color NO NO YES YES YES 636 -color NO NO NO NO NO 637 background -color NO NO YES YES YES 638 -position NO NO YES YES YES 639 -position-x NO NO NO NO NO 640 font line-height YES YES NO NO NO NO YES 641 -style YES YES NO YES YES 642 -size YES YES NO YES YES 643 -size-adjust ??? ??? n/a n/a n/a ??? ??? 644 padding -top NO NO YES YES YES 645 margin -top NO NO YES YES YES 646 list-style -type YES YES YES YES YES 647 -position YES YES YES YES YES 648 overflow -x NO NO YES n/a YES 649 650 ??? - unsetting font-size-adjust has the same effect as unsetting font-size 651 * FireFox and WebKit populate rule.cssText as "SELECTOR { CSSTEXT }", but 652 Opera and IE do not. 653 * IE6 and IE7 silently ignore the { and } if passed into addRule('.foo','{ 654 color:#000}',0). IE8 does not and creates an empty rule. 655 * IE6-8 addRule('.foo','',n) throws an error. Must supply *some* cssText 656 */ 657 658 YAHOO.register("stylesheet", YAHOO.util.StyleSheet, {version: "2.9.0", build: "2800"}); 659 660 }, '2.9.0' ,{"requires": ["yui2-yahoo"]});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |