2 Copyright (c) 2009, 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 // for use with generateId (global to save state if Dom is overwritten)
15 YAHOO.env._id_counter = YAHOO.env._id_counter || 0;
21 trim = YAHOO.lang.trim,
22 propertyCache = {}, // for faster hyphen converts
23 reCache = {}, // cache className regexes
24 RE_TABLE = /^t(?:able|d|h)$/i, // for _calcBorders
28 document = window.document,
29 documentElement = document.documentElement,
32 OWNER_DOCUMENT = 'ownerDocument',
33 DEFAULT_VIEW = 'defaultView',
34 DOCUMENT_ELEMENT = 'documentElement',
35 COMPAT_MODE = 'compatMode',
36 OFFSET_LEFT = 'offsetLeft',
37 OFFSET_TOP = 'offsetTop',
38 OFFSET_PARENT = 'offsetParent',
39 PARENT_NODE = 'parentNode',
40 NODE_TYPE = 'nodeType',
42 SCROLL_LEFT = 'scrollLeft',
43 SCROLL_TOP = 'scrollTop',
44 GET_BOUNDING_CLIENT_RECT = 'getBoundingClientRect',
45 GET_COMPUTED_STYLE = 'getComputedStyle',
46 CURRENT_STYLE = 'currentStyle',
47 CSS1_COMPAT = 'CSS1Compat',
48 _BACK_COMPAT = 'BackCompat',
49 _CLASS = 'class', // underscore due to reserved word
50 CLASS_NAME = 'className',
53 C_START = '(?:^|\\s)',
56 POSITION = 'position',
58 RELATIVE = 'relative',
62 BORDER_LEFT_WIDTH = 'borderLeftWidth',
63 BORDER_TOP_WIDTH = 'borderTopWidth',
72 * Provides helper methods for DOM elements.
73 * @namespace YAHOO.util
75 * @requires yahoo, event
78 CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
89 * Returns an HTMLElement reference.
91 * @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.
92 * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
95 var id, nodes, c, i, len, attr;
98 if (el[NODE_TYPE] || el.item) { // Node, or NodeList
102 if (typeof el === 'string') { // id
104 el = document.getElementById(el);
105 attr = (el) ? el.attributes : null;
106 if (el && attr && attr.id && attr.id.value === id) { // IE: avoid false match on "name" attribute
108 } else if (el && document.all) { // filter by name
110 nodes = document.all[id];
111 for (i = 0, len = nodes.length; i < len; ++i) {
112 if (nodes[i].id === id) {
120 if (YAHOO.util.Element && el instanceof YAHOO.util.Element) {
121 el = el.get('element');
124 if ('length' in el) { // array-like
126 for (i = 0, len = el.length; i < len; ++i) {
127 c[c.length] = Y.Dom.get(el[i]);
133 return el; // some other object, just pass it back
139 getComputedStyle: function(el, property) {
140 if (window[GET_COMPUTED_STYLE]) {
141 return el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null)[property];
142 } else if (el[CURRENT_STYLE]) {
143 return Y.Dom.IE_ComputedStyle.get(el, property);
148 * Normalizes currentStyle and ComputedStyle.
150 * @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.
151 * @param {String} property The style property whose value is returned.
152 * @return {String | Array} The current value of the style property for the element(s).
154 getStyle: function(el, property) {
155 return Y.Dom.batch(el, Y.Dom._getStyle, property);
158 // branching at load instead of runtime
159 _getStyle: function() {
160 if (window[GET_COMPUTED_STYLE]) { // W3C DOM method
161 return function(el, property) {
162 property = (property === 'float') ? property = 'cssFloat' :
163 Y.Dom._toCamel(property);
165 var value = el.style[property],
169 computed = el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null);
170 if (computed) { // test computed before touching for safari
171 value = computed[property];
177 } else if (documentElement[CURRENT_STYLE]) {
178 return function(el, property) {
182 case 'opacity' :// IE opacity uses filter
184 try { // will error if no DXImageTransform
185 value = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
188 try { // make sure its in the document
189 value = el.filters('alpha').opacity;
191 YAHOO.log('getStyle: IE filter failed',
196 case 'float': // fix reserved word
197 property = 'styleFloat'; // fall through
199 property = Y.Dom._toCamel(property);
200 value = el[CURRENT_STYLE] ? el[CURRENT_STYLE][property] : null;
201 return ( el.style[property] || value );
208 * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
210 * @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.
211 * @param {String} property The style property to be set.
212 * @param {String} val The value to apply to the given property.
214 setStyle: function(el, property, val) {
215 Y.Dom.batch(el, Y.Dom._setStyle, { prop: property, val: val });
218 _setStyle: function() {
220 return function(el, args) {
221 var property = Y.Dom._toCamel(args.prop),
227 if ( lang.isString(el.style.filter) ) { // in case not appended
228 el.style.filter = 'alpha(opacity=' + val * 100 + ')';
230 if (!el[CURRENT_STYLE] || !el[CURRENT_STYLE].hasLayout) {
231 el.style.zoom = 1; // when no layout or cant tell
236 property = 'styleFloat';
238 el.style[property] = val;
241 YAHOO.log('element ' + el + ' is undefined', 'error', 'Dom');
245 return function(el, args) {
246 var property = Y.Dom._toCamel(args.prop),
249 if (property == 'float') {
250 property = 'cssFloat';
252 el.style[property] = val;
254 YAHOO.log('element ' + el + ' is undefined', 'error', 'Dom');
262 * Gets the current position of an element based on page coordinates.
263 * Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
265 * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM
266 * reference, or an Array of IDs and/or HTMLElements
267 * @return {Array} The XY position of the element(s)
269 getXY: function(el) {
270 return Y.Dom.batch(el, Y.Dom._getXY);
273 _canPosition: function(el) {
274 return ( Y.Dom._getStyle(el, 'display') !== 'none' && Y.Dom._inDoc(el) );
278 if (document[DOCUMENT_ELEMENT][GET_BOUNDING_CLIENT_RECT]) {
279 return function(node) {
280 var scrollLeft, scrollTop, box, doc,
281 off1, off2, mode, bLeft, bTop,
282 floor = Math.floor, // TODO: round?
285 if (Y.Dom._canPosition(node)) {
286 box = node[GET_BOUNDING_CLIENT_RECT]();
287 doc = node[OWNER_DOCUMENT];
288 scrollLeft = Y.Dom.getDocumentScrollLeft(doc);
289 scrollTop = Y.Dom.getDocumentScrollTop(doc);
290 xy = [floor(box[LEFT]), floor(box[TOP])];
292 if (isIE && UA.ie < 8) { // IE < 8: viewport off by 2
295 mode = doc[COMPAT_MODE];
298 if (mode !== _BACK_COMPAT) {
304 if ((mode === _BACK_COMPAT)) {
305 bLeft = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_LEFT_WIDTH);
306 bTop = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_TOP_WIDTH);
307 if (bLeft !== MEDIUM) {
308 off1 = parseInt(bLeft, 10);
310 if (bTop !== MEDIUM) {
311 off2 = parseInt(bTop, 10);
320 if ((scrollTop || scrollLeft)) {
325 // gecko may return sub-pixel (non-int) values
326 xy[0] = floor(xy[0]);
327 xy[1] = floor(xy[1]);
329 YAHOO.log('getXY failed: element not positionable (either not in a document or not displayed)', 'error', 'Dom');
335 return function(node) { // ff2, safari: manually calculate by crawling up offsetParents
336 var docScrollLeft, docScrollTop,
337 scrollTop, scrollLeft,
342 if (Y.Dom._canPosition(node) ) {
343 xy = [node[OFFSET_LEFT], node[OFFSET_TOP]];
344 docScrollLeft = Y.Dom.getDocumentScrollLeft(node[OWNER_DOCUMENT]);
345 docScrollTop = Y.Dom.getDocumentScrollTop(node[OWNER_DOCUMENT]);
347 // TODO: refactor with !! or just falsey
348 bCheck = ((isGecko || UA.webkit > 519) ? true : false);
350 // TODO: worth refactoring for TOP/LEFT only?
351 while ((parentNode = parentNode[OFFSET_PARENT])) {
352 xy[0] += parentNode[OFFSET_LEFT];
353 xy[1] += parentNode[OFFSET_TOP];
355 xy = Y.Dom._calcBorders(parentNode, xy);
359 // account for any scrolled ancestors
360 if (Y.Dom._getStyle(node, POSITION) !== FIXED) {
363 while ((parentNode = parentNode[PARENT_NODE]) && parentNode[TAG_NAME]) {
364 scrollTop = parentNode[SCROLL_TOP];
365 scrollLeft = parentNode[SCROLL_LEFT];
367 //Firefox does something funky with borders when overflow is not visible.
368 if (isGecko && (Y.Dom._getStyle(parentNode, 'overflow') !== 'visible')) {
369 xy = Y.Dom._calcBorders(parentNode, xy);
372 if (scrollTop || scrollLeft) {
377 xy[0] += docScrollLeft;
378 xy[1] += docScrollTop;
381 //Fix FIXED position -- add scrollbars
383 xy[0] -= docScrollLeft;
384 xy[1] -= docScrollTop;
385 } else if (isSafari || isGecko) {
386 xy[0] += docScrollLeft;
387 xy[1] += docScrollTop;
390 //Round the numbers so we get sane data back
391 xy[0] = Math.floor(xy[0]);
392 xy[1] = Math.floor(xy[1]);
394 YAHOO.log('getXY failed: element not positionable (either not in a document or not displayed)', 'error', 'Dom');
399 }(), // NOTE: Executing for loadtime branching
402 * 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).
404 * @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
405 * @return {Number | Array} The X position of the element(s)
408 var f = function(el) {
409 return Y.Dom.getXY(el)[0];
412 return Y.Dom.batch(el, f, Y.Dom, true);
416 * 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).
418 * @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
419 * @return {Number | Array} The Y position of the element(s)
422 var f = function(el) {
423 return Y.Dom.getXY(el)[1];
426 return Y.Dom.batch(el, f, Y.Dom, true);
430 * Set the position of an html element in page coordinates, regardless of how the element is positioned.
431 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
433 * @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
434 * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
435 * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
437 setXY: function(el, pos, noRetry) {
438 Y.Dom.batch(el, Y.Dom._setXY, { pos: pos, noRetry: noRetry });
441 _setXY: function(node, args) {
442 var pos = Y.Dom._getStyle(node, POSITION),
443 setStyle = Y.Dom.setStyle,
445 noRetry = args.noRetry,
447 delta = [ // assuming pixels; if not we will have to retry
448 parseInt( Y.Dom.getComputedStyle(node, LEFT), 10 ),
449 parseInt( Y.Dom.getComputedStyle(node, TOP), 10 )
455 if (pos == 'static') { // default to relative
457 setStyle(node, POSITION, pos);
460 currentXY = Y.Dom._getXY(node);
462 if (!xy || currentXY === false) { // has to be part of doc to have xy
463 YAHOO.log('xy failed: node not available', 'error', 'Node');
467 if ( isNaN(delta[0]) ) {// in case of 'auto'
468 delta[0] = (pos == RELATIVE) ? 0 : node[OFFSET_LEFT];
470 if ( isNaN(delta[1]) ) { // in case of 'auto'
471 delta[1] = (pos == RELATIVE) ? 0 : node[OFFSET_TOP];
474 if (xy[0] !== null) { // from setX
475 setStyle(node, LEFT, xy[0] - currentXY[0] + delta[0] + 'px');
478 if (xy[1] !== null) { // from setY
479 setStyle(node, TOP, xy[1] - currentXY[1] + delta[1] + 'px');
483 newXY = Y.Dom._getXY(node);
485 // if retry is true, try one more time if we miss
486 if ( (xy[0] !== null && newXY[0] != xy[0]) ||
487 (xy[1] !== null && newXY[1] != xy[1]) ) {
488 Y.Dom._setXY(node, { pos: xy, noRetry: true });
492 YAHOO.log('setXY setting position to ' + xy, 'info', 'Node');
496 * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
497 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
499 * @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.
500 * @param {Int} x The value to use as the X coordinate for the element(s).
502 setX: function(el, x) {
503 Y.Dom.setXY(el, [x, null]);
507 * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
508 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
510 * @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.
511 * @param {Int} x To use as the Y coordinate for the element(s).
513 setY: function(el, y) {
514 Y.Dom.setXY(el, [null, y]);
518 * Returns the region position of the given element.
519 * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
521 * @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.
522 * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
524 getRegion: function(el) {
525 var f = function(el) {
527 if ( Y.Dom._canPosition(el) ) {
528 region = Y.Region.getRegion(el);
529 YAHOO.log('getRegion returning ' + region, 'info', 'Dom');
531 YAHOO.log('getRegion failed: element not positionable (either not in a document or not displayed)', 'error', 'Dom');
537 return Y.Dom.batch(el, f, Y.Dom, true);
541 * Returns the width of the client (viewport).
542 * @method getClientWidth
543 * @deprecated Now using getViewportWidth. This interface left intact for back compat.
544 * @return {Int} The width of the viewable area of the page.
546 getClientWidth: function() {
547 return Y.Dom.getViewportWidth();
551 * Returns the height of the client (viewport).
552 * @method getClientHeight
553 * @deprecated Now using getViewportHeight. This interface left intact for back compat.
554 * @return {Int} The height of the viewable area of the page.
556 getClientHeight: function() {
557 return Y.Dom.getViewportHeight();
561 * Returns an array of HTMLElements with the given class.
562 * For optimized performance, include a tag and/or root node when possible.
563 * Note: This method operates against a live collection, so modifying the
564 * collection in the callback (removing/appending nodes, etc.) will have
565 * side effects. Instead you should iterate the returned nodes array,
566 * as you would with the native "getElementsByTagName" method.
567 * @method getElementsByClassName
568 * @param {String} className The class name to match against
569 * @param {String} tag (optional) The tag name of the elements being collected
570 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point.
571 * This element is not included in the className scan.
572 * @param {Function} apply (optional) A function to apply to each element when found
573 * @param {Any} o (optional) An optional arg that is passed to the supplied method
574 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
575 * @return {Array} An array of elements that have the given class name
577 getElementsByClassName: function(className, tag, root, apply, o, overrides) {
579 root = (root) ? Y.Dom.get(root) : null || document;
585 elements = root.getElementsByTagName(tag),
586 hasClass = Y.Dom.hasClass;
588 for (var i = 0, len = elements.length; i < len; ++i) {
589 if ( hasClass(elements[i], className) ) {
590 nodes[nodes.length] = elements[i];
595 Y.Dom.batch(nodes, apply, o, overrides);
602 * Determines whether an HTMLElement has the given className.
604 * @param {String | HTMLElement | Array} el The element or collection to test
605 * @param {String} className the class name to search for
606 * @return {Boolean | Array} A boolean value or array of boolean values
608 hasClass: function(el, className) {
609 return Y.Dom.batch(el, Y.Dom._hasClass, className);
612 _hasClass: function(el, className) {
616 if (el && className) {
617 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
618 if (className.exec) {
619 ret = className.test(current);
621 ret = className && (SPACE + current + SPACE).
622 indexOf(SPACE + className + SPACE) > -1;
625 YAHOO.log('hasClass called with invalid arguments', 'warn', 'Dom');
632 * Adds a class name to a given element or collection of elements.
634 * @param {String | HTMLElement | Array} el The element or collection to add the class to
635 * @param {String} className the class name to add to the class attribute
636 * @return {Boolean | Array} A pass/fail boolean or array of booleans
638 addClass: function(el, className) {
639 return Y.Dom.batch(el, Y.Dom._addClass, className);
642 _addClass: function(el, className) {
646 if (el && className) {
647 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
648 if ( !Y.Dom._hasClass(el, className) ) {
649 Y.Dom.setAttribute(el, CLASS_NAME, trim(current + SPACE + className));
653 YAHOO.log('addClass called with invalid arguments', 'warn', 'Dom');
660 * Removes a class name from a given element or collection of elements.
661 * @method removeClass
662 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
663 * @param {String} className the class name to remove from the class attribute
664 * @return {Boolean | Array} A pass/fail boolean or array of booleans
666 removeClass: function(el, className) {
667 return Y.Dom.batch(el, Y.Dom._removeClass, className);
670 _removeClass: function(el, className) {
676 if (el && className) {
677 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
678 Y.Dom.setAttribute(el, CLASS_NAME, current.replace(Y.Dom._getClassRegex(className), EMPTY));
680 newClass = Y.Dom._getAttribute(el, CLASS_NAME);
681 if (current !== newClass) { // else nothing changed
682 Y.Dom.setAttribute(el, CLASS_NAME, trim(newClass)); // trim after comparing to current class
685 if (Y.Dom._getAttribute(el, CLASS_NAME) === '') { // remove class attribute if empty
686 attr = (el.hasAttribute && el.hasAttribute(_CLASS)) ? _CLASS : CLASS_NAME;
687 YAHOO.log('removeClass removing empty class attribute', 'info', 'Dom');
688 el.removeAttribute(attr);
693 YAHOO.log('removeClass called with invalid arguments', 'warn', 'Dom');
700 * Replace a class with another class for a given element or collection of elements.
701 * If no oldClassName is present, the newClassName is simply added.
702 * @method replaceClass
703 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
704 * @param {String} oldClassName the class name to be replaced
705 * @param {String} newClassName the class name that will be replacing the old class name
706 * @return {Boolean | Array} A pass/fail boolean or array of booleans
708 replaceClass: function(el, oldClassName, newClassName) {
709 return Y.Dom.batch(el, Y.Dom._replaceClass, { from: oldClassName, to: newClassName });
712 _replaceClass: function(el, classObj) {
719 if (el && classObj) {
720 from = classObj.from;
725 } else if (!from) { // just add if no "from"
726 ret = Y.Dom._addClass(el, classObj.to);
727 } else if (from !== to) { // else nothing to replace
728 // May need to lead with DBLSPACE?
729 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
730 className = (SPACE + current.replace(Y.Dom._getClassRegex(from), SPACE + to)).
731 split(Y.Dom._getClassRegex(to));
733 // insert to into what would have been the first occurrence slot
734 className.splice(1, 0, SPACE + to);
735 Y.Dom.setAttribute(el, CLASS_NAME, trim(className.join(EMPTY)));
739 YAHOO.log('replaceClass called with invalid arguments', 'warn', 'Dom');
746 * Returns an ID and applies it to the element "el", if provided.
748 * @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).
749 * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
750 * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
752 generateId: function(el, prefix) {
753 prefix = prefix || 'yui-gen';
755 var f = function(el) {
756 if (el && el.id) { // do not override existing ID
757 YAHOO.log('generateId returning existing id ' + el.id, 'info', 'Dom');
761 var id = prefix + YAHOO.env._id_counter++;
762 YAHOO.log('generateId generating ' + id, 'info', 'Dom');
765 if (el[OWNER_DOCUMENT] && el[OWNER_DOCUMENT].getElementById(id)) { // in case one already exists
766 // use failed id plus prefix to help ensure uniqueness
767 return Y.Dom.generateId(el, id + prefix);
775 // batch fails when no element, so just generate and return single ID
776 return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
780 * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
782 * @param {String | HTMLElement} haystack The possible ancestor
783 * @param {String | HTMLElement} needle The possible descendent
784 * @return {Boolean} Whether or not the haystack is an ancestor of needle
786 isAncestor: function(haystack, needle) {
787 haystack = Y.Dom.get(haystack);
788 needle = Y.Dom.get(needle);
792 if ( (haystack && needle) && (haystack[NODE_TYPE] && needle[NODE_TYPE]) ) {
793 if (haystack.contains && haystack !== needle) { // contains returns true when equal
794 ret = haystack.contains(needle);
796 else if (haystack.compareDocumentPosition) { // gecko
797 ret = !!(haystack.compareDocumentPosition(needle) & 16);
800 YAHOO.log('isAncestor failed; invalid input: ' + haystack + ',' + needle, 'error', 'Dom');
802 YAHOO.log('isAncestor(' + haystack + ',' + needle + ' returning ' + ret, 'info', 'Dom');
807 * Determines whether an HTMLElement is present in the current document.
809 * @param {String | HTMLElement} el The element to search for
810 * @param {Object} doc An optional document to search, defaults to element's owner document
811 * @return {Boolean} Whether or not the element is present in the current document
813 inDocument: function(el, doc) {
814 return Y.Dom._inDoc(Y.Dom.get(el), doc);
817 _inDoc: function(el, doc) {
819 if (el && el[TAG_NAME]) {
820 doc = doc || el[OWNER_DOCUMENT];
821 ret = Y.Dom.isAncestor(doc[DOCUMENT_ELEMENT], el);
823 YAHOO.log('inDocument failed: invalid input', 'error', 'Dom');
829 * Returns an array of HTMLElements that pass the test applied by supplied boolean method.
830 * For optimized performance, include a tag and/or root node when possible.
831 * Note: This method operates against a live collection, so modifying the
832 * collection in the callback (removing/appending nodes, etc.) will have
833 * side effects. Instead you should iterate the returned nodes array,
834 * as you would with the native "getElementsByTagName" method.
835 * @method getElementsBy
836 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
837 * @param {String} tag (optional) The tag name of the elements being collected
838 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
839 * @param {Function} apply (optional) A function to apply to each element when found
840 * @param {Any} o (optional) An optional arg that is passed to the supplied method
841 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
842 * @return {Array} Array of HTMLElements
844 getElementsBy: function(method, tag, root, apply, o, overrides, firstOnly) {
846 root = (root) ? Y.Dom.get(root) : null || document;
853 elements = root.getElementsByTagName(tag);
855 for (var i = 0, len = elements.length; i < len; ++i) {
856 if ( method(elements[i]) ) {
861 nodes[nodes.length] = elements[i];
867 Y.Dom.batch(nodes, apply, o, overrides);
870 YAHOO.log('getElementsBy returning ' + nodes, 'info', 'Dom');
876 * Returns the first HTMLElement that passes the test applied by the supplied boolean method.
877 * @method getElementBy
878 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
879 * @param {String} tag (optional) The tag name of the elements being collected
880 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
881 * @return {HTMLElement}
883 getElementBy: function(method, tag, root) {
884 return Y.Dom.getElementsBy(method, tag, root, null, null, null, true);
888 * Runs the supplied method against each item in the Collection/Array.
889 * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
891 * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
892 * @param {Function} method The method to apply to the element(s)
893 * @param {Any} o (optional) An optional arg that is passed to the supplied method
894 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
895 * @return {Any | Array} The return value(s) from the supplied method
897 batch: function(el, method, o, overrides) {
899 scope = (overrides) ? o : window;
901 el = (el && (el[TAG_NAME] || el.item)) ? el : Y.Dom.get(el); // skip get() when possible
903 if (el[TAG_NAME] || el.length === undefined) { // element or not array-like
904 return method.call(scope, el, o);
907 for (var i = 0; i < el.length; ++i) {
908 collection[collection.length] = method.call(scope, el[i], o);
911 YAHOO.log('batch called with invalid arguments', 'warn', 'Dom');
918 * Returns the height of the document.
919 * @method getDocumentHeight
920 * @return {Int} The height of the actual document (which includes the body and its margin).
922 getDocumentHeight: function() {
923 var scrollHeight = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollHeight : documentElement.scrollHeight,
924 h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
926 YAHOO.log('getDocumentHeight returning ' + h, 'info', 'Dom');
931 * Returns the width of the document.
932 * @method getDocumentWidth
933 * @return {Int} The width of the actual document (which includes the body and its margin).
935 getDocumentWidth: function() {
936 var scrollWidth = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollWidth : documentElement.scrollWidth,
937 w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
938 YAHOO.log('getDocumentWidth returning ' + w, 'info', 'Dom');
943 * Returns the current height of the viewport.
944 * @method getViewportHeight
945 * @return {Int} The height of the viewable area of the page (excludes scrollbars).
947 getViewportHeight: function() {
948 var height = self.innerHeight, // Safari, Opera
949 mode = document[COMPAT_MODE];
951 if ( (mode || isIE) && !isOpera ) { // IE, Gecko
952 height = (mode == CSS1_COMPAT) ?
953 documentElement.clientHeight : // Standards
954 document.body.clientHeight; // Quirks
957 YAHOO.log('getViewportHeight returning ' + height, 'info', 'Dom');
962 * Returns the current width of the viewport.
963 * @method getViewportWidth
964 * @return {Int} The width of the viewable area of the page (excludes scrollbars).
967 getViewportWidth: function() {
968 var width = self.innerWidth, // Safari
969 mode = document[COMPAT_MODE];
971 if (mode || isIE) { // IE, Gecko, Opera
972 width = (mode == CSS1_COMPAT) ?
973 documentElement.clientWidth : // Standards
974 document.body.clientWidth; // Quirks
976 YAHOO.log('getViewportWidth returning ' + width, 'info', 'Dom');
981 * Returns the nearest ancestor that passes the test applied by supplied boolean method.
982 * For performance reasons, IDs are not accepted and argument validation omitted.
983 * @method getAncestorBy
984 * @param {HTMLElement} node The HTMLElement to use as the starting point
985 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
986 * @return {Object} HTMLElement or null if not found
988 getAncestorBy: function(node, method) {
989 while ( (node = node[PARENT_NODE]) ) { // NOTE: assignment
990 if ( Y.Dom._testElement(node, method) ) {
991 YAHOO.log('getAncestorBy returning ' + node, 'info', 'Dom');
996 YAHOO.log('getAncestorBy returning null (no ancestor passed test)', 'error', 'Dom');
1001 * Returns the nearest ancestor with the given className.
1002 * @method getAncestorByClassName
1003 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1004 * @param {String} className
1005 * @return {Object} HTMLElement
1007 getAncestorByClassName: function(node, className) {
1008 node = Y.Dom.get(node);
1010 YAHOO.log('getAncestorByClassName failed: invalid node argument', 'error', 'Dom');
1013 var method = function(el) { return Y.Dom.hasClass(el, className); };
1014 return Y.Dom.getAncestorBy(node, method);
1018 * Returns the nearest ancestor with the given tagName.
1019 * @method getAncestorByTagName
1020 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1021 * @param {String} tagName
1022 * @return {Object} HTMLElement
1024 getAncestorByTagName: function(node, tagName) {
1025 node = Y.Dom.get(node);
1027 YAHOO.log('getAncestorByTagName failed: invalid node argument', 'error', 'Dom');
1030 var method = function(el) {
1031 return el[TAG_NAME] && el[TAG_NAME].toUpperCase() == tagName.toUpperCase();
1034 return Y.Dom.getAncestorBy(node, method);
1038 * Returns the previous sibling that is an HTMLElement.
1039 * For performance reasons, IDs are not accepted and argument validation omitted.
1040 * Returns the nearest HTMLElement sibling if no method provided.
1041 * @method getPreviousSiblingBy
1042 * @param {HTMLElement} node The HTMLElement to use as the starting point
1043 * @param {Function} method A boolean function used to test siblings
1044 * that receives the sibling node being tested as its only argument
1045 * @return {Object} HTMLElement or null if not found
1047 getPreviousSiblingBy: function(node, method) {
1049 node = node.previousSibling;
1050 if ( Y.Dom._testElement(node, method) ) {
1058 * Returns the previous sibling that is an HTMLElement
1059 * @method getPreviousSibling
1060 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1061 * @return {Object} HTMLElement or null if not found
1063 getPreviousSibling: function(node) {
1064 node = Y.Dom.get(node);
1066 YAHOO.log('getPreviousSibling failed: invalid node argument', 'error', 'Dom');
1070 return Y.Dom.getPreviousSiblingBy(node);
1074 * Returns the next HTMLElement sibling that passes the boolean method.
1075 * For performance reasons, IDs are not accepted and argument validation omitted.
1076 * Returns the nearest HTMLElement sibling if no method provided.
1077 * @method getNextSiblingBy
1078 * @param {HTMLElement} node The HTMLElement to use as the starting point
1079 * @param {Function} method A boolean function used to test siblings
1080 * that receives the sibling node being tested as its only argument
1081 * @return {Object} HTMLElement or null if not found
1083 getNextSiblingBy: function(node, method) {
1085 node = node.nextSibling;
1086 if ( Y.Dom._testElement(node, method) ) {
1094 * Returns the next sibling that is an HTMLElement
1095 * @method getNextSibling
1096 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1097 * @return {Object} HTMLElement or null if not found
1099 getNextSibling: function(node) {
1100 node = Y.Dom.get(node);
1102 YAHOO.log('getNextSibling failed: invalid node argument', 'error', 'Dom');
1106 return Y.Dom.getNextSiblingBy(node);
1110 * Returns the first HTMLElement child that passes the test method.
1111 * @method getFirstChildBy
1112 * @param {HTMLElement} node The HTMLElement to use as the starting point
1113 * @param {Function} method A boolean function used to test children
1114 * that receives the node being tested as its only argument
1115 * @return {Object} HTMLElement or null if not found
1117 getFirstChildBy: function(node, method) {
1118 var child = ( Y.Dom._testElement(node.firstChild, method) ) ? node.firstChild : null;
1119 return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
1123 * Returns the first HTMLElement child.
1124 * @method getFirstChild
1125 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1126 * @return {Object} HTMLElement or null if not found
1128 getFirstChild: function(node, method) {
1129 node = Y.Dom.get(node);
1131 YAHOO.log('getFirstChild failed: invalid node argument', 'error', 'Dom');
1134 return Y.Dom.getFirstChildBy(node);
1138 * Returns the last HTMLElement child that passes the test method.
1139 * @method getLastChildBy
1140 * @param {HTMLElement} node The HTMLElement to use as the starting point
1141 * @param {Function} method A boolean function used to test children
1142 * that receives the node being tested as its only argument
1143 * @return {Object} HTMLElement or null if not found
1145 getLastChildBy: function(node, method) {
1147 YAHOO.log('getLastChild failed: invalid node argument', 'error', 'Dom');
1150 var child = ( Y.Dom._testElement(node.lastChild, method) ) ? node.lastChild : null;
1151 return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
1155 * Returns the last HTMLElement child.
1156 * @method getLastChild
1157 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1158 * @return {Object} HTMLElement or null if not found
1160 getLastChild: function(node) {
1161 node = Y.Dom.get(node);
1162 return Y.Dom.getLastChildBy(node);
1166 * Returns an array of HTMLElement childNodes that pass the test method.
1167 * @method getChildrenBy
1168 * @param {HTMLElement} node The HTMLElement to start from
1169 * @param {Function} method A boolean function used to test children
1170 * that receives the node being tested as its only argument
1171 * @return {Array} A static array of HTMLElements
1173 getChildrenBy: function(node, method) {
1174 var child = Y.Dom.getFirstChildBy(node, method),
1175 children = child ? [child] : [];
1177 Y.Dom.getNextSiblingBy(child, function(node) {
1178 if ( !method || method(node) ) {
1179 children[children.length] = node;
1181 return false; // fail test to collect all children
1188 * Returns an array of HTMLElement childNodes.
1189 * @method getChildren
1190 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1191 * @return {Array} A static array of HTMLElements
1193 getChildren: function(node) {
1194 node = Y.Dom.get(node);
1196 YAHOO.log('getChildren failed: invalid node argument', 'error', 'Dom');
1199 return Y.Dom.getChildrenBy(node);
1203 * Returns the left scroll value of the document
1204 * @method getDocumentScrollLeft
1205 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1206 * @return {Int} The amount that the document is scrolled to the left
1208 getDocumentScrollLeft: function(doc) {
1209 doc = doc || document;
1210 return Math.max(doc[DOCUMENT_ELEMENT].scrollLeft, doc.body.scrollLeft);
1214 * Returns the top scroll value of the document
1215 * @method getDocumentScrollTop
1216 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1217 * @return {Int} The amount that the document is scrolled to the top
1219 getDocumentScrollTop: function(doc) {
1220 doc = doc || document;
1221 return Math.max(doc[DOCUMENT_ELEMENT].scrollTop, doc.body.scrollTop);
1225 * Inserts the new node as the previous sibling of the reference node
1226 * @method insertBefore
1227 * @param {String | HTMLElement} newNode The node to be inserted
1228 * @param {String | HTMLElement} referenceNode The node to insert the new node before
1229 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1231 insertBefore: function(newNode, referenceNode) {
1232 newNode = Y.Dom.get(newNode);
1233 referenceNode = Y.Dom.get(referenceNode);
1235 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1236 YAHOO.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
1240 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode);
1244 * Inserts the new node as the next sibling of the reference node
1245 * @method insertAfter
1246 * @param {String | HTMLElement} newNode The node to be inserted
1247 * @param {String | HTMLElement} referenceNode The node to insert the new node after
1248 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1250 insertAfter: function(newNode, referenceNode) {
1251 newNode = Y.Dom.get(newNode);
1252 referenceNode = Y.Dom.get(referenceNode);
1254 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1255 YAHOO.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
1259 if (referenceNode.nextSibling) {
1260 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode.nextSibling);
1262 return referenceNode[PARENT_NODE].appendChild(newNode);
1267 * Creates a Region based on the viewport relative to the document.
1268 * @method getClientRegion
1269 * @return {Region} A Region object representing the viewport which accounts for document scroll
1271 getClientRegion: function() {
1272 var t = Y.Dom.getDocumentScrollTop(),
1273 l = Y.Dom.getDocumentScrollLeft(),
1274 r = Y.Dom.getViewportWidth() + l,
1275 b = Y.Dom.getViewportHeight() + t;
1277 return new Y.Region(t, r, b, l);
1281 * Provides a normalized attribute interface.
1282 * @method setAttribute
1283 * @param {String | HTMLElement} el The target element for the attribute.
1284 * @param {String} attr The attribute to set.
1285 * @param {String} val The value of the attribute.
1287 setAttribute: function(el, attr, val) {
1288 Y.Dom.batch(el, Y.Dom._setAttribute, { attr: attr, val: val });
1291 _setAttribute: function(el, args) {
1292 var attr = Y.Dom._toCamel(args.attr),
1295 if (el && el.setAttribute) {
1296 if (Y.Dom.DOT_ATTRIBUTES[attr]) {
1299 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1300 el.setAttribute(attr, val);
1303 YAHOO.log('setAttribute method not available for ' + el, 'error', 'Dom');
1308 * Provides a normalized attribute interface.
1309 * @method getAttribute
1310 * @param {String | HTMLElement} el The target element for the attribute.
1311 * @param {String} attr The attribute to get.
1312 * @return {String} The current value of the attribute.
1314 getAttribute: function(el, attr) {
1315 return Y.Dom.batch(el, Y.Dom._getAttribute, attr);
1319 _getAttribute: function(el, attr) {
1321 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1323 if (el && el.getAttribute) {
1324 val = el.getAttribute(attr, 2);
1326 YAHOO.log('getAttribute method not available for ' + el, 'error', 'Dom');
1332 _toCamel: function(property) {
1333 var c = propertyCache;
1336 return l.toUpperCase();
1339 return c[property] || (c[property] = property.indexOf('-') === -1 ?
1341 property.replace( /-([a-z])/gi, tU ));
1344 _getClassRegex: function(className) {
1346 if (className !== undefined) { // allow empty string to pass
1347 if (className.exec) { // already a RegExp
1350 re = reCache[className];
1352 // escape special chars (".", "[", etc.)
1353 className = className.replace(Y.Dom._patterns.CLASS_RE_TOKENS, '\\$1');
1354 re = reCache[className] = new RegExp(C_START + className + C_END, G);
1362 ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
1363 CLASS_RE_TOKENS: /([\.\(\)\^\$\*\+\?\|\[\]\{\}\\])/g
1367 _testElement: function(node, method) {
1368 return node && node[NODE_TYPE] == 1 && ( !method || method(node) );
1371 _calcBorders: function(node, xy2) {
1372 var t = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_TOP_WIDTH), 10) || 0,
1373 l = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_LEFT_WIDTH), 10) || 0;
1375 if (RE_TABLE.test(node[TAG_NAME])) {
1386 var _getComputedStyle = Y.Dom[GET_COMPUTED_STYLE];
1387 // fix opera computedStyle default color unit (convert to rgb)
1389 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1390 var val = _getComputedStyle(node, att);
1391 if (RE_COLOR.test(att)) {
1392 val = Y.Dom.Color.toRGB(val);
1400 // safari converts transparent to rgba(), others use "transparent"
1402 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1403 var val = _getComputedStyle(node, att);
1405 if (val === 'rgba(0, 0, 0, 0)') {
1406 val = 'transparent';
1414 if (UA.ie && UA.ie >= 8 && document.documentElement.hasAttribute) { // IE 8 standards
1415 Y.Dom.DOT_ATTRIBUTES.type = true; // IE 8 errors on input.setAttribute('type')
1419 * A region is a representation of an object on a grid. It is defined
1420 * by the top, right, bottom, left extents, so is rectangular by default. If
1421 * other shapes are required, this class could be extended to support it.
1422 * @namespace YAHOO.util
1424 * @param {Int} t the top extent
1425 * @param {Int} r the right extent
1426 * @param {Int} b the bottom extent
1427 * @param {Int} l the left extent
1430 YAHOO.util.Region = function(t, r, b, l) {
1433 * The region's top extent
1440 * The region's top extent
1447 * The region's top extent as index, for symmetry with set/getXY
1454 * The region's right extent
1461 * The region's bottom extent
1468 * The region's left extent
1475 * The region's left extent
1482 * The region's left extent as index, for symmetry with set/getXY
1489 * The region's total width
1493 this.width = this.right - this.left;
1496 * The region's total height
1500 this.height = this.bottom - this.top;
1504 * Returns true if this region contains the region passed in
1506 * @param {Region} region The region to evaluate
1507 * @return {Boolean} True if the region is contained with this region,
1510 YAHOO.util.Region.prototype.contains = function(region) {
1511 return ( region.left >= this.left &&
1512 region.right <= this.right &&
1513 region.top >= this.top &&
1514 region.bottom <= this.bottom );
1516 // this.logger.debug("does " + this + " contain " + region + " ... " + ret);
1520 * Returns the area of the region
1522 * @return {Int} the region's area
1524 YAHOO.util.Region.prototype.getArea = function() {
1525 return ( (this.bottom - this.top) * (this.right - this.left) );
1529 * Returns the region where the passed in region overlaps with this one
1531 * @param {Region} region The region that intersects
1532 * @return {Region} The overlap region, or null if there is no overlap
1534 YAHOO.util.Region.prototype.intersect = function(region) {
1535 var t = Math.max( this.top, region.top ),
1536 r = Math.min( this.right, region.right ),
1537 b = Math.min( this.bottom, region.bottom ),
1538 l = Math.max( this.left, region.left );
1540 if (b >= t && r >= l) {
1541 return new YAHOO.util.Region(t, r, b, l);
1548 * Returns the region representing the smallest region that can contain both
1549 * the passed in region and this region.
1551 * @param {Region} region The region that to create the union with
1552 * @return {Region} The union region
1554 YAHOO.util.Region.prototype.union = function(region) {
1555 var t = Math.min( this.top, region.top ),
1556 r = Math.max( this.right, region.right ),
1557 b = Math.max( this.bottom, region.bottom ),
1558 l = Math.min( this.left, region.left );
1560 return new YAHOO.util.Region(t, r, b, l);
1566 * @return string the region properties
1568 YAHOO.util.Region.prototype.toString = function() {
1569 return ( "Region {" +
1570 "top: " + this.top +
1571 ", right: " + this.right +
1572 ", bottom: " + this.bottom +
1573 ", left: " + this.left +
1574 ", height: " + this.height +
1575 ", width: " + this.width +
1580 * Returns a region that is occupied by the DOM element
1582 * @param {HTMLElement} el The element
1583 * @return {Region} The region that the element occupies
1586 YAHOO.util.Region.getRegion = function(el) {
1587 var p = YAHOO.util.Dom.getXY(el),
1589 r = p[0] + el.offsetWidth,
1590 b = p[1] + el.offsetHeight,
1593 return new YAHOO.util.Region(t, r, b, l);
1596 /////////////////////////////////////////////////////////////////////////////
1600 * A point is a region that is special in that it represents a single point on
1602 * @namespace YAHOO.util
1604 * @param {Int} x The X position of the point
1605 * @param {Int} y The Y position of the point
1607 * @extends YAHOO.util.Region
1609 YAHOO.util.Point = function(x, y) {
1610 if (YAHOO.lang.isArray(x)) { // accept input from Dom.getXY, Event.getXY, etc.
1611 y = x[1]; // dont blow away x yet
1615 YAHOO.util.Point.superclass.constructor.call(this, y, x, y, x);
1618 YAHOO.extend(YAHOO.util.Point, YAHOO.util.Region);
1622 * Add style management functionality to DOM.
1628 CLIENT_TOP = 'clientTop',
1629 CLIENT_LEFT = 'clientLeft',
1630 PARENT_NODE = 'parentNode',
1632 HAS_LAYOUT = 'hasLayout',
1634 OPACITY = 'opacity',
1636 BORDER_LEFT_WIDTH = 'borderLeftWidth',
1637 BORDER_TOP_WIDTH = 'borderTopWidth',
1638 BORDER_RIGHT_WIDTH = 'borderRightWidth',
1639 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
1640 VISIBLE = 'visible',
1641 TRANSPARENT = 'transparent',
1645 CURRENT_STYLE = 'currentStyle',
1647 // IE getComputedStyle
1648 // TODO: unit-less lineHeight (e.g. 1.22)
1649 re_size = /^width|height$/,
1650 re_unit = /^(\d[.\d]*)+(em|ex|px|gd|rem|vw|vh|vm|ch|mm|cm|in|pt|pc|deg|rad|ms|s|hz|khz|%){1}?/i,
1653 get: function(el, property) {
1655 current = el[CURRENT_STYLE][property];
1657 if (property === OPACITY) {
1658 value = Y.Dom.getStyle(el, OPACITY);
1659 } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
1661 } else if (Y.Dom.IE_COMPUTED[property]) { // use compute function
1662 value = Y.Dom.IE_COMPUTED[property](el, property);
1663 } else if (re_unit.test(current)) { // convert to pixel
1664 value = Y.Dom.IE.ComputedStyle.getPixel(el, property);
1672 getOffset: function(el, prop) {
1673 var current = el[CURRENT_STYLE][prop], // value of "width", "top", etc.
1674 capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
1675 offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
1676 pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
1680 if (current == AUTO) {
1681 actual = el[offset]; // offsetHeight/Top etc.
1682 if (actual === undefined) { // likely "right" or "bottom"
1687 if (re_size.test(prop)) { // account for box model diff
1688 el[STYLE][prop] = actual;
1689 if (el[offset] > actual) {
1690 // the difference is padding + border (works in Standards & Quirks modes)
1691 value = actual - (el[offset] - actual);
1693 el[STYLE][prop] = AUTO; // revert to auto
1695 } else { // convert units to px
1696 if (!el[STYLE][pixel] && !el[STYLE][prop]) { // need to map style.width to currentStyle (no currentStyle.pixelWidth)
1697 el[STYLE][prop] = current; // no style.pixelWidth if no style.width
1699 value = el[STYLE][pixel];
1704 getBorderWidth: function(el, property) {
1705 // clientHeight/Width = paddingBox (e.g. offsetWidth - borderWidth)
1706 // clientTop/Left = borderWidth
1708 if (!el[CURRENT_STYLE][HAS_LAYOUT]) { // TODO: unset layout?
1709 el[STYLE].zoom = 1; // need layout to measure client
1713 case BORDER_TOP_WIDTH:
1714 value = el[CLIENT_TOP];
1716 case BORDER_BOTTOM_WIDTH:
1717 value = el.offsetHeight - el.clientHeight - el[CLIENT_TOP];
1719 case BORDER_LEFT_WIDTH:
1720 value = el[CLIENT_LEFT];
1722 case BORDER_RIGHT_WIDTH:
1723 value = el.offsetWidth - el.clientWidth - el[CLIENT_LEFT];
1729 getPixel: function(node, att) {
1730 // use pixelRight to convert to px
1732 styleRight = node[CURRENT_STYLE][RIGHT],
1733 current = node[CURRENT_STYLE][att];
1735 node[STYLE][RIGHT] = current;
1736 val = node[STYLE].pixelRight;
1737 node[STYLE][RIGHT] = styleRight; // revert
1742 getMargin: function(node, att) {
1744 if (node[CURRENT_STYLE][att] == AUTO) {
1747 val = Y.Dom.IE.ComputedStyle.getPixel(node, att);
1752 getVisibility: function(node, att) {
1754 while ( (current = node[CURRENT_STYLE]) && current[att] == 'inherit') { // NOTE: assignment in test
1755 node = node[PARENT_NODE];
1757 return (current) ? current[att] : VISIBLE;
1760 getColor: function(node, att) {
1761 return Y.Dom.Color.toRGB(node[CURRENT_STYLE][att]) || TRANSPARENT;
1764 getBorderColor: function(node, att) {
1765 var current = node[CURRENT_STYLE],
1766 val = current[att] || current.color;
1767 return Y.Dom.Color.toRGB(Y.Dom.Color.toHex(val));
1772 //fontSize: getPixelFont,
1775 IEComputed.top = IEComputed.right = IEComputed.bottom = IEComputed.left =
1776 IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
1778 IEComputed.color = ComputedStyle.getColor;
1780 IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
1781 IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
1782 ComputedStyle.getBorderWidth;
1784 IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
1785 IEComputed.marginLeft = ComputedStyle.getMargin;
1787 IEComputed.visibility = ComputedStyle.getVisibility;
1788 IEComputed.borderColor = IEComputed.borderTopColor =
1789 IEComputed.borderRightColor = IEComputed.borderBottomColor =
1790 IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
1792 Y.Dom.IE_COMPUTED = IEComputed;
1793 Y.Dom.IE_ComputedStyle = ComputedStyle;
1797 * Add style management functionality to DOM.
1802 var TO_STRING = 'toString',
1803 PARSE_INT = parseInt,
1827 re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
1828 re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
1829 re_hex3: /([0-9A-F])/gi,
1831 toRGB: function(val) {
1832 if (!Y.Dom.Color.re_RGB.test(val)) {
1833 val = Y.Dom.Color.toHex(val);
1836 if(Y.Dom.Color.re_hex.exec(val)) {
1838 PARSE_INT(RE.$1, 16),
1839 PARSE_INT(RE.$2, 16),
1840 PARSE_INT(RE.$3, 16)
1846 toHex: function(val) {
1847 val = Y.Dom.Color.KEYWORDS[val] || val;
1848 if (Y.Dom.Color.re_RGB.exec(val)) {
1849 var r = (RE.$1.length === 1) ? '0' + RE.$1 : Number(RE.$1),
1850 g = (RE.$2.length === 1) ? '0' + RE.$2 : Number(RE.$2),
1851 b = (RE.$3.length === 1) ? '0' + RE.$3 : Number(RE.$3);
1860 if (val.length < 6) {
1861 val = val.replace(Y.Dom.Color.re_hex3, '$1$1');
1864 if (val !== 'transparent' && val.indexOf('#') < 0) {
1868 return val.toLowerCase();
1872 YAHOO.register("dom", YAHOO.util.Dom, {version: "2.8.0r4", build: "2449"});