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;
91 YAHOO.log('getStyle: IE filter failed',
96 case 'float': // fix reserved word
97 property = 'styleFloat'; // fall through
99 // test currentStyle before touching
100 var value = el.currentStyle ? el.currentStyle[property] : null;
101 return ( el.style[property] || value );
104 } else { // default to inline only
105 getStyle = function(el, property) { return el.style[property]; };
109 setStyle = function(el, property, val) {
112 if ( YAHOO.lang.isString(el.style.filter) ) { // in case not appended
113 el.style.filter = 'alpha(opacity=' + val * 100 + ')';
115 if (!el.currentStyle || !el.currentStyle.hasLayout) {
116 el.style.zoom = 1; // when no layout or cant tell
121 property = 'styleFloat';
123 el.style[property] = val;
127 setStyle = function(el, property, val) {
128 if (property == 'float') {
129 property = 'cssFloat';
131 el.style[property] = val;
135 var testElement = function(node, method) {
136 return node && node.nodeType == 1 && ( !method || method(node) );
140 * Provides helper methods for DOM elements.
141 * @namespace YAHOO.util
146 * Returns an HTMLElement reference.
148 * @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.
149 * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
152 if (!el || el.tagName || el.item) { // null, HTMLElement, or HTMLCollection
156 if (YAHOO.lang.isString(el)) { // HTMLElement or null
157 return document.getElementById(el);
160 if (el.splice) { // Array of HTMLElements/IDs
162 for (var i = 0, len = el.length; i < len; ++i) {
163 c[c.length] = Y.Dom.get(el[i]);
169 return el; // some other object, just pass it back
173 * Normalizes currentStyle and ComputedStyle.
175 * @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.
176 * @param {String} property The style property whose value is returned.
177 * @return {String | Array} The current value of the style property for the element(s).
179 getStyle: function(el, property) {
180 property = toCamel(property);
182 var f = function(element) {
183 return getStyle(element, property);
186 return Y.Dom.batch(el, f, Y.Dom, true);
190 * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
192 * @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.
193 * @param {String} property The style property to be set.
194 * @param {String} val The value to apply to the given property.
196 setStyle: function(el, property, val) {
197 property = toCamel(property);
199 var f = function(element) {
200 setStyle(element, property, val);
201 YAHOO.log('setStyle setting ' + property + ' to ' + val, 'info', 'Dom');
205 Y.Dom.batch(el, f, Y.Dom, true);
209 * 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).
211 * @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
212 * @return {Array} The XY position of the element(s)
214 getXY: function(el) {
215 var f = function(el) {
217 // has to be part of document to have pageXY
218 if ( (el.parentNode === null || el.offsetParent === null ||
219 this.getStyle(el, 'display') == 'none') && el != document.body) {
220 YAHOO.log('getXY failed: element not available', 'error', 'Dom');
224 var parentNode = null;
227 var doc = el.ownerDocument;
229 if (el.getBoundingClientRect) { // IE
230 box = el.getBoundingClientRect();
231 return [box.left + Y.Dom.getDocumentScrollLeft(el.ownerDocument), box.top + Y.Dom.getDocumentScrollTop(el.ownerDocument)];
233 else { // safari, opera, & gecko
234 pos = [el.offsetLeft, el.offsetTop];
235 parentNode = el.offsetParent;
237 // safari: if el is abs or any parent is abs, subtract body offsets
238 var hasAbs = this.getStyle(el, 'position') == 'absolute';
240 if (parentNode != el) {
242 pos[0] += parentNode.offsetLeft;
243 pos[1] += parentNode.offsetTop;
244 if (isSafari && !hasAbs &&
245 this.getStyle(parentNode,'position') == 'absolute' ) {
246 hasAbs = true; // we need to offset if any parent is absolutely positioned
248 parentNode = parentNode.offsetParent;
252 if (isSafari && hasAbs) { //safari doubles in this case
253 pos[0] -= el.ownerDocument.body.offsetLeft;
254 pos[1] -= el.ownerDocument.body.offsetTop;
258 parentNode = el.parentNode;
260 // account for any scrolled ancestors
261 while ( parentNode.tagName && !patterns.ROOT_TAG.test(parentNode.tagName) )
263 // work around opera inline/table scrollLeft/Top bug
264 if (Y.Dom.getStyle(parentNode, 'display').search(/^inline|table-row.*$/i)) {
265 pos[0] -= parentNode.scrollLeft;
266 pos[1] -= parentNode.scrollTop;
269 parentNode = parentNode.parentNode;
272 YAHOO.log('getXY returning ' + pos, 'info', 'Dom');
277 return Y.Dom.batch(el, f, Y.Dom, true);
281 * 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).
283 * @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
284 * @return {Number | Array} The X position of the element(s)
287 var f = function(el) {
288 return Y.Dom.getXY(el)[0];
291 return Y.Dom.batch(el, f, Y.Dom, true);
295 * 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).
297 * @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
298 * @return {Number | Array} The Y position of the element(s)
301 var f = function(el) {
302 return Y.Dom.getXY(el)[1];
305 return Y.Dom.batch(el, f, Y.Dom, true);
309 * Set the position of an html element in page coordinates, regardless of how the element is positioned.
310 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
312 * @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
313 * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
314 * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
316 setXY: function(el, pos, noRetry) {
317 var f = function(el) {
318 var style_pos = this.getStyle(el, 'position');
319 if (style_pos == 'static') { // default to relative
320 this.setStyle(el, 'position', 'relative');
321 style_pos = 'relative';
324 var pageXY = this.getXY(el);
325 if (pageXY === false) { // has to be part of doc to have pageXY
326 YAHOO.log('setXY failed: element not available', 'error', 'Dom');
330 var delta = [ // assuming pixels; if not we will have to retry
331 parseInt( this.getStyle(el, 'left'), 10 ),
332 parseInt( this.getStyle(el, 'top'), 10 )
335 if ( isNaN(delta[0]) ) {// in case of 'auto'
336 delta[0] = (style_pos == 'relative') ? 0 : el.offsetLeft;
338 if ( isNaN(delta[1]) ) { // in case of 'auto'
339 delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
342 if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
343 if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }
346 var newXY = this.getXY(el);
348 // if retry is true, try one more time if we miss
349 if ( (pos[0] !== null && newXY[0] != pos[0]) ||
350 (pos[1] !== null && newXY[1] != pos[1]) ) {
351 this.setXY(el, pos, true);
355 YAHOO.log('setXY setting position to ' + pos, 'info', 'Dom');
358 Y.Dom.batch(el, f, Y.Dom, true);
362 * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
363 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
365 * @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.
366 * @param {Int} x The value to use as the X coordinate for the element(s).
368 setX: function(el, x) {
369 Y.Dom.setXY(el, [x, null]);
373 * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
374 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
376 * @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.
377 * @param {Int} x To use as the Y coordinate for the element(s).
379 setY: function(el, y) {
380 Y.Dom.setXY(el, [null, y]);
384 * Returns the region position of the given element.
385 * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
387 * @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.
388 * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
390 getRegion: function(el) {
391 var f = function(el) {
392 if ( (el.parentNode === null || el.offsetParent === null ||
393 this.getStyle(el, 'display') == 'none') && el != document.body) {
394 YAHOO.log('getRegion failed: element not available', 'error', 'Dom');
398 var region = Y.Region.getRegion(el);
399 YAHOO.log('getRegion returning ' + region, 'info', 'Dom');
403 return Y.Dom.batch(el, f, Y.Dom, true);
407 * Returns the width of the client (viewport).
408 * @method getClientWidth
409 * @deprecated Now using getViewportWidth. This interface left intact for back compat.
410 * @return {Int} The width of the viewable area of the page.
412 getClientWidth: function() {
413 return Y.Dom.getViewportWidth();
417 * Returns the height of the client (viewport).
418 * @method getClientHeight
419 * @deprecated Now using getViewportHeight. This interface left intact for back compat.
420 * @return {Int} The height of the viewable area of the page.
422 getClientHeight: function() {
423 return Y.Dom.getViewportHeight();
427 * Returns a array of HTMLElements with the given class.
428 * For optimized performance, include a tag and/or root node when possible.
429 * @method getElementsByClassName
430 * @param {String} className The class name to match against
431 * @param {String} tag (optional) The tag name of the elements being collected
432 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
433 * @param {Function} apply (optional) A function to apply to each element when found
434 * @return {Array} An array of elements that have the given class name
436 getElementsByClassName: function(className, tag, root, apply) {
438 root = (root) ? Y.Dom.get(root) : null || document;
444 elements = root.getElementsByTagName(tag),
445 re = getClassRegEx(className);
447 for (var i = 0, len = elements.length; i < len; ++i) {
448 if ( re.test(elements[i].className) ) {
449 nodes[nodes.length] = elements[i];
451 apply.call(elements[i], elements[i]);
460 * Determines whether an HTMLElement has the given className.
462 * @param {String | HTMLElement | Array} el The element or collection to test
463 * @param {String} className the class name to search for
464 * @return {Boolean | Array} A boolean value or array of boolean values
466 hasClass: function(el, className) {
467 var re = getClassRegEx(className);
469 var f = function(el) {
470 YAHOO.log('hasClass returning ' + re.test(el.className), 'info', 'Dom');
471 return re.test(el.className);
474 return Y.Dom.batch(el, f, Y.Dom, true);
478 * Adds a class name to a given element or collection of elements.
480 * @param {String | HTMLElement | Array} el The element or collection to add the class to
481 * @param {String} className the class name to add to the class attribute
483 addClass: function(el, className) {
484 var f = function(el) {
485 if (this.hasClass(el, className)) {
486 return false; // already present
489 YAHOO.log('addClass adding ' + className, 'info', 'Dom');
491 el.className = YAHOO.lang.trim([el.className, className].join(' '));
495 return Y.Dom.batch(el, f, Y.Dom, true);
499 * Removes a class name from a given element or collection of elements.
500 * @method removeClass
501 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
502 * @param {String} className the class name to remove from the class attribute
504 removeClass: function(el, className) {
505 var re = getClassRegEx(className);
507 var f = function(el) {
508 if (!this.hasClass(el, className)) {
509 return false; // not present
512 YAHOO.log('removeClass removing ' + className, 'info', 'Dom');
514 var c = el.className;
515 el.className = c.replace(re, ' ');
516 if ( this.hasClass(el, className) ) { // in case of multiple adjacent
517 this.removeClass(el, className);
520 el.className = YAHOO.lang.trim(el.className); // remove any trailing spaces
524 return Y.Dom.batch(el, f, Y.Dom, true);
528 * Replace a class with another class for a given element or collection of elements.
529 * If no oldClassName is present, the newClassName is simply added.
530 * @method replaceClass
531 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
532 * @param {String} oldClassName the class name to be replaced
533 * @param {String} newClassName the class name that will be replacing the old class name
535 replaceClass: function(el, oldClassName, newClassName) {
536 if (!newClassName || oldClassName === newClassName) { // avoid infinite loop
540 var re = getClassRegEx(oldClassName);
542 var f = function(el) {
543 YAHOO.log('replaceClass replacing ' + oldClassName + ' with ' + newClassName, 'info', 'Dom');
545 if ( !this.hasClass(el, oldClassName) ) {
546 this.addClass(el, newClassName); // just add it if nothing to replace
547 return true; // NOTE: return
550 el.className = el.className.replace(re, ' ' + newClassName + ' ');
552 if ( this.hasClass(el, oldClassName) ) { // in case of multiple adjacent
553 this.replaceClass(el, oldClassName, newClassName);
556 el.className = YAHOO.lang.trim(el.className); // remove any trailing spaces
560 return Y.Dom.batch(el, f, Y.Dom, true);
564 * Returns an ID and applies it to the element "el", if provided.
566 * @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).
567 * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
568 * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
570 generateId: function(el, prefix) {
571 prefix = prefix || 'yui-gen';
573 var f = function(el) {
574 if (el && el.id) { // do not override existing ID
575 YAHOO.log('generateId returning existing id ' + el.id, 'info', 'Dom');
579 var id = prefix + id_counter++;
580 YAHOO.log('generateId generating ' + id, 'info', 'Dom');
589 // batch fails when no element, so just generate and return single ID
590 return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
594 * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
596 * @param {String | HTMLElement} haystack The possible ancestor
597 * @param {String | HTMLElement} needle The possible descendent
598 * @return {Boolean} Whether or not the haystack is an ancestor of needle
600 isAncestor: function(haystack, needle) {
601 haystack = Y.Dom.get(haystack);
602 if (!haystack || !needle) { return false; }
604 var f = function(node) {
605 if (haystack.contains && node.nodeType && !isSafari) { // safari contains is broken
606 YAHOO.log('isAncestor returning ' + haystack.contains(node), 'info', 'Dom');
607 return haystack.contains(node);
609 else if ( haystack.compareDocumentPosition && node.nodeType ) {
610 YAHOO.log('isAncestor returning ' + !!(haystack.compareDocumentPosition(node) & 16), 'info', 'Dom');
611 return !!(haystack.compareDocumentPosition(node) & 16);
612 } else if (node.nodeType) {
613 // fallback to crawling up (safari)
614 return !!this.getAncestorBy(node, function(el) {
615 return el == haystack;
618 YAHOO.log('isAncestor failed; most likely needle is not an HTMLElement', 'error', 'Dom');
622 return Y.Dom.batch(needle, f, Y.Dom, true);
626 * Determines whether an HTMLElement is present in the current document.
628 * @param {String | HTMLElement} el The element to search for
629 * @return {Boolean} Whether or not the element is present in the current document
631 inDocument: function(el) {
632 var f = function(el) { // safari contains fails for body so crawl up
634 while (el = el.parentNode) { // note assignment
635 if (el == document.documentElement) {
641 return this.isAncestor(document.documentElement, el);
644 return Y.Dom.batch(el, f, Y.Dom, true);
648 * Returns a array of HTMLElements that pass the test applied by supplied boolean method.
649 * For optimized performance, include a tag and/or root node when possible.
650 * @method getElementsBy
651 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
652 * @param {String} tag (optional) The tag name of the elements being collected
653 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
654 * @param {Function} apply (optional) A function to apply to each element when found
655 * @return {Array} Array of HTMLElements
657 getElementsBy: function(method, tag, root, apply) {
659 root = (root) ? Y.Dom.get(root) : null || document;
666 elements = root.getElementsByTagName(tag);
668 for (var i = 0, len = elements.length; i < len; ++i) {
669 if ( method(elements[i]) ) {
670 nodes[nodes.length] = elements[i];
677 YAHOO.log('getElementsBy returning ' + nodes, 'info', 'Dom');
683 * Returns an the method(s) return value(s).
684 * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
686 * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
687 * @param {Function} method The method to apply to the element(s)
688 * @param {Any} o (optional) An optional arg that is passed to the supplied method
689 * @param {Boolean} override (optional) Whether or not to override the scope of "method" with "o"
690 * @return {Any | Array} The return value(s) from the supplied methods
692 batch: function(el, method, o, override) {
693 el = (el && el.tagName) ? el : Y.Dom.get(el); // skip get() when possible
695 if (!el || !method) {
696 YAHOO.log('batch failed: invalid arguments', 'error', 'Dom');
699 var scope = (override) ? o : window;
701 if (el.tagName || (!el.item && !el.slice)) { // not a collection or array, just run the method
702 return method.call(scope, el, o);
707 for (var i = 0, len = el.length; i < len; ++i) {
708 collection[collection.length] = method.call(scope, el[i], o);
715 * Returns the height of the document.
716 * @method getDocumentHeight
717 * @return {Int} The height of the actual document (which includes the body and its margin).
719 getDocumentHeight: function() {
720 var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;
722 var h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
723 YAHOO.log('getDocumentHeight returning ' + h, 'info', 'Dom');
728 * Returns the width of the document.
729 * @method getDocumentWidth
730 * @return {Int} The width of the actual document (which includes the body and its margin).
732 getDocumentWidth: function() {
733 var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
734 var w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
735 YAHOO.log('getDocumentWidth returning ' + w, 'info', 'Dom');
740 * Returns the current height of the viewport.
741 * @method getViewportHeight
742 * @return {Int} The height of the viewable area of the page (excludes scrollbars).
744 getViewportHeight: function() {
745 var height = self.innerHeight; // Safari, Opera
746 var mode = document.compatMode;
748 if ( (mode || isIE) && !isOpera ) { // IE, Gecko
749 height = (mode == 'CSS1Compat') ?
750 document.documentElement.clientHeight : // Standards
751 document.body.clientHeight; // Quirks
754 YAHOO.log('getViewportHeight returning ' + height, 'info', 'Dom');
759 * Returns the current width of the viewport.
760 * @method getViewportWidth
761 * @return {Int} The width of the viewable area of the page (excludes scrollbars).
764 getViewportWidth: function() {
765 var width = self.innerWidth; // Safari
766 var mode = document.compatMode;
768 if (mode || isIE) { // IE, Gecko, Opera
769 width = (mode == 'CSS1Compat') ?
770 document.documentElement.clientWidth : // Standards
771 document.body.clientWidth; // Quirks
773 YAHOO.log('getViewportWidth returning ' + width, 'info', 'Dom');
778 * Returns the nearest ancestor that passes the test applied by supplied boolean method.
779 * For performance reasons, IDs are not accepted and argument validation omitted.
780 * @method getAncestorBy
781 * @param {HTMLElement} node The HTMLElement to use as the starting point
782 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
783 * @return {Object} HTMLElement or null if not found
785 getAncestorBy: function(node, method) {
786 while (node = node.parentNode) { // NOTE: assignment
787 if ( testElement(node, method) ) {
788 YAHOO.log('getAncestorBy returning ' + node, 'info', 'Dom');
793 YAHOO.log('getAncestorBy returning null (no ancestor passed test)', 'error', 'Dom');
798 * Returns the nearest ancestor with the given className.
799 * @method getAncestorByClassName
800 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
801 * @param {String} className
802 * @return {Object} HTMLElement
804 getAncestorByClassName: function(node, className) {
805 node = Y.Dom.get(node);
807 YAHOO.log('getAncestorByClassName failed: invalid node argument', 'error', 'Dom');
810 var method = function(el) { return Y.Dom.hasClass(el, className); };
811 return Y.Dom.getAncestorBy(node, method);
815 * Returns the nearest ancestor with the given tagName.
816 * @method getAncestorByTagName
817 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
818 * @param {String} tagName
819 * @return {Object} HTMLElement
821 getAncestorByTagName: function(node, tagName) {
822 node = Y.Dom.get(node);
824 YAHOO.log('getAncestorByTagName failed: invalid node argument', 'error', 'Dom');
827 var method = function(el) {
828 return el.tagName && el.tagName.toUpperCase() == tagName.toUpperCase();
831 return Y.Dom.getAncestorBy(node, method);
835 * Returns the previous sibling that is an HTMLElement.
836 * For performance reasons, IDs are not accepted and argument validation omitted.
837 * Returns the nearest HTMLElement sibling if no method provided.
838 * @method getPreviousSiblingBy
839 * @param {HTMLElement} node The HTMLElement to use as the starting point
840 * @param {Function} method A boolean function used to test siblings
841 * that receives the sibling node being tested as its only argument
842 * @return {Object} HTMLElement or null if not found
844 getPreviousSiblingBy: function(node, method) {
846 node = node.previousSibling;
847 if ( testElement(node, method) ) {
855 * Returns the previous sibling that is an HTMLElement
856 * @method getPreviousSibling
857 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
858 * @return {Object} HTMLElement or null if not found
860 getPreviousSibling: function(node) {
861 node = Y.Dom.get(node);
863 YAHOO.log('getPreviousSibling failed: invalid node argument', 'error', 'Dom');
867 return Y.Dom.getPreviousSiblingBy(node);
871 * Returns the next HTMLElement sibling that passes the boolean method.
872 * For performance reasons, IDs are not accepted and argument validation omitted.
873 * Returns the nearest HTMLElement sibling if no method provided.
874 * @method getNextSiblingBy
875 * @param {HTMLElement} node The HTMLElement to use as the starting point
876 * @param {Function} method A boolean function used to test siblings
877 * that receives the sibling node being tested as its only argument
878 * @return {Object} HTMLElement or null if not found
880 getNextSiblingBy: function(node, method) {
882 node = node.nextSibling;
883 if ( testElement(node, method) ) {
891 * Returns the next sibling that is an HTMLElement
892 * @method getNextSibling
893 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
894 * @return {Object} HTMLElement or null if not found
896 getNextSibling: function(node) {
897 node = Y.Dom.get(node);
899 YAHOO.log('getNextSibling failed: invalid node argument', 'error', 'Dom');
903 return Y.Dom.getNextSiblingBy(node);
907 * Returns the first HTMLElement child that passes the test method.
908 * @method getFirstChildBy
909 * @param {HTMLElement} node The HTMLElement to use as the starting point
910 * @param {Function} method A boolean function used to test children
911 * that receives the node being tested as its only argument
912 * @return {Object} HTMLElement or null if not found
914 getFirstChildBy: function(node, method) {
915 var child = ( testElement(node.firstChild, method) ) ? node.firstChild : null;
916 return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
920 * Returns the first HTMLElement child.
921 * @method getFirstChild
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 getFirstChild: function(node, method) {
926 node = Y.Dom.get(node);
928 YAHOO.log('getFirstChild failed: invalid node argument', 'error', 'Dom');
931 return Y.Dom.getFirstChildBy(node);
935 * Returns the last HTMLElement child that passes the test method.
936 * @method getLastChildBy
937 * @param {HTMLElement} node The HTMLElement to use as the starting point
938 * @param {Function} method A boolean function used to test children
939 * that receives the node being tested as its only argument
940 * @return {Object} HTMLElement or null if not found
942 getLastChildBy: function(node, method) {
944 YAHOO.log('getLastChild failed: invalid node argument', 'error', 'Dom');
947 var child = ( testElement(node.lastChild, method) ) ? node.lastChild : null;
948 return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
952 * Returns the last HTMLElement child.
953 * @method getLastChild
954 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
955 * @return {Object} HTMLElement or null if not found
957 getLastChild: function(node) {
958 node = Y.Dom.get(node);
959 return Y.Dom.getLastChildBy(node);
963 * Returns an array of HTMLElement childNodes that pass the test method.
964 * @method getChildrenBy
965 * @param {HTMLElement} node The HTMLElement to start from
966 * @param {Function} method A boolean function used to test children
967 * that receives the node being tested as its only argument
968 * @return {Array} A static array of HTMLElements
970 getChildrenBy: function(node, method) {
971 var child = Y.Dom.getFirstChildBy(node, method);
972 var children = child ? [child] : [];
974 Y.Dom.getNextSiblingBy(child, function(node) {
975 if ( !method || method(node) ) {
976 children[children.length] = node;
978 return false; // fail test to collect all children
985 * Returns an array of HTMLElement childNodes.
986 * @method getChildren
987 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
988 * @return {Array} A static array of HTMLElements
990 getChildren: function(node) {
991 node = Y.Dom.get(node);
993 YAHOO.log('getChildren failed: invalid node argument', 'error', 'Dom');
996 return Y.Dom.getChildrenBy(node);
1000 * Returns the left scroll value of the document
1001 * @method getDocumentScrollLeft
1002 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1003 * @return {Int} The amount that the document is scrolled to the left
1005 getDocumentScrollLeft: function(doc) {
1006 doc = doc || document;
1007 return Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
1011 * Returns the top scroll value of the document
1012 * @method getDocumentScrollTop
1013 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1014 * @return {Int} The amount that the document is scrolled to the top
1016 getDocumentScrollTop: function(doc) {
1017 doc = doc || document;
1018 return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
1022 * Inserts the new node as the previous sibling of the reference node
1023 * @method insertBefore
1024 * @param {String | HTMLElement} newNode The node to be inserted
1025 * @param {String | HTMLElement} referenceNode The node to insert the new node before
1026 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1028 insertBefore: function(newNode, referenceNode) {
1029 newNode = Y.Dom.get(newNode);
1030 referenceNode = Y.Dom.get(referenceNode);
1032 if (!newNode || !referenceNode || !referenceNode.parentNode) {
1033 YAHOO.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
1037 return referenceNode.parentNode.insertBefore(newNode, referenceNode);
1041 * Inserts the new node as the next sibling of the reference node
1042 * @method insertAfter
1043 * @param {String | HTMLElement} newNode The node to be inserted
1044 * @param {String | HTMLElement} referenceNode The node to insert the new node after
1045 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1047 insertAfter: function(newNode, referenceNode) {
1048 newNode = Y.Dom.get(newNode);
1049 referenceNode = Y.Dom.get(referenceNode);
1051 if (!newNode || !referenceNode || !referenceNode.parentNode) {
1052 YAHOO.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
1056 if (referenceNode.nextSibling) {
1057 return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
1059 return referenceNode.parentNode.appendChild(newNode);
1065 * A region is a representation of an object on a grid. It is defined
1066 * by the top, right, bottom, left extents, so is rectangular by default. If
1067 * other shapes are required, this class could be extended to support it.
1068 * @namespace YAHOO.util
1070 * @param {Int} t the top extent
1071 * @param {Int} r the right extent
1072 * @param {Int} b the bottom extent
1073 * @param {Int} l the left extent
1076 YAHOO.util.Region = function(t, r, b, l) {
1079 * The region's top extent
1086 * The region's top extent as index, for symmetry with set/getXY
1093 * The region's right extent
1100 * The region's bottom extent
1107 * The region's left extent
1114 * The region's left extent as index, for symmetry with set/getXY
1122 * Returns true if this region contains the region passed in
1124 * @param {Region} region The region to evaluate
1125 * @return {Boolean} True if the region is contained with this region,
1128 YAHOO.util.Region.prototype.contains = function(region) {
1129 return ( region.left >= this.left &&
1130 region.right <= this.right &&
1131 region.top >= this.top &&
1132 region.bottom <= this.bottom );
1134 // this.logger.debug("does " + this + " contain " + region + " ... " + ret);
1138 * Returns the area of the region
1140 * @return {Int} the region's area
1142 YAHOO.util.Region.prototype.getArea = function() {
1143 return ( (this.bottom - this.top) * (this.right - this.left) );
1147 * Returns the region where the passed in region overlaps with this one
1149 * @param {Region} region The region that intersects
1150 * @return {Region} The overlap region, or null if there is no overlap
1152 YAHOO.util.Region.prototype.intersect = function(region) {
1153 var t = Math.max( this.top, region.top );
1154 var r = Math.min( this.right, region.right );
1155 var b = Math.min( this.bottom, region.bottom );
1156 var l = Math.max( this.left, region.left );
1158 if (b >= t && r >= l) {
1159 return new YAHOO.util.Region(t, r, b, l);
1166 * Returns the region representing the smallest region that can contain both
1167 * the passed in region and this region.
1169 * @param {Region} region The region that to create the union with
1170 * @return {Region} The union region
1172 YAHOO.util.Region.prototype.union = function(region) {
1173 var t = Math.min( this.top, region.top );
1174 var r = Math.max( this.right, region.right );
1175 var b = Math.max( this.bottom, region.bottom );
1176 var l = Math.min( this.left, region.left );
1178 return new YAHOO.util.Region(t, r, b, l);
1184 * @return string the region properties
1186 YAHOO.util.Region.prototype.toString = function() {
1187 return ( "Region {" +
1188 "top: " + this.top +
1189 ", right: " + this.right +
1190 ", bottom: " + this.bottom +
1191 ", left: " + this.left +
1196 * Returns a region that is occupied by the DOM element
1198 * @param {HTMLElement} el The element
1199 * @return {Region} The region that the element occupies
1202 YAHOO.util.Region.getRegion = function(el) {
1203 var p = YAHOO.util.Dom.getXY(el);
1206 var r = p[0] + el.offsetWidth;
1207 var b = p[1] + el.offsetHeight;
1210 return new YAHOO.util.Region(t, r, b, l);
1213 /////////////////////////////////////////////////////////////////////////////
1217 * A point is a region that is special in that it represents a single point on
1219 * @namespace YAHOO.util
1221 * @param {Int} x The X position of the point
1222 * @param {Int} y The Y position of the point
1224 * @extends YAHOO.util.Region
1226 YAHOO.util.Point = function(x, y) {
1227 if (YAHOO.lang.isArray(x)) { // accept input from Dom.getXY, Event.getXY, etc.
1228 y = x[1]; // dont blow away x yet
1233 * The X position of the point, which is also the right, left and index zero (for Dom.getXY symmetry)
1238 this.x = this.right = this.left = this[0] = x;
1241 * The Y position of the point, which is also the top, bottom and index one (for Dom.getXY symmetry)
1245 this.y = this.top = this.bottom = this[1] = y;
1248 YAHOO.util.Point.prototype = new YAHOO.util.Region();
1250 YAHOO.register("dom", YAHOO.util.Dom, {version: "2.3.0", build: "442"});