2 Copyright (c) 2007, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * The dom module provides helper methods for manipulating Dom elements.
14 var Y
= YAHOO
.util
, // internal shorthand
15 getStyle
, // for load time browser branching
17 id_counter
= 0, // for use with generateId
18 propertyCache
= {}, // for faster hyphen converts
19 reClassNameCache
= {}; // cache regexes for className
22 var isOpera
= YAHOO
.env
.ua
.opera
,
23 isSafari
= YAHOO
.env
.ua
.webkit
,
24 isGecko
= YAHOO
.env
.ua
.gecko
,
25 isIE
= YAHOO
.env
.ua
.ie
;
29 HYPHEN
: /(-[a-z])/i, // to normalize get/setStyle
30 ROOT_TAG
: /^body|html$/i // body for quirks mode, html for standards
33 var toCamel = function(property
) {
34 if ( !patterns
.HYPHEN
.test(property
) ) {
35 return property
; // no hyphens
38 if (propertyCache
[property
]) { // already converted
39 return propertyCache
[property
];
42 var converted
= property
;
44 while( patterns
.HYPHEN
.exec(converted
) ) {
45 converted
= converted
.replace(RegExp
.$1,
46 RegExp
.$1.substr(1).toUpperCase());
49 propertyCache
[property
] = converted
;
51 //return property.replace(/-([a-z])/gi, function(m0, m1) {return m1.toUpperCase()}) // cant use function as 2nd arg yet due to safari bug
54 var getClassRegEx = function(className
) {
55 var re
= reClassNameCache
[className
];
57 re
= new RegExp('(?:^|\\s+)' + className
+ '(?:\\s+|$)');
58 reClassNameCache
[className
] = re
;
63 // branching at load instead of runtime
64 if (document
.defaultView
&& document
.defaultView
.getComputedStyle
) { // W3C DOM method
65 getStyle = function(el
, property
) {
68 if (property
== 'float') { // fix reserved word
69 property
= 'cssFloat';
72 var computed
= document
.defaultView
.getComputedStyle(el
, '');
73 if (computed
) { // test computed before touching for safari
74 value
= computed
[toCamel(property
)];
77 return el
.style
[property
] || value
;
79 } else if (document
.documentElement
.currentStyle
&& isIE
) { // IE method
80 getStyle = function(el
, property
) {
81 switch( toCamel(property
) ) {
82 case 'opacity' :// IE opacity uses filter
84 try { // will error if no DXImageTransform
85 val
= el
.filters
['DXImageTransform.Microsoft.Alpha'].opacity
;
88 try { // make sure its in the document
89 val
= el
.filters('alpha').opacity
;
94 case 'float': // fix reserved word
95 property
= 'styleFloat'; // fall through
97 // test currentStyle before touching
98 var value
= el
.currentStyle
? el
.currentStyle
[property
] : null;
99 return ( el
.style
[property
] || value
);
102 } else { // default to inline only
103 getStyle = function(el
, property
) { return el
.style
[property
]; };
107 setStyle = function(el
, property
, val
) {
110 if ( YAHOO
.lang
.isString(el
.style
.filter
) ) { // in case not appended
111 el
.style
.filter
= 'alpha(opacity=' + val
* 100 + ')';
113 if (!el
.currentStyle
|| !el
.currentStyle
.hasLayout
) {
114 el
.style
.zoom
= 1; // when no layout or cant tell
119 property
= 'styleFloat';
121 el
.style
[property
] = val
;
125 setStyle = function(el
, property
, val
) {
126 if (property
== 'float') {
127 property
= 'cssFloat';
129 el
.style
[property
] = val
;
133 var testElement = function(node
, method
) {
134 return node
&& node
.nodeType
== 1 && ( !method
|| method(node
) );
138 * Provides helper methods for DOM elements.
139 * @namespace YAHOO.util
144 * Returns an HTMLElement reference.
146 * @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.
147 * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
150 if (!el
|| el
.tagName
|| el
.item
) { // null, HTMLElement, or HTMLCollection
154 if (YAHOO
.lang
.isString(el
)) { // HTMLElement or null
155 return document
.getElementById(el
);
158 if (el
.splice
) { // Array of HTMLElements/IDs
160 for (var i
= 0, len
= el
.length
; i
< len
; ++i
) {
161 c
[c
.length
] = Y
.Dom
.get(el
[i
]);
167 return el
; // some other object, just pass it back
171 * Normalizes currentStyle and ComputedStyle.
173 * @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.
174 * @param {String} property The style property whose value is returned.
175 * @return {String | Array} The current value of the style property for the element(s).
177 getStyle: function(el
, property
) {
178 property
= toCamel(property
);
180 var f = function(element
) {
181 return getStyle(element
, property
);
184 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
188 * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
190 * @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.
191 * @param {String} property The style property to be set.
192 * @param {String} val The value to apply to the given property.
194 setStyle: function(el
, property
, val
) {
195 property
= toCamel(property
);
197 var f = function(element
) {
198 setStyle(element
, property
, val
);
202 Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
206 * 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 * @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
209 * @return {Array} The XY position of the element(s)
211 getXY: function(el
) {
212 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
!= document
.body
) {
220 var parentNode
= null;
223 var doc
= el
.ownerDocument
;
225 if (el
.getBoundingClientRect
) { // IE
226 box
= el
.getBoundingClientRect();
227 return [box
.left
+ Y
.Dom
.getDocumentScrollLeft(el
.ownerDocument
), box
.top
+ Y
.Dom
.getDocumentScrollTop(el
.ownerDocument
)];
229 else { // safari, opera, & gecko
230 pos
= [el
.offsetLeft
, el
.offsetTop
];
231 parentNode
= el
.offsetParent
;
233 // safari: if el is abs or any parent is abs, subtract body offsets
234 var hasAbs
= this.getStyle(el
, 'position') == 'absolute';
236 if (parentNode
!= el
) {
238 pos
[0] += parentNode
.offsetLeft
;
239 pos
[1] += parentNode
.offsetTop
;
240 if (isSafari
&& !hasAbs
&&
241 this.getStyle(parentNode
,'position') == 'absolute' ) {
242 hasAbs
= true; // we need to offset if any parent is absolutely positioned
244 parentNode
= parentNode
.offsetParent
;
248 if (isSafari
&& hasAbs
) { //safari doubles in this case
249 pos
[0] -= el
.ownerDocument
.body
.offsetLeft
;
250 pos
[1] -= el
.ownerDocument
.body
.offsetTop
;
254 parentNode
= el
.parentNode
;
256 // account for any scrolled ancestors
257 while ( parentNode
.tagName
&& !patterns
.ROOT_TAG
.test(parentNode
.tagName
) )
259 // work around opera inline/table scrollLeft/Top bug
260 if (Y
.Dom
.getStyle(parentNode
, 'display').search(/^inline|table-row.*$/i)) {
261 pos
[0] -= parentNode
.scrollLeft
;
262 pos
[1] -= parentNode
.scrollTop
;
265 parentNode
= parentNode
.parentNode
;
272 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
276 * 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).
278 * @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
279 * @return {Number | Array} The X position of the element(s)
282 var f = function(el
) {
283 return Y
.Dom
.getXY(el
)[0];
286 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
290 * 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).
292 * @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
293 * @return {Number | Array} The Y position of the element(s)
296 var f = function(el
) {
297 return Y
.Dom
.getXY(el
)[1];
300 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
304 * Set the position of an html element in page coordinates, regardless of how the element is positioned.
305 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
307 * @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
308 * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
309 * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
311 setXY: function(el
, pos
, noRetry
) {
312 var f = function(el
) {
313 var style_pos
= this.getStyle(el
, 'position');
314 if (style_pos
== 'static') { // default to relative
315 this.setStyle(el
, 'position', 'relative');
316 style_pos
= 'relative';
319 var pageXY
= this.getXY(el
);
320 if (pageXY
=== false) { // has to be part of doc to have pageXY
324 var delta
= [ // assuming pixels; if not we will have to retry
325 parseInt( this.getStyle(el
, 'left'), 10 ),
326 parseInt( this.getStyle(el
, 'top'), 10 )
329 if ( isNaN(delta
[0]) ) {// in case of 'auto'
330 delta
[0] = (style_pos
== 'relative') ? 0 : el
.offsetLeft
;
332 if ( isNaN(delta
[1]) ) { // in case of 'auto'
333 delta
[1] = (style_pos
== 'relative') ? 0 : el
.offsetTop
;
336 if (pos
[0] !== null) { el
.style
.left
= pos
[0] - pageXY
[0] + delta
[0] + 'px'; }
337 if (pos
[1] !== null) { el
.style
.top
= pos
[1] - pageXY
[1] + delta
[1] + 'px'; }
340 var newXY
= this.getXY(el
);
342 // if retry is true, try one more time if we miss
343 if ( (pos
[0] !== null && newXY
[0] != pos
[0]) ||
344 (pos
[1] !== null && newXY
[1] != pos
[1]) ) {
345 this.setXY(el
, pos
, true);
351 Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
355 * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
356 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
358 * @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.
359 * @param {Int} x The value to use as the X coordinate for the element(s).
361 setX: function(el
, x
) {
362 Y
.Dom
.setXY(el
, [x
, null]);
366 * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
367 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
369 * @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.
370 * @param {Int} x To use as the Y coordinate for the element(s).
372 setY: function(el
, y
) {
373 Y
.Dom
.setXY(el
, [null, y
]);
377 * Returns the region position of the given element.
378 * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
380 * @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.
381 * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
383 getRegion: function(el
) {
384 var f = function(el
) {
385 if ( (el
.parentNode
=== null || el
.offsetParent
=== null ||
386 this.getStyle(el
, 'display') == 'none') && el
!= document
.body
) {
390 var region
= Y
.Region
.getRegion(el
);
394 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
398 * Returns the width of the client (viewport).
399 * @method getClientWidth
400 * @deprecated Now using getViewportWidth. This interface left intact for back compat.
401 * @return {Int} The width of the viewable area of the page.
403 getClientWidth: function() {
404 return Y
.Dom
.getViewportWidth();
408 * Returns the height of the client (viewport).
409 * @method getClientHeight
410 * @deprecated Now using getViewportHeight. This interface left intact for back compat.
411 * @return {Int} The height of the viewable area of the page.
413 getClientHeight: function() {
414 return Y
.Dom
.getViewportHeight();
418 * Returns a array of HTMLElements with the given class.
419 * For optimized performance, include a tag and/or root node when possible.
420 * @method getElementsByClassName
421 * @param {String} className The class name to match against
422 * @param {String} tag (optional) The tag name of the elements being collected
423 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
424 * @param {Function} apply (optional) A function to apply to each element when found
425 * @return {Array} An array of elements that have the given class name
427 getElementsByClassName: function(className
, tag
, root
, apply
) {
429 root
= (root
) ? Y
.Dom
.get(root
) : null || document
;
435 elements
= root
.getElementsByTagName(tag
),
436 re
= getClassRegEx(className
);
438 for (var i
= 0, len
= elements
.length
; i
< len
; ++i
) {
439 if ( re
.test(elements
[i
].className
) ) {
440 nodes
[nodes
.length
] = elements
[i
];
442 apply
.call(elements
[i
], elements
[i
]);
451 * Determines whether an HTMLElement has the given className.
453 * @param {String | HTMLElement | Array} el The element or collection to test
454 * @param {String} className the class name to search for
455 * @return {Boolean | Array} A boolean value or array of boolean values
457 hasClass: function(el
, className
) {
458 var re
= getClassRegEx(className
);
460 var f = function(el
) {
461 return re
.test(el
.className
);
464 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
468 * Adds a class name to a given element or collection of elements.
470 * @param {String | HTMLElement | Array} el The element or collection to add the class to
471 * @param {String} className the class name to add to the class attribute
473 addClass: function(el
, className
) {
474 var f = function(el
) {
475 if (this.hasClass(el
, className
)) {
476 return false; // already present
480 el
.className
= YAHOO
.lang
.trim([el
.className
, className
].join(' '));
484 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
488 * Removes a class name from a given element or collection of elements.
489 * @method removeClass
490 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
491 * @param {String} className the class name to remove from the class attribute
493 removeClass: function(el
, className
) {
494 var re
= getClassRegEx(className
);
496 var f = function(el
) {
497 if (!this.hasClass(el
, className
)) {
498 return false; // not present
502 var c
= el
.className
;
503 el
.className
= c
.replace(re
, ' ');
504 if ( this.hasClass(el
, className
) ) { // in case of multiple adjacent
505 this.removeClass(el
, className
);
508 el
.className
= YAHOO
.lang
.trim(el
.className
); // remove any trailing spaces
512 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
516 * Replace a class with another class for a given element or collection of elements.
517 * If no oldClassName is present, the newClassName is simply added.
518 * @method replaceClass
519 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
520 * @param {String} oldClassName the class name to be replaced
521 * @param {String} newClassName the class name that will be replacing the old class name
523 replaceClass: function(el
, oldClassName
, newClassName
) {
524 if (!newClassName
|| oldClassName
=== newClassName
) { // avoid infinite loop
528 var re
= getClassRegEx(oldClassName
);
530 var f = function(el
) {
532 if ( !this.hasClass(el
, oldClassName
) ) {
533 this.addClass(el
, newClassName
); // just add it if nothing to replace
534 return true; // NOTE: return
537 el
.className
= el
.className
.replace(re
, ' ' + newClassName
+ ' ');
539 if ( this.hasClass(el
, oldClassName
) ) { // in case of multiple adjacent
540 this.replaceClass(el
, oldClassName
, newClassName
);
543 el
.className
= YAHOO
.lang
.trim(el
.className
); // remove any trailing spaces
547 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
551 * Returns an ID and applies it to the element "el", if provided.
553 * @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).
554 * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
555 * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
557 generateId: function(el
, prefix
) {
558 prefix
= prefix
|| 'yui-gen';
560 var f = function(el
) {
561 if (el
&& el
.id
) { // do not override existing ID
565 var id
= prefix
+ id_counter
++;
574 // batch fails when no element, so just generate and return single ID
575 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true) || f
.apply(Y
.Dom
, arguments
);
579 * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
581 * @param {String | HTMLElement} haystack The possible ancestor
582 * @param {String | HTMLElement} needle The possible descendent
583 * @return {Boolean} Whether or not the haystack is an ancestor of needle
585 isAncestor: function(haystack
, needle
) {
586 haystack
= Y
.Dom
.get(haystack
);
587 if (!haystack
|| !needle
) { return false; }
589 var f = function(node
) {
590 if (haystack
.contains
&& node
.nodeType
&& !isSafari
) { // safari contains is broken
591 return haystack
.contains(node
);
593 else if ( haystack
.compareDocumentPosition
&& node
.nodeType
) {
594 return !!(haystack
.compareDocumentPosition(node
) & 16);
595 } else if (node
.nodeType
) {
596 // fallback to crawling up (safari)
597 return !!this.getAncestorBy(node
, function(el
) {
598 return el
== haystack
;
604 return Y
.Dom
.batch(needle
, f
, Y
.Dom
, true);
608 * Determines whether an HTMLElement is present in the current document.
610 * @param {String | HTMLElement} el The element to search for
611 * @return {Boolean} Whether or not the element is present in the current document
613 inDocument: function(el
) {
614 var f = function(el
) { // safari contains fails for body so crawl up
616 while (el
= el
.parentNode
) { // note assignment
617 if (el
== document
.documentElement
) {
623 return this.isAncestor(document
.documentElement
, el
);
626 return Y
.Dom
.batch(el
, f
, Y
.Dom
, true);
630 * Returns a array of HTMLElements that pass the test applied by supplied boolean method.
631 * For optimized performance, include a tag and/or root node when possible.
632 * @method getElementsBy
633 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
634 * @param {String} tag (optional) The tag name of the elements being collected
635 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
636 * @param {Function} apply (optional) A function to apply to each element when found
637 * @return {Array} Array of HTMLElements
639 getElementsBy: function(method
, tag
, root
, apply
) {
641 root
= (root
) ? Y
.Dom
.get(root
) : null || document
;
648 elements
= root
.getElementsByTagName(tag
);
650 for (var i
= 0, len
= elements
.length
; i
< len
; ++i
) {
651 if ( method(elements
[i
]) ) {
652 nodes
[nodes
.length
] = elements
[i
];
664 * Returns an the method(s) return value(s).
665 * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
667 * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
668 * @param {Function} method The method to apply to the element(s)
669 * @param {Any} o (optional) An optional arg that is passed to the supplied method
670 * @param {Boolean} override (optional) Whether or not to override the scope of "method" with "o"
671 * @return {Any | Array} The return value(s) from the supplied methods
673 batch: function(el
, method
, o
, override
) {
674 el
= (el
&& el
.tagName
) ? el
: Y
.Dom
.get(el
); // skip get() when possible
676 if (!el
|| !method
) {
679 var scope
= (override
) ? o
: window
;
681 if (el
.tagName
|| (!el
.item
&& !el
.slice
)) { // not a collection or array, just run the method
682 return method
.call(scope
, el
, o
);
687 for (var i
= 0, len
= el
.length
; i
< len
; ++i
) {
688 collection
[collection
.length
] = method
.call(scope
, el
[i
], o
);
695 * Returns the height of the document.
696 * @method getDocumentHeight
697 * @return {Int} The height of the actual document (which includes the body and its margin).
699 getDocumentHeight: function() {
700 var scrollHeight
= (document
.compatMode
!= 'CSS1Compat') ? document
.body
.scrollHeight
: document
.documentElement
.scrollHeight
;
702 var h
= Math
.max(scrollHeight
, Y
.Dom
.getViewportHeight());
707 * Returns the width of the document.
708 * @method getDocumentWidth
709 * @return {Int} The width of the actual document (which includes the body and its margin).
711 getDocumentWidth: function() {
712 var scrollWidth
= (document
.compatMode
!= 'CSS1Compat') ? document
.body
.scrollWidth
: document
.documentElement
.scrollWidth
;
713 var w
= Math
.max(scrollWidth
, Y
.Dom
.getViewportWidth());
718 * Returns the current height of the viewport.
719 * @method getViewportHeight
720 * @return {Int} The height of the viewable area of the page (excludes scrollbars).
722 getViewportHeight: function() {
723 var height
= self
.innerHeight
; // Safari, Opera
724 var mode
= document
.compatMode
;
726 if ( (mode
|| isIE
) && !isOpera
) { // IE, Gecko
727 height
= (mode
== 'CSS1Compat') ?
728 document
.documentElement
.clientHeight
: // Standards
729 document
.body
.clientHeight
; // Quirks
736 * Returns the current width of the viewport.
737 * @method getViewportWidth
738 * @return {Int} The width of the viewable area of the page (excludes scrollbars).
741 getViewportWidth: function() {
742 var width
= self
.innerWidth
; // Safari
743 var mode
= document
.compatMode
;
745 if (mode
|| isIE
) { // IE, Gecko, Opera
746 width
= (mode
== 'CSS1Compat') ?
747 document
.documentElement
.clientWidth
: // Standards
748 document
.body
.clientWidth
; // Quirks
754 * Returns the nearest ancestor that passes the test applied by supplied boolean method.
755 * For performance reasons, IDs are not accepted and argument validation omitted.
756 * @method getAncestorBy
757 * @param {HTMLElement} node The HTMLElement to use as the starting point
758 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
759 * @return {Object} HTMLElement or null if not found
761 getAncestorBy: function(node
, method
) {
762 while (node
= node
.parentNode
) { // NOTE: assignment
763 if ( testElement(node
, method
) ) {
772 * Returns the nearest ancestor with the given className.
773 * @method getAncestorByClassName
774 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
775 * @param {String} className
776 * @return {Object} HTMLElement
778 getAncestorByClassName: function(node
, className
) {
779 node
= Y
.Dom
.get(node
);
783 var method = function(el
) { return Y
.Dom
.hasClass(el
, className
); };
784 return Y
.Dom
.getAncestorBy(node
, method
);
788 * Returns the nearest ancestor with the given tagName.
789 * @method getAncestorByTagName
790 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
791 * @param {String} tagName
792 * @return {Object} HTMLElement
794 getAncestorByTagName: function(node
, tagName
) {
795 node
= Y
.Dom
.get(node
);
799 var method = function(el
) {
800 return el
.tagName
&& el
.tagName
.toUpperCase() == tagName
.toUpperCase();
803 return Y
.Dom
.getAncestorBy(node
, method
);
807 * Returns the previous sibling that is an HTMLElement.
808 * For performance reasons, IDs are not accepted and argument validation omitted.
809 * Returns the nearest HTMLElement sibling if no method provided.
810 * @method getPreviousSiblingBy
811 * @param {HTMLElement} node The HTMLElement to use as the starting point
812 * @param {Function} method A boolean function used to test siblings
813 * that receives the sibling node being tested as its only argument
814 * @return {Object} HTMLElement or null if not found
816 getPreviousSiblingBy: function(node
, method
) {
818 node
= node
.previousSibling
;
819 if ( testElement(node
, method
) ) {
827 * Returns the previous sibling that is an HTMLElement
828 * @method getPreviousSibling
829 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
830 * @return {Object} HTMLElement or null if not found
832 getPreviousSibling: function(node
) {
833 node
= Y
.Dom
.get(node
);
838 return Y
.Dom
.getPreviousSiblingBy(node
);
842 * Returns the next HTMLElement sibling that passes the boolean method.
843 * For performance reasons, IDs are not accepted and argument validation omitted.
844 * Returns the nearest HTMLElement sibling if no method provided.
845 * @method getNextSiblingBy
846 * @param {HTMLElement} node The HTMLElement to use as the starting point
847 * @param {Function} method A boolean function used to test siblings
848 * that receives the sibling node being tested as its only argument
849 * @return {Object} HTMLElement or null if not found
851 getNextSiblingBy: function(node
, method
) {
853 node
= node
.nextSibling
;
854 if ( testElement(node
, method
) ) {
862 * Returns the next sibling that is an HTMLElement
863 * @method getNextSibling
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
867 getNextSibling: function(node
) {
868 node
= Y
.Dom
.get(node
);
873 return Y
.Dom
.getNextSiblingBy(node
);
877 * Returns the first HTMLElement child that passes the test method.
878 * @method getFirstChildBy
879 * @param {HTMLElement} node The HTMLElement to use as the starting point
880 * @param {Function} method A boolean function used to test children
881 * that receives the node being tested as its only argument
882 * @return {Object} HTMLElement or null if not found
884 getFirstChildBy: function(node
, method
) {
885 var child
= ( testElement(node
.firstChild
, method
) ) ? node
.firstChild
: null;
886 return child
|| Y
.Dom
.getNextSiblingBy(node
.firstChild
, method
);
890 * Returns the first HTMLElement child.
891 * @method getFirstChild
892 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
893 * @return {Object} HTMLElement or null if not found
895 getFirstChild: function(node
, method
) {
896 node
= Y
.Dom
.get(node
);
900 return Y
.Dom
.getFirstChildBy(node
);
904 * Returns the last HTMLElement child that passes the test method.
905 * @method getLastChildBy
906 * @param {HTMLElement} node The HTMLElement to use as the starting point
907 * @param {Function} method A boolean function used to test children
908 * that receives the node being tested as its only argument
909 * @return {Object} HTMLElement or null if not found
911 getLastChildBy: function(node
, method
) {
915 var child
= ( testElement(node
.lastChild
, method
) ) ? node
.lastChild
: null;
916 return child
|| Y
.Dom
.getPreviousSiblingBy(node
.lastChild
, method
);
920 * Returns the last HTMLElement child.
921 * @method getLastChild
922 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
923 * @return {Object} HTMLElement or null if not found
925 getLastChild: function(node
) {
926 node
= Y
.Dom
.get(node
);
927 return Y
.Dom
.getLastChildBy(node
);
931 * Returns an array of HTMLElement childNodes that pass the test method.
932 * @method getChildrenBy
933 * @param {HTMLElement} node The HTMLElement to start from
934 * @param {Function} method A boolean function used to test children
935 * that receives the node being tested as its only argument
936 * @return {Array} A static array of HTMLElements
938 getChildrenBy: function(node
, method
) {
939 var child
= Y
.Dom
.getFirstChildBy(node
, method
);
940 var children
= child
? [child
] : [];
942 Y
.Dom
.getNextSiblingBy(child
, function(node
) {
943 if ( !method
|| method(node
) ) {
944 children
[children
.length
] = node
;
946 return false; // fail test to collect all children
953 * Returns an array of HTMLElement childNodes.
954 * @method getChildren
955 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
956 * @return {Array} A static array of HTMLElements
958 getChildren: function(node
) {
959 node
= Y
.Dom
.get(node
);
963 return Y
.Dom
.getChildrenBy(node
);
967 * Returns the left scroll value of the document
968 * @method getDocumentScrollLeft
969 * @param {HTMLDocument} document (optional) The document to get the scroll value of
970 * @return {Int} The amount that the document is scrolled to the left
972 getDocumentScrollLeft: function(doc
) {
973 doc
= doc
|| document
;
974 return Math
.max(doc
.documentElement
.scrollLeft
, doc
.body
.scrollLeft
);
978 * Returns the top scroll value of the document
979 * @method getDocumentScrollTop
980 * @param {HTMLDocument} document (optional) The document to get the scroll value of
981 * @return {Int} The amount that the document is scrolled to the top
983 getDocumentScrollTop: function(doc
) {
984 doc
= doc
|| document
;
985 return Math
.max(doc
.documentElement
.scrollTop
, doc
.body
.scrollTop
);
989 * Inserts the new node as the previous sibling of the reference node
990 * @method insertBefore
991 * @param {String | HTMLElement} newNode The node to be inserted
992 * @param {String | HTMLElement} referenceNode The node to insert the new node before
993 * @return {HTMLElement} The node that was inserted (or null if insert fails)
995 insertBefore: function(newNode
, referenceNode
) {
996 newNode
= Y
.Dom
.get(newNode
);
997 referenceNode
= Y
.Dom
.get(referenceNode
);
999 if (!newNode
|| !referenceNode
|| !referenceNode
.parentNode
) {
1003 return referenceNode
.parentNode
.insertBefore(newNode
, referenceNode
);
1007 * Inserts the new node as the next sibling of the reference node
1008 * @method insertAfter
1009 * @param {String | HTMLElement} newNode The node to be inserted
1010 * @param {String | HTMLElement} referenceNode The node to insert the new node after
1011 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1013 insertAfter: function(newNode
, referenceNode
) {
1014 newNode
= Y
.Dom
.get(newNode
);
1015 referenceNode
= Y
.Dom
.get(referenceNode
);
1017 if (!newNode
|| !referenceNode
|| !referenceNode
.parentNode
) {
1021 if (referenceNode
.nextSibling
) {
1022 return referenceNode
.parentNode
.insertBefore(newNode
, referenceNode
.nextSibling
);
1024 return referenceNode
.parentNode
.appendChild(newNode
);
1030 * A region is a representation of an object on a grid. It is defined
1031 * by the top, right, bottom, left extents, so is rectangular by default. If
1032 * other shapes are required, this class could be extended to support it.
1033 * @namespace YAHOO.util
1035 * @param {Int} t the top extent
1036 * @param {Int} r the right extent
1037 * @param {Int} b the bottom extent
1038 * @param {Int} l the left extent
1041 YAHOO
.util
.Region = function(t
, r
, b
, l
) {
1044 * The region's top extent
1051 * The region's top extent as index, for symmetry with set/getXY
1058 * The region's right extent
1065 * The region's bottom extent
1072 * The region's left extent
1079 * The region's left extent as index, for symmetry with set/getXY
1087 * Returns true if this region contains the region passed in
1089 * @param {Region} region The region to evaluate
1090 * @return {Boolean} True if the region is contained with this region,
1093 YAHOO
.util
.Region
.prototype.contains = function(region
) {
1094 return ( region
.left
>= this.left
&&
1095 region
.right
<= this.right
&&
1096 region
.top
>= this.top
&&
1097 region
.bottom
<= this.bottom
);
1102 * Returns the area of the region
1104 * @return {Int} the region's area
1106 YAHOO
.util
.Region
.prototype.getArea = function() {
1107 return ( (this.bottom
- this.top
) * (this.right
- this.left
) );
1111 * Returns the region where the passed in region overlaps with this one
1113 * @param {Region} region The region that intersects
1114 * @return {Region} The overlap region, or null if there is no overlap
1116 YAHOO
.util
.Region
.prototype.intersect = function(region
) {
1117 var t
= Math
.max( this.top
, region
.top
);
1118 var r
= Math
.min( this.right
, region
.right
);
1119 var b
= Math
.min( this.bottom
, region
.bottom
);
1120 var l
= Math
.max( this.left
, region
.left
);
1122 if (b
>= t
&& r
>= l
) {
1123 return new YAHOO
.util
.Region(t
, r
, b
, l
);
1130 * Returns the region representing the smallest region that can contain both
1131 * the passed in region and this region.
1133 * @param {Region} region The region that to create the union with
1134 * @return {Region} The union region
1136 YAHOO
.util
.Region
.prototype.union = function(region
) {
1137 var t
= Math
.min( this.top
, region
.top
);
1138 var r
= Math
.max( this.right
, region
.right
);
1139 var b
= Math
.max( this.bottom
, region
.bottom
);
1140 var l
= Math
.min( this.left
, region
.left
);
1142 return new YAHOO
.util
.Region(t
, r
, b
, l
);
1148 * @return string the region properties
1150 YAHOO
.util
.Region
.prototype.toString = function() {
1151 return ( "Region {" +
1152 "top: " + this.top
+
1153 ", right: " + this.right
+
1154 ", bottom: " + this.bottom
+
1155 ", left: " + this.left
+
1160 * Returns a region that is occupied by the DOM element
1162 * @param {HTMLElement} el The element
1163 * @return {Region} The region that the element occupies
1166 YAHOO
.util
.Region
.getRegion = function(el
) {
1167 var p
= YAHOO
.util
.Dom
.getXY(el
);
1170 var r
= p
[0] + el
.offsetWidth
;
1171 var b
= p
[1] + el
.offsetHeight
;
1174 return new YAHOO
.util
.Region(t
, r
, b
, l
);
1177 /////////////////////////////////////////////////////////////////////////////
1181 * A point is a region that is special in that it represents a single point on
1183 * @namespace YAHOO.util
1185 * @param {Int} x The X position of the point
1186 * @param {Int} y The Y position of the point
1188 * @extends YAHOO.util.Region
1190 YAHOO
.util
.Point = function(x
, y
) {
1191 if (YAHOO
.lang
.isArray(x
)) { // accept input from Dom.getXY, Event.getXY, etc.
1192 y
= x
[1]; // dont blow away x yet
1197 * The X position of the point, which is also the right, left and index zero (for Dom.getXY symmetry)
1202 this.x
= this.right
= this.left
= this[0] = x
;
1205 * The Y position of the point, which is also the top, bottom and index one (for Dom.getXY symmetry)
1209 this.y
= this.top
= this.bottom
= this[1] = y
;
1212 YAHOO
.util
.Point
.prototype = new YAHOO
.util
.Region();
1214 YAHOO
.register("dom", YAHOO
.util
.Dom
, {version
: "2.3.0", build
: "442"});