Project

General

Profile

1
/*
2
Copyright (c) 2007, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
5
version: 2.4.1
6
*/
7
/**
8
 * The dom module provides helper methods for manipulating Dom elements.
9
 * @module dom
10
 *
11
 */
12

    
13
(function() {
14
    var Y = YAHOO.util,     // internal shorthand
15
        getStyle,           // for load time browser branching
16
        setStyle,           // ditto
17
        id_counter = 0,     // for use with generateId
18
        propertyCache = {}, // for faster hyphen converts
19
        reClassNameCache = {},          // cache regexes for className
20
        document = window.document;     // cache for faster lookups
21
    
22
    // brower detection
23
    var isOpera = YAHOO.env.ua.opera,
24
        isSafari = YAHOO.env.ua.webkit, 
25
        isGecko = YAHOO.env.ua.gecko,
26
        isIE = YAHOO.env.ua.ie; 
27
    
28
    // regex cache
29
    var patterns = {
30
        HYPHEN: /(-[a-z])/i, // to normalize get/setStyle
31
        ROOT_TAG: /^body|html$/i // body for quirks mode, html for standards
32
    };
33

    
34
    var toCamel = function(property) {
35
        if ( !patterns.HYPHEN.test(property) ) {
36
            return property; // no hyphens
37
        }
38
        
39
        if (propertyCache[property]) { // already converted
40
            return propertyCache[property];
41
        }
42
       
43
        var converted = property;
44
 
45
        while( patterns.HYPHEN.exec(converted) ) {
46
            converted = converted.replace(RegExp.$1,
47
                    RegExp.$1.substr(1).toUpperCase());
48
        }
49
        
50
        propertyCache[property] = converted;
51
        return converted;
52
        //return property.replace(/-([a-z])/gi, function(m0, m1) {return m1.toUpperCase()}) // cant use function as 2nd arg yet due to safari bug
53
    };
54
    
55
    var getClassRegEx = function(className) {
56
        var re = reClassNameCache[className];
57
        if (!re) {
58
            re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
59
            reClassNameCache[className] = re;
60
        }
61
        return re;
62
    };
63

    
64
    // branching at load instead of runtime
65
    if (document.defaultView && document.defaultView.getComputedStyle) { // W3C DOM method
66
        getStyle = function(el, property) {
67
            var value = null;
68
            
69
            if (property == 'float') { // fix reserved word
70
                property = 'cssFloat';
71
            }
72

    
73
            var computed = document.defaultView.getComputedStyle(el, '');
74
            if (computed) { // test computed before touching for safari
75
                value = computed[toCamel(property)];
76
            }
77
            
78
            return el.style[property] || value;
79
        };
80
    } else if (document.documentElement.currentStyle && isIE) { // IE method
81
        getStyle = function(el, property) {                         
82
            switch( toCamel(property) ) {
83
                case 'opacity' :// IE opacity uses filter
84
                    var val = 100;
85
                    try { // will error if no DXImageTransform
86
                        val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
87

    
88
                    } catch(e) {
89
                        try { // make sure its in the document
90
                            val = el.filters('alpha').opacity;
91
                        } catch(e) {
92
                        }
93
                    }
94
                    return val / 100;
95
                case 'float': // fix reserved word
96
                    property = 'styleFloat'; // fall through
97
                default: 
98
                    // test currentStyle before touching
99
                    var value = el.currentStyle ? el.currentStyle[property] : null;
100
                    return ( el.style[property] || value );
101
            }
102
        };
103
    } else { // default to inline only
104
        getStyle = function(el, property) { return el.style[property]; };
105
    }
106
    
107
    if (isIE) {
108
        setStyle = function(el, property, val) {
109
            switch (property) {
110
                case 'opacity':
111
                    if ( YAHOO.lang.isString(el.style.filter) ) { // in case not appended
112
                        el.style.filter = 'alpha(opacity=' + val * 100 + ')';
113
                        
114
                        if (!el.currentStyle || !el.currentStyle.hasLayout) {
115
                            el.style.zoom = 1; // when no layout or cant tell
116
                        }
117
                    }
118
                    break;
119
                case 'float':
120
                    property = 'styleFloat';
121
                default:
122
                el.style[property] = val;
123
            }
124
        };
125
    } else {
126
        setStyle = function(el, property, val) {
127
            if (property == 'float') {
128
                property = 'cssFloat';
129
            }
130
            el.style[property] = val;
131
        };
132
    }
133

    
134
    var testElement = function(node, method) {
135
        return node && node.nodeType == 1 && ( !method || method(node) );
136
    };
137

    
138
    /**
139
     * Provides helper methods for DOM elements.
140
     * @namespace YAHOO.util
141
     * @class Dom
142
     */
143
    YAHOO.util.Dom = {
144
        /**
145
         * Returns an HTMLElement reference.
146
         * @method get
147
         * @param {String | HTMLElement |Array} el Accepts a string to use as an ID for getting a DOM reference, an actual DOM reference, or an Array of IDs and/or HTMLElements.
148
         * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
149
         */
150
        get: function(el) {
151
            if (el && (el.tagName || el.item)) { // HTMLElement, or HTMLCollection
152
                return el;
153
            }
154

    
155
            if (YAHOO.lang.isString(el) || !el) { // HTMLElement or null
156
                return document.getElementById(el);
157
            }
158
            
159
            if (el.length !== undefined) { // array-like 
160
                var c = [];
161
                for (var i = 0, len = el.length; i < len; ++i) {
162
                    c[c.length] = Y.Dom.get(el[i]);
163
                }
164
                
165
                return c;
166
            }
167

    
168
            return el; // some other object, just pass it back
169
        },
170
    
171
        /**
172
         * Normalizes currentStyle and ComputedStyle.
173
         * @method getStyle
174
         * @param {String | HTMLElement |Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
175
         * @param {String} property The style property whose value is returned.
176
         * @return {String | Array} The current value of the style property for the element(s).
177
         */
178
        getStyle: function(el, property) {
179
            property = toCamel(property);
180
            
181
            var f = function(element) {
182
                return getStyle(element, property);
183
            };
184
            
185
            return Y.Dom.batch(el, f, Y.Dom, true);
186
        },
187
    
188
        /**
189
         * Wrapper for setting style properties of HTMLElements.  Normalizes "opacity" across modern browsers.
190
         * @method setStyle
191
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
192
         * @param {String} property The style property to be set.
193
         * @param {String} val The value to apply to the given property.
194
         */
195
        setStyle: function(el, property, val) {
196
            property = toCamel(property);
197
            
198
            var f = function(element) {
199
                setStyle(element, property, val);
200
                
201
            };
202
            
203
            Y.Dom.batch(el, f, Y.Dom, true);
204
        },
205
        
206
        /**
207
         * Gets the current position of an element based on page coordinates.  Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
208
         * @method getXY
209
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
210
         * @return {Array} The XY position of the element(s)
211
         */
212
        getXY: function(el) {
213
            var f = function(el) {
214
                // has to be part of document to have pageXY
215
                if ( (el.parentNode === null || el.offsetParent === null ||
216
                        this.getStyle(el, 'display') == 'none') && el != el.ownerDocument.body) {
217
                    return false;
218
                }
219
                
220
                return getXY(el);
221
            };
222
            
223
            return Y.Dom.batch(el, f, Y.Dom, true);
224
        },
225
        
226
        /**
227
         * Gets the current X position of an element based on page coordinates.  The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
228
         * @method getX
229
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
230
         * @return {Number | Array} The X position of the element(s)
231
         */
232
        getX: function(el) {
233
            var f = function(el) {
234
                return Y.Dom.getXY(el)[0];
235
            };
236
            
237
            return Y.Dom.batch(el, f, Y.Dom, true);
238
        },
239
        
240
        /**
241
         * Gets the current Y position of an element based on page coordinates.  Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
242
         * @method getY
243
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
244
         * @return {Number | Array} The Y position of the element(s)
245
         */
246
        getY: function(el) {
247
            var f = function(el) {
248
                return Y.Dom.getXY(el)[1];
249
            };
250
            
251
            return Y.Dom.batch(el, f, Y.Dom, true);
252
        },
253
        
254
        /**
255
         * Set the position of an html element in page coordinates, regardless of how the element is positioned.
256
         * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
257
         * @method setXY
258
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
259
         * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
260
         * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
261
         */
262
        setXY: function(el, pos, noRetry) {
263
            var f = function(el) {
264
                var style_pos = this.getStyle(el, 'position');
265
                if (style_pos == 'static') { // default to relative
266
                    this.setStyle(el, 'position', 'relative');
267
                    style_pos = 'relative';
268
                }
269

    
270
                var pageXY = this.getXY(el);
271
                if (pageXY === false) { // has to be part of doc to have pageXY
272
                    return false; 
273
                }
274
                
275
                var delta = [ // assuming pixels; if not we will have to retry
276
                    parseInt( this.getStyle(el, 'left'), 10 ),
277
                    parseInt( this.getStyle(el, 'top'), 10 )
278
                ];
279
            
280
                if ( isNaN(delta[0]) ) {// in case of 'auto'
281
                    delta[0] = (style_pos == 'relative') ? 0 : el.offsetLeft;
282
                } 
283
                if ( isNaN(delta[1]) ) { // in case of 'auto'
284
                    delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
285
                } 
286
        
287
                if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
288
                if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }
289
              
290
                if (!noRetry) {
291
                    var newXY = this.getXY(el);
292

    
293
                    // if retry is true, try one more time if we miss 
294
                   if ( (pos[0] !== null && newXY[0] != pos[0]) || 
295
                        (pos[1] !== null && newXY[1] != pos[1]) ) {
296
                       this.setXY(el, pos, true);
297
                   }
298
                }        
299
        
300
            };
301
            
302
            Y.Dom.batch(el, f, Y.Dom, true);
303
        },
304
        
305
        /**
306
         * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
307
         * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
308
         * @method setX
309
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
310
         * @param {Int} x The value to use as the X coordinate for the element(s).
311
         */
312
        setX: function(el, x) {
313
            Y.Dom.setXY(el, [x, null]);
314
        },
315
        
316
        /**
317
         * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
318
         * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
319
         * @method setY
320
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
321
         * @param {Int} x To use as the Y coordinate for the element(s).
322
         */
323
        setY: function(el, y) {
324
            Y.Dom.setXY(el, [null, y]);
325
        },
326
        
327
        /**
328
         * Returns the region position of the given element.
329
         * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
330
         * @method getRegion
331
         * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
332
         * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
333
         */
334
        getRegion: function(el) {
335
            var f = function(el) {
336
                if ( (el.parentNode === null || el.offsetParent === null ||
337
                        this.getStyle(el, 'display') == 'none') && el != document.body) {
338
                    return false;
339
                }
340

    
341
                var region = Y.Region.getRegion(el);
342
                return region;
343
            };
344
            
345
            return Y.Dom.batch(el, f, Y.Dom, true);
346
        },
347
        
348
        /**
349
         * Returns the width of the client (viewport).
350
         * @method getClientWidth
351
         * @deprecated Now using getViewportWidth.  This interface left intact for back compat.
352
         * @return {Int} The width of the viewable area of the page.
353
         */
354
        getClientWidth: function() {
355
            return Y.Dom.getViewportWidth();
356
        },
357
        
358
        /**
359
         * Returns the height of the client (viewport).
360
         * @method getClientHeight
361
         * @deprecated Now using getViewportHeight.  This interface left intact for back compat.
362
         * @return {Int} The height of the viewable area of the page.
363
         */
364
        getClientHeight: function() {
365
            return Y.Dom.getViewportHeight();
366
        },
367

    
368
        /**
369
         * Returns a array of HTMLElements with the given class.
370
         * For optimized performance, include a tag and/or root node when possible.
371
         * @method getElementsByClassName
372
         * @param {String} className The class name to match against
373
         * @param {String} tag (optional) The tag name of the elements being collected
374
         * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point 
375
         * @param {Function} apply (optional) A function to apply to each element when found 
376
         * @return {Array} An array of elements that have the given class name
377
         */
378
        getElementsByClassName: function(className, tag, root, apply) {
379
            tag = tag || '*';
380
            root = (root) ? Y.Dom.get(root) : null || document; 
381
            if (!root) {
382
                return [];
383
            }
384

    
385
            var nodes = [],
386
                elements = root.getElementsByTagName(tag),
387
                re = getClassRegEx(className);
388

    
389
            for (var i = 0, len = elements.length; i < len; ++i) {
390
                if ( re.test(elements[i].className) ) {
391
                    nodes[nodes.length] = elements[i];
392
                    if (apply) {
393
                        apply.call(elements[i], elements[i]);
394
                    }
395
                }
396
            }
397
            
398
            return nodes;
399
        },
400

    
401
        /**
402
         * Determines whether an HTMLElement has the given className.
403
         * @method hasClass
404
         * @param {String | HTMLElement | Array} el The element or collection to test
405
         * @param {String} className the class name to search for
406
         * @return {Boolean | Array} A boolean value or array of boolean values
407
         */
408
        hasClass: function(el, className) {
409
            var re = getClassRegEx(className);
410

    
411
            var f = function(el) {
412
                return re.test(el.className);
413
            };
414
            
415
            return Y.Dom.batch(el, f, Y.Dom, true);
416
        },
417
    
418
        /**
419
         * Adds a class name to a given element or collection of elements.
420
         * @method addClass         
421
         * @param {String | HTMLElement | Array} el The element or collection to add the class to
422
         * @param {String} className the class name to add to the class attribute
423
         * @return {Boolean | Array} A pass/fail boolean or array of booleans
424
         */
425
        addClass: function(el, className) {
426
            var f = function(el) {
427
                if (this.hasClass(el, className)) {
428
                    return false; // already present
429
                }
430
                
431
                
432
                el.className = YAHOO.lang.trim([el.className, className].join(' '));
433
                return true;
434
            };
435
            
436
            return Y.Dom.batch(el, f, Y.Dom, true);
437
        },
438
    
439
        /**
440
         * Removes a class name from a given element or collection of elements.
441
         * @method removeClass         
442
         * @param {String | HTMLElement | Array} el The element or collection to remove the class from
443
         * @param {String} className the class name to remove from the class attribute
444
         * @return {Boolean | Array} A pass/fail boolean or array of booleans
445
         */
446
        removeClass: function(el, className) {
447
            var re = getClassRegEx(className);
448
            
449
            var f = function(el) {
450
                if (!this.hasClass(el, className)) {
451
                    return false; // not present
452
                }                 
453

    
454
                
455
                var c = el.className;
456
                el.className = c.replace(re, ' ');
457
                if ( this.hasClass(el, className) ) { // in case of multiple adjacent
458
                    this.removeClass(el, className);
459
                }
460

    
461
                el.className = YAHOO.lang.trim(el.className); // remove any trailing spaces
462
                return true;
463
            };
464
            
465
            return Y.Dom.batch(el, f, Y.Dom, true);
466
        },
467
        
468
        /**
469
         * Replace a class with another class for a given element or collection of elements.
470
         * If no oldClassName is present, the newClassName is simply added.
471
         * @method replaceClass  
472
         * @param {String | HTMLElement | Array} el The element or collection to remove the class from
473
         * @param {String} oldClassName the class name to be replaced
474
         * @param {String} newClassName the class name that will be replacing the old class name
475
         * @return {Boolean | Array} A pass/fail boolean or array of booleans
476
         */
477
        replaceClass: function(el, oldClassName, newClassName) {
478
            if (!newClassName || oldClassName === newClassName) { // avoid infinite loop
479
                return false;
480
            }
481
            
482
            var re = getClassRegEx(oldClassName);
483

    
484
            var f = function(el) {
485
            
486
                if ( !this.hasClass(el, oldClassName) ) {
487
                    this.addClass(el, newClassName); // just add it if nothing to replace
488
                    return true; // NOTE: return
489
                }
490
            
491
                el.className = el.className.replace(re, ' ' + newClassName + ' ');
492

    
493
                if ( this.hasClass(el, oldClassName) ) { // in case of multiple adjacent
494
                    this.replaceClass(el, oldClassName, newClassName);
495
                }
496

    
497
                el.className = YAHOO.lang.trim(el.className); // remove any trailing spaces
498
                return true;
499
            };
500
            
501
            return Y.Dom.batch(el, f, Y.Dom, true);
502
        },
503
        
504
        /**
505
         * Returns an ID and applies it to the element "el", if provided.
506
         * @method generateId  
507
         * @param {String | HTMLElement | Array} el (optional) An optional element array of elements to add an ID to (no ID is added if one is already present).
508
         * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
509
         * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
510
         */
511
        generateId: function(el, prefix) {
512
            prefix = prefix || 'yui-gen';
513

    
514
            var f = function(el) {
515
                if (el && el.id) { // do not override existing ID
516
                    return el.id;
517
                } 
518

    
519
                var id = prefix + id_counter++;
520

    
521
                if (el) {
522
                    el.id = id;
523
                }
524
                
525
                return id;
526
            };
527

    
528
            // batch fails when no element, so just generate and return single ID
529
            return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
530
        },
531
        
532
        /**
533
         * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
534
         * @method isAncestor
535
         * @param {String | HTMLElement} haystack The possible ancestor
536
         * @param {String | HTMLElement} needle The possible descendent
537
         * @return {Boolean} Whether or not the haystack is an ancestor of needle
538
         */
539
        isAncestor: function(haystack, needle) {
540
            haystack = Y.Dom.get(haystack);
541
            needle = Y.Dom.get(needle);
542
            
543
            if (!haystack || !needle) {
544
                return false;
545
            }
546

    
547
            if (haystack.contains && needle.nodeType && !isSafari) { // safari contains is broken
548
                return haystack.contains(needle);
549
            }
550
            else if ( haystack.compareDocumentPosition && needle.nodeType ) {
551
                return !!(haystack.compareDocumentPosition(needle) & 16);
552
            } else if (needle.nodeType) {
553
                // fallback to crawling up (safari)
554
                return !!this.getAncestorBy(needle, function(el) {
555
                    return el == haystack; 
556
                }); 
557
            }
558
            return false;
559
        },
560
        
561
        /**
562
         * Determines whether an HTMLElement is present in the current document.
563
         * @method inDocument         
564
         * @param {String | HTMLElement} el The element to search for
565
         * @return {Boolean} Whether or not the element is present in the current document
566
         */
567
        inDocument: function(el) {
568
            return this.isAncestor(document.documentElement, el);
569
        },
570
        
571
        /**
572
         * Returns a array of HTMLElements that pass the test applied by supplied boolean method.
573
         * For optimized performance, include a tag and/or root node when possible.
574
         * @method getElementsBy
575
         * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
576
         * @param {String} tag (optional) The tag name of the elements being collected
577
         * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point 
578
         * @param {Function} apply (optional) A function to apply to each element when found 
579
         * @return {Array} Array of HTMLElements
580
         */
581
        getElementsBy: function(method, tag, root, apply) {
582
            tag = tag || '*';
583
            root = (root) ? Y.Dom.get(root) : null || document; 
584

    
585
            if (!root) {
586
                return [];
587
            }
588

    
589
            var nodes = [],
590
                elements = root.getElementsByTagName(tag);
591
            
592
            for (var i = 0, len = elements.length; i < len; ++i) {
593
                if ( method(elements[i]) ) {
594
                    nodes[nodes.length] = elements[i];
595
                    if (apply) {
596
                        apply(elements[i]);
597
                    }
598
                }
599
            }
600

    
601
            
602
            return nodes;
603
        },
604
        
605
        /**
606
         * Runs the supplied method against each item in the Collection/Array.
607
         * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
608
         * @method batch
609
         * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
610
         * @param {Function} method The method to apply to the element(s)
611
         * @param {Any} o (optional) An optional arg that is passed to the supplied method
612
         * @param {Boolean} override (optional) Whether or not to override the scope of "method" with "o"
613
         * @return {Any | Array} The return value(s) from the supplied method
614
         */
615
        batch: function(el, method, o, override) {
616
            el = (el && (el.tagName || el.item)) ? el : Y.Dom.get(el); // skip get() when possible
617

    
618
            if (!el || !method) {
619
                return false;
620
            } 
621
            var scope = (override) ? o : window;
622
            
623
            if (el.tagName || el.length === undefined) { // element or not array-like 
624
                return method.call(scope, el, o);
625
            } 
626

    
627
            var collection = [];
628
            
629
            for (var i = 0, len = el.length; i < len; ++i) {
630
                collection[collection.length] = method.call(scope, el[i], o);
631
            }
632
            
633
            return collection;
634
        },
635
        
636
        /**
637
         * Returns the height of the document.
638
         * @method getDocumentHeight
639
         * @return {Int} The height of the actual document (which includes the body and its margin).
640
         */
641
        getDocumentHeight: function() {
642
            var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;
643

    
644
            var h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
645
            return h;
646
        },
647
        
648
        /**
649
         * Returns the width of the document.
650
         * @method getDocumentWidth
651
         * @return {Int} The width of the actual document (which includes the body and its margin).
652
         */
653
        getDocumentWidth: function() {
654
            var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
655
            var w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
656
            return w;
657
        },
658

    
659
        /**
660
         * Returns the current height of the viewport.
661
         * @method getViewportHeight
662
         * @return {Int} The height of the viewable area of the page (excludes scrollbars).
663
         */
664
        getViewportHeight: function() {
665
            var height = self.innerHeight; // Safari, Opera
666
            var mode = document.compatMode;
667
        
668
            if ( (mode || isIE) && !isOpera ) { // IE, Gecko
669
                height = (mode == 'CSS1Compat') ?
670
                        document.documentElement.clientHeight : // Standards
671
                        document.body.clientHeight; // Quirks
672
            }
673
        
674
            return height;
675
        },
676
        
677
        /**
678
         * Returns the current width of the viewport.
679
         * @method getViewportWidth
680
         * @return {Int} The width of the viewable area of the page (excludes scrollbars).
681
         */
682
        
683
        getViewportWidth: function() {
684
            var width = self.innerWidth;  // Safari
685
            var mode = document.compatMode;
686
            
687
            if (mode || isIE) { // IE, Gecko, Opera
688
                width = (mode == 'CSS1Compat') ?
689
                        document.documentElement.clientWidth : // Standards
690
                        document.body.clientWidth; // Quirks
691
            }
692
            return width;
693
        },
694

    
695
       /**
696
         * Returns the nearest ancestor that passes the test applied by supplied boolean method.
697
         * For performance reasons, IDs are not accepted and argument validation omitted.
698
         * @method getAncestorBy
699
         * @param {HTMLElement} node The HTMLElement to use as the starting point 
700
         * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
701
         * @return {Object} HTMLElement or null if not found
702
         */
703
        getAncestorBy: function(node, method) {
704
            while (node = node.parentNode) { // NOTE: assignment
705
                if ( testElement(node, method) ) {
706
                    return node;
707
                }
708
            } 
709

    
710
            return null;
711
        },
712
        
713
        /**
714
         * Returns the nearest ancestor with the given className.
715
         * @method getAncestorByClassName
716
         * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point 
717
         * @param {String} className
718
         * @return {Object} HTMLElement
719
         */
720
        getAncestorByClassName: function(node, className) {
721
            node = Y.Dom.get(node);
722
            if (!node) {
723
                return null;
724
            }
725
            var method = function(el) { return Y.Dom.hasClass(el, className); };
726
            return Y.Dom.getAncestorBy(node, method);
727
        },
728

    
729
        /**
730
         * Returns the nearest ancestor with the given tagName.
731
         * @method getAncestorByTagName
732
         * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point 
733
         * @param {String} tagName
734
         * @return {Object} HTMLElement
735
         */
736
        getAncestorByTagName: function(node, tagName) {
737
            node = Y.Dom.get(node);
738
            if (!node) {
739
                return null;
740
            }
741
            var method = function(el) {
742
                 return el.tagName && el.tagName.toUpperCase() == tagName.toUpperCase();
743
            };
744

    
745
            return Y.Dom.getAncestorBy(node, method);
746
        },
747

    
748
        /**
749
         * Returns the previous sibling that is an HTMLElement. 
750
         * For performance reasons, IDs are not accepted and argument validation omitted.
751
         * Returns the nearest HTMLElement sibling if no method provided.
752
         * @method getPreviousSiblingBy
753
         * @param {HTMLElement} node The HTMLElement to use as the starting point 
754
         * @param {Function} method A boolean function used to test siblings
755
         * that receives the sibling node being tested as its only argument
756
         * @return {Object} HTMLElement or null if not found
757
         */
758
        getPreviousSiblingBy: function(node, method) {
759
            while (node) {
760
                node = node.previousSibling;
761
                if ( testElement(node, method) ) {
762
                    return node;
763
                }
764
            }
765
            return null;
766
        }, 
767

    
768
        /**
769
         * Returns the previous sibling that is an HTMLElement 
770
         * @method getPreviousSibling
771
         * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point 
772
         * @return {Object} HTMLElement or null if not found
773
         */
774
        getPreviousSibling: function(node) {
775
            node = Y.Dom.get(node);
776
            if (!node) {
777
                return null;
778
            }
779

    
780
            return Y.Dom.getPreviousSiblingBy(node);
781
        }, 
782

    
783
        /**
784
         * Returns the next HTMLElement sibling that passes the boolean method. 
785
         * For performance reasons, IDs are not accepted and argument validation omitted.
786
         * Returns the nearest HTMLElement sibling if no method provided.
787
         * @method getNextSiblingBy
788
         * @param {HTMLElement} node The HTMLElement to use as the starting point 
789
         * @param {Function} method A boolean function used to test siblings
790
         * that receives the sibling node being tested as its only argument
791
         * @return {Object} HTMLElement or null if not found
792
         */
793
        getNextSiblingBy: function(node, method) {
794
            while (node) {
795
                node = node.nextSibling;
796
                if ( testElement(node, method) ) {
797
                    return node;
798
                }
799
            }
800
            return null;
801
        }, 
802

    
803
        /**
804
         * Returns the next sibling that is an HTMLElement 
805
         * @method getNextSibling
806
         * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point 
807
         * @return {Object} HTMLElement or null if not found
808
         */
809
        getNextSibling: function(node) {
810
            node = Y.Dom.get(node);
811
            if (!node) {
812
                return null;
813
            }
814

    
815
            return Y.Dom.getNextSiblingBy(node);
816
        }, 
817

    
818
        /**
819
         * Returns the first HTMLElement child that passes the test method. 
820
         * @method getFirstChildBy
821
         * @param {HTMLElement} node The HTMLElement to use as the starting point 
822
         * @param {Function} method A boolean function used to test children
823
         * that receives the node being tested as its only argument
824
         * @return {Object} HTMLElement or null if not found
825
         */
826
        getFirstChildBy: function(node, method) {
827
            var child = ( testElement(node.firstChild, method) ) ? node.firstChild : null;
828
            return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
829
        }, 
830

    
831
        /**
832
         * Returns the first HTMLElement child. 
833
         * @method getFirstChild
834
         * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point 
835
         * @return {Object} HTMLElement or null if not found
836
         */
837
        getFirstChild: function(node, method) {
838
            node = Y.Dom.get(node);
839
            if (!node) {
840
                return null;
841
            }
842
            return Y.Dom.getFirstChildBy(node);
843
        }, 
844

    
845
        /**
846
         * Returns the last HTMLElement child that passes the test method. 
847
         * @method getLastChildBy
848
         * @param {HTMLElement} node The HTMLElement to use as the starting point 
849
         * @param {Function} method A boolean function used to test children
850
         * that receives the node being tested as its only argument
851
         * @return {Object} HTMLElement or null if not found
852
         */
853
        getLastChildBy: function(node, method) {
854
            if (!node) {
855
                return null;
856
            }
857
            var child = ( testElement(node.lastChild, method) ) ? node.lastChild : null;
858
            return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
859
        }, 
860

    
861
        /**
862
         * Returns the last HTMLElement child. 
863
         * @method getLastChild
864
         * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point 
865
         * @return {Object} HTMLElement or null if not found
866
         */
867
        getLastChild: function(node) {
868
            node = Y.Dom.get(node);
869
            return Y.Dom.getLastChildBy(node);
870
        }, 
871

    
872
        /**
873
         * Returns an array of HTMLElement childNodes that pass the test method. 
874
         * @method getChildrenBy
875
         * @param {HTMLElement} node The HTMLElement to start from
876
         * @param {Function} method A boolean function used to test children
877
         * that receives the node being tested as its only argument
878
         * @return {Array} A static array of HTMLElements
879
         */
880
        getChildrenBy: function(node, method) {
881
            var child = Y.Dom.getFirstChildBy(node, method);
882
            var children = child ? [child] : [];
883

    
884
            Y.Dom.getNextSiblingBy(child, function(node) {
885
                if ( !method || method(node) ) {
886
                    children[children.length] = node;
887
                }
888
                return false; // fail test to collect all children
889
            });
890

    
891
            return children;
892
        },
893
 
894
        /**
895
         * Returns an array of HTMLElement childNodes. 
896
         * @method getChildren
897
         * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point 
898
         * @return {Array} A static array of HTMLElements
899
         */
900
        getChildren: function(node) {
901
            node = Y.Dom.get(node);
902
            if (!node) {
903
            }
904

    
905
            return Y.Dom.getChildrenBy(node);
906
        },
907
 
908
        /**
909
         * Returns the left scroll value of the document 
910
         * @method getDocumentScrollLeft
911
         * @param {HTMLDocument} document (optional) The document to get the scroll value of
912
         * @return {Int}  The amount that the document is scrolled to the left
913
         */
914
        getDocumentScrollLeft: function(doc) {
915
            doc = doc || document;
916
            return Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
917
        }, 
918

    
919
        /**
920
         * Returns the top scroll value of the document 
921
         * @method getDocumentScrollTop
922
         * @param {HTMLDocument} document (optional) The document to get the scroll value of
923
         * @return {Int}  The amount that the document is scrolled to the top
924
         */
925
        getDocumentScrollTop: function(doc) {
926
            doc = doc || document;
927
            return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
928
        },
929

    
930
        /**
931
         * Inserts the new node as the previous sibling of the reference node 
932
         * @method insertBefore
933
         * @param {String | HTMLElement} newNode The node to be inserted
934
         * @param {String | HTMLElement} referenceNode The node to insert the new node before 
935
         * @return {HTMLElement} The node that was inserted (or null if insert fails) 
936
         */
937
        insertBefore: function(newNode, referenceNode) {
938
            newNode = Y.Dom.get(newNode); 
939
            referenceNode = Y.Dom.get(referenceNode); 
940
            
941
            if (!newNode || !referenceNode || !referenceNode.parentNode) {
942
                return null;
943
            }       
944

    
945
            return referenceNode.parentNode.insertBefore(newNode, referenceNode); 
946
        },
947

    
948
        /**
949
         * Inserts the new node as the next sibling of the reference node 
950
         * @method insertAfter
951
         * @param {String | HTMLElement} newNode The node to be inserted
952
         * @param {String | HTMLElement} referenceNode The node to insert the new node after 
953
         * @return {HTMLElement} The node that was inserted (or null if insert fails) 
954
         */
955
        insertAfter: function(newNode, referenceNode) {
956
            newNode = Y.Dom.get(newNode); 
957
            referenceNode = Y.Dom.get(referenceNode); 
958
            
959
            if (!newNode || !referenceNode || !referenceNode.parentNode) {
960
                return null;
961
            }       
962

    
963
            if (referenceNode.nextSibling) {
964
                return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); 
965
            } else {
966
                return referenceNode.parentNode.appendChild(newNode);
967
            }
968
        },
969

    
970
        /**
971
         * Creates a Region based on the viewport relative to the document. 
972
         * @method getClientRegion
973
         * @return {Region} A Region object representing the viewport which accounts for document scroll
974
         */
975
        getClientRegion: function() {
976
            var t = Y.Dom.getDocumentScrollTop(),
977
                l = Y.Dom.getDocumentScrollLeft(),
978
                r = Y.Dom.getViewportWidth() + l,
979
                b = Y.Dom.getViewportHeight() + t;
980

    
981
            return new Y.Region(t, r, b, l);
982
        }
983
    };
984
    
985
    var getXY = function() {
986
        if (document.documentElement.getBoundingClientRect) { // IE
987
            return function(el) {
988
                var box = el.getBoundingClientRect();
989

    
990
                var rootNode = el.ownerDocument;
991
                return [box.left + Y.Dom.getDocumentScrollLeft(rootNode), box.top +
992
                        Y.Dom.getDocumentScrollTop(rootNode)];
993
            };
994
        } else {
995
            return function(el) { // manually calculate by crawling up offsetParents
996
                var pos = [el.offsetLeft, el.offsetTop];
997
                var parentNode = el.offsetParent;
998

    
999
                // safari: subtract body offsets if el is abs (or any offsetParent), unless body is offsetParent
1000
                var accountForBody = (isSafari &&
1001
                        Y.Dom.getStyle(el, 'position') == 'absolute' &&
1002
                        el.offsetParent == el.ownerDocument.body);
1003

    
1004
                if (parentNode != el) {
1005
                    while (parentNode) {
1006
                        pos[0] += parentNode.offsetLeft;
1007
                        pos[1] += parentNode.offsetTop;
1008
                        if (!accountForBody && isSafari && 
1009
                                Y.Dom.getStyle(parentNode,'position') == 'absolute' ) { 
1010
                            accountForBody = true;
1011
                        }
1012
                        parentNode = parentNode.offsetParent;
1013
                    }
1014
                }
1015

    
1016
                if (accountForBody) { //safari doubles in this case
1017
                    pos[0] -= el.ownerDocument.body.offsetLeft;
1018
                    pos[1] -= el.ownerDocument.body.offsetTop;
1019
                } 
1020
                parentNode = el.parentNode;
1021

    
1022
                // account for any scrolled ancestors
1023
                while ( parentNode.tagName && !patterns.ROOT_TAG.test(parentNode.tagName) ) 
1024
                {
1025
                   // work around opera inline/table scrollLeft/Top bug
1026
                   if (Y.Dom.getStyle(parentNode, 'display').search(/^inline|table-row.*$/i)) { 
1027
                        pos[0] -= parentNode.scrollLeft;
1028
                        pos[1] -= parentNode.scrollTop;
1029
                    }
1030
                    
1031
                    parentNode = parentNode.parentNode; 
1032
                }
1033

    
1034
                return pos;
1035
            };
1036
        }
1037
    }() // NOTE: Executing for loadtime branching
1038
})();
1039
/**
1040
 * A region is a representation of an object on a grid.  It is defined
1041
 * by the top, right, bottom, left extents, so is rectangular by default.  If 
1042
 * other shapes are required, this class could be extended to support it.
1043
 * @namespace YAHOO.util
1044
 * @class Region
1045
 * @param {Int} t the top extent
1046
 * @param {Int} r the right extent
1047
 * @param {Int} b the bottom extent
1048
 * @param {Int} l the left extent
1049
 * @constructor
1050
 */
1051
YAHOO.util.Region = function(t, r, b, l) {
1052

    
1053
    /**
1054
     * The region's top extent
1055
     * @property top
1056
     * @type Int
1057
     */
1058
    this.top = t;
1059
    
1060
    /**
1061
     * The region's top extent as index, for symmetry with set/getXY
1062
     * @property 1
1063
     * @type Int
1064
     */
1065
    this[1] = t;
1066

    
1067
    /**
1068
     * The region's right extent
1069
     * @property right
1070
     * @type int
1071
     */
1072
    this.right = r;
1073

    
1074
    /**
1075
     * The region's bottom extent
1076
     * @property bottom
1077
     * @type Int
1078
     */
1079
    this.bottom = b;
1080

    
1081
    /**
1082
     * The region's left extent
1083
     * @property left
1084
     * @type Int
1085
     */
1086
    this.left = l;
1087
    
1088
    /**
1089
     * The region's left extent as index, for symmetry with set/getXY
1090
     * @property 0
1091
     * @type Int
1092
     */
1093
    this[0] = l;
1094
};
1095

    
1096
/**
1097
 * Returns true if this region contains the region passed in
1098
 * @method contains
1099
 * @param  {Region}  region The region to evaluate
1100
 * @return {Boolean}        True if the region is contained with this region, 
1101
 *                          else false
1102
 */
1103
YAHOO.util.Region.prototype.contains = function(region) {
1104
    return ( region.left   >= this.left   && 
1105
             region.right  <= this.right  && 
1106
             region.top    >= this.top    && 
1107
             region.bottom <= this.bottom    );
1108

    
1109
};
1110

    
1111
/**
1112
 * Returns the area of the region
1113
 * @method getArea
1114
 * @return {Int} the region's area
1115
 */
1116
YAHOO.util.Region.prototype.getArea = function() {
1117
    return ( (this.bottom - this.top) * (this.right - this.left) );
1118
};
1119

    
1120
/**
1121
 * Returns the region where the passed in region overlaps with this one
1122
 * @method intersect
1123
 * @param  {Region} region The region that intersects
1124
 * @return {Region}        The overlap region, or null if there is no overlap
1125
 */
1126
YAHOO.util.Region.prototype.intersect = function(region) {
1127
    var t = Math.max( this.top,    region.top    );
1128
    var r = Math.min( this.right,  region.right  );
1129
    var b = Math.min( this.bottom, region.bottom );
1130
    var l = Math.max( this.left,   region.left   );
1131
    
1132
    if (b >= t && r >= l) {
1133
        return new YAHOO.util.Region(t, r, b, l);
1134
    } else {
1135
        return null;
1136
    }
1137
};
1138

    
1139
/**
1140
 * Returns the region representing the smallest region that can contain both
1141
 * the passed in region and this region.
1142
 * @method union
1143
 * @param  {Region} region The region that to create the union with
1144
 * @return {Region}        The union region
1145
 */
1146
YAHOO.util.Region.prototype.union = function(region) {
1147
    var t = Math.min( this.top,    region.top    );
1148
    var r = Math.max( this.right,  region.right  );
1149
    var b = Math.max( this.bottom, region.bottom );
1150
    var l = Math.min( this.left,   region.left   );
1151

    
1152
    return new YAHOO.util.Region(t, r, b, l);
1153
};
1154

    
1155
/**
1156
 * toString
1157
 * @method toString
1158
 * @return string the region properties
1159
 */
1160
YAHOO.util.Region.prototype.toString = function() {
1161
    return ( "Region {"    +
1162
             "top: "       + this.top    + 
1163
             ", right: "   + this.right  + 
1164
             ", bottom: "  + this.bottom + 
1165
             ", left: "    + this.left   + 
1166
             "}" );
1167
};
1168

    
1169
/**
1170
 * Returns a region that is occupied by the DOM element
1171
 * @method getRegion
1172
 * @param  {HTMLElement} el The element
1173
 * @return {Region}         The region that the element occupies
1174
 * @static
1175
 */
1176
YAHOO.util.Region.getRegion = function(el) {
1177
    var p = YAHOO.util.Dom.getXY(el);
1178

    
1179
    var t = p[1];
1180
    var r = p[0] + el.offsetWidth;
1181
    var b = p[1] + el.offsetHeight;
1182
    var l = p[0];
1183

    
1184
    return new YAHOO.util.Region(t, r, b, l);
1185
};
1186

    
1187
/////////////////////////////////////////////////////////////////////////////
1188

    
1189

    
1190
/**
1191
 * A point is a region that is special in that it represents a single point on 
1192
 * the grid.
1193
 * @namespace YAHOO.util
1194
 * @class Point
1195
 * @param {Int} x The X position of the point
1196
 * @param {Int} y The Y position of the point
1197
 * @constructor
1198
 * @extends YAHOO.util.Region
1199
 */
1200
YAHOO.util.Point = function(x, y) {
1201
   if (YAHOO.lang.isArray(x)) { // accept input from Dom.getXY, Event.getXY, etc.
1202
      y = x[1]; // dont blow away x yet
1203
      x = x[0];
1204
   }
1205
   
1206
    /**
1207
     * The X position of the point, which is also the right, left and index zero (for Dom.getXY symmetry)
1208
     * @property x
1209
     * @type Int
1210
     */
1211

    
1212
    this.x = this.right = this.left = this[0] = x;
1213
     
1214
    /**
1215
     * The Y position of the point, which is also the top, bottom and index one (for Dom.getXY symmetry)
1216
     * @property y
1217
     * @type Int
1218
     */
1219
    this.y = this.top = this.bottom = this[1] = y;
1220
};
1221

    
1222
YAHOO.util.Point.prototype = new YAHOO.util.Region();
1223

    
1224
YAHOO.register("dom", YAHOO.util.Dom, {version: "2.4.1", build: "742"});
(4-4/4)