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