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 YAHOO.env._id_counter = YAHOO.env._id_counter || 0; // for use with generateId (global to save state if Dom is overwritten)
20 trim = YAHOO.lang.trim,
21 propertyCache = {}, // for faster hyphen converts
22 reCache = {}, // cache className regexes
23 RE_TABLE = /^t(?:able|d|h)$/i, // for _calcBorders
27 document = window.document,
28 documentElement = document.documentElement,
31 OWNER_DOCUMENT = 'ownerDocument',
32 DEFAULT_VIEW = 'defaultView',
33 DOCUMENT_ELEMENT = 'documentElement',
34 COMPAT_MODE = 'compatMode',
35 OFFSET_LEFT = 'offsetLeft',
36 OFFSET_TOP = 'offsetTop',
37 OFFSET_PARENT = 'offsetParent',
38 PARENT_NODE = 'parentNode',
39 NODE_TYPE = 'nodeType',
41 SCROLL_LEFT = 'scrollLeft',
42 SCROLL_TOP = 'scrollTop',
43 GET_BOUNDING_CLIENT_RECT = 'getBoundingClientRect',
44 GET_COMPUTED_STYLE = 'getComputedStyle',
45 CURRENT_STYLE = 'currentStyle',
46 CSS1_COMPAT = 'CSS1Compat',
47 _BACK_COMPAT = 'BackCompat',
48 _CLASS = 'class', // underscore due to reserved word
49 CLASS_NAME = 'className',
52 C_START = '(?:^|\\s)',
55 POSITION = 'position',
57 RELATIVE = 'relative',
61 BORDER_LEFT_WIDTH = 'borderLeftWidth',
62 BORDER_TOP_WIDTH = 'borderTopWidth',
71 * Provides helper methods for DOM elements.
72 * @namespace YAHOO.util
74 * @requires yahoo, event
77 CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
86 * Returns an HTMLElement reference.
88 * @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.
89 * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
92 var id, nodes, c, i, len;
95 if (el[NODE_TYPE] || el.item) { // Node, or NodeList
99 if (typeof el === 'string') { // id
101 el = document.getElementById(el);
102 if (el && el.id === id) { // IE: avoid false match on "name" attribute
104 } else if (el && document.all) { // filter by name
106 nodes = document.all[id];
107 for (i = 0, len = nodes.length; i < len; ++i) {
108 if (nodes[i].id === id) {
116 if (el.DOM_EVENTS) { // YAHOO.util.Element
117 el = el.get('element');
120 if ('length' in el) { // array-like
122 for (i = 0, len = el.length; i < len; ++i) {
123 c[c.length] = Y.Dom.get(el[i]);
129 return el; // some other object, just pass it back
135 getComputedStyle: function(el, property) {
136 if (window[GET_COMPUTED_STYLE]) {
137 return el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null)[property];
138 } else if (el[CURRENT_STYLE]) {
139 return Y.Dom.IE_ComputedStyle.get(el, property);
144 * Normalizes currentStyle and ComputedStyle.
146 * @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.
147 * @param {String} property The style property whose value is returned.
148 * @return {String | Array} The current value of the style property for the element(s).
150 getStyle: function(el, property) {
151 return Y.Dom.batch(el, Y.Dom._getStyle, property);
154 // branching at load instead of runtime
155 _getStyle: function() {
156 if (window[GET_COMPUTED_STYLE]) { // W3C DOM method
157 return function(el, property) {
158 property = (property === 'float') ? property = 'cssFloat' :
159 Y.Dom._toCamel(property);
161 var value = el.style[property],
165 computed = el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null);
166 if (computed) { // test computed before touching for safari
167 value = computed[property];
173 } else if (documentElement[CURRENT_STYLE]) {
174 return function(el, property) {
178 case 'opacity' :// IE opacity uses filter
180 try { // will error if no DXImageTransform
181 value = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
184 try { // make sure its in the document
185 value = el.filters('alpha').opacity;
190 case 'float': // fix reserved word
191 property = 'styleFloat'; // fall through
193 property = Y.Dom._toCamel(property);
194 value = el[CURRENT_STYLE] ? el[CURRENT_STYLE][property] : null;
195 return ( el.style[property] || value );
202 * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
204 * @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.
205 * @param {String} property The style property to be set.
206 * @param {String} val The value to apply to the given property.
208 setStyle: function(el, property, val) {
209 Y.Dom.batch(el, Y.Dom._setStyle, { prop: property, val: val });
212 _setStyle: function() {
214 return function(el, args) {
215 var property = Y.Dom._toCamel(args.prop),
221 if ( lang.isString(el.style.filter) ) { // in case not appended
222 el.style.filter = 'alpha(opacity=' + val * 100 + ')';
224 if (!el[CURRENT_STYLE] || !el[CURRENT_STYLE].hasLayout) {
225 el.style.zoom = 1; // when no layout or cant tell
230 property = 'styleFloat';
232 el.style[property] = val;
238 return function(el, args) {
239 var property = Y.Dom._toCamel(args.prop),
242 if (property == 'float') {
243 property = 'cssFloat';
245 el.style[property] = val;
254 * Gets the current position of an element based on page coordinates.
255 * Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
257 * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM
258 * reference, or an Array of IDs and/or HTMLElements
259 * @return {Array} The XY position of the element(s)
261 getXY: function(el) {
262 return Y.Dom.batch(el, Y.Dom._getXY);
265 _canPosition: function(el) {
266 return ( Y.Dom._getStyle(el, 'display') !== 'none' && Y.Dom._inDoc(el) );
270 if (document[DOCUMENT_ELEMENT][GET_BOUNDING_CLIENT_RECT]) {
271 return function(node) {
272 var scrollLeft, scrollTop, box, doc,
273 off1, off2, mode, bLeft, bTop,
274 floor = Math.floor, // TODO: round?
277 if (Y.Dom._canPosition(node)) {
278 box = node[GET_BOUNDING_CLIENT_RECT]();
279 doc = node[OWNER_DOCUMENT];
280 scrollLeft = Y.Dom.getDocumentScrollLeft(doc);
281 scrollTop = Y.Dom.getDocumentScrollTop(doc);
282 xy = [floor(box[LEFT]), floor(box[TOP])];
284 if (isIE && UA.ie < 8) { // IE < 8: viewport off by 2
287 mode = doc[COMPAT_MODE];
288 bLeft = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_LEFT_WIDTH);
289 bTop = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_TOP_WIDTH);
292 if (mode !== _BACK_COMPAT) {
298 if ((mode == _BACK_COMPAT)) {
299 if (bLeft !== MEDIUM) {
300 off1 = parseInt(bLeft, 10);
302 if (bTop !== MEDIUM) {
303 off2 = parseInt(bTop, 10);
312 if ((scrollTop || scrollLeft)) {
317 // gecko may return sub-pixel (non-int) values
318 xy[0] = floor(xy[0]);
319 xy[1] = floor(xy[1]);
326 return function(node) { // ff2, safari: manually calculate by crawling up offsetParents
327 var docScrollLeft, docScrollTop,
328 scrollTop, scrollLeft,
333 if (Y.Dom._canPosition(node) ) {
334 xy = [node[OFFSET_LEFT], node[OFFSET_TOP]];
335 docScrollLeft = Y.Dom.getDocumentScrollLeft(node[OWNER_DOCUMENT]);
336 docScrollTop = Y.Dom.getDocumentScrollTop(node[OWNER_DOCUMENT]);
338 // TODO: refactor with !! or just falsey
339 bCheck = ((isGecko || UA.webkit > 519) ? true : false);
341 // TODO: worth refactoring for TOP/LEFT only?
342 while ((parentNode = parentNode[OFFSET_PARENT])) {
343 xy[0] += parentNode[OFFSET_LEFT];
344 xy[1] += parentNode[OFFSET_TOP];
346 xy = Y.Dom._calcBorders(parentNode, xy);
350 // account for any scrolled ancestors
351 if (Y.Dom._getStyle(node, POSITION) !== FIXED) {
354 while ((parentNode = parentNode[PARENT_NODE]) && parentNode[TAG_NAME]) {
355 scrollTop = parentNode[SCROLL_TOP];
356 scrollLeft = parentNode[SCROLL_LEFT];
358 //Firefox does something funky with borders when overflow is not visible.
359 if (isGecko && (Y.Dom._getStyle(parentNode, 'overflow') !== 'visible')) {
360 xy = Y.Dom._calcBorders(parentNode, xy);
363 if (scrollTop || scrollLeft) {
368 xy[0] += docScrollLeft;
369 xy[1] += docScrollTop;
372 //Fix FIXED position -- add scrollbars
374 xy[0] -= docScrollLeft;
375 xy[1] -= docScrollTop;
376 } else if (isSafari || isGecko) {
377 xy[0] += docScrollLeft;
378 xy[1] += docScrollTop;
381 //Round the numbers so we get sane data back
382 xy[0] = Math.floor(xy[0]);
383 xy[1] = Math.floor(xy[1]);
389 }(), // NOTE: Executing for loadtime branching
392 * 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).
394 * @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
395 * @return {Number | Array} The X position of the element(s)
398 var f = function(el) {
399 return Y.Dom.getXY(el)[0];
402 return Y.Dom.batch(el, f, Y.Dom, true);
406 * 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).
408 * @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
409 * @return {Number | Array} The Y position of the element(s)
412 var f = function(el) {
413 return Y.Dom.getXY(el)[1];
416 return Y.Dom.batch(el, f, Y.Dom, true);
420 * Set the position of an html element in page coordinates, regardless of how the element is positioned.
421 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
423 * @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
424 * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
425 * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
427 setXY: function(el, pos, noRetry) {
428 Y.Dom.batch(el, Y.Dom._setXY, { pos: pos, noRetry: noRetry });
431 _setXY: function(node, args) {
432 var pos = Y.Dom._getStyle(node, POSITION),
433 setStyle = Y.Dom.setStyle,
435 noRetry = args.noRetry,
437 delta = [ // assuming pixels; if not we will have to retry
438 parseInt( Y.Dom.getComputedStyle(node, LEFT), 10 ),
439 parseInt( Y.Dom.getComputedStyle(node, TOP), 10 )
445 if (pos == 'static') { // default to relative
447 setStyle(node, POSITION, pos);
450 currentXY = Y.Dom._getXY(node);
452 if (!xy || currentXY === false) { // has to be part of doc to have xy
456 if ( isNaN(delta[0]) ) {// in case of 'auto'
457 delta[0] = (pos == RELATIVE) ? 0 : node[OFFSET_LEFT];
459 if ( isNaN(delta[1]) ) { // in case of 'auto'
460 delta[1] = (pos == RELATIVE) ? 0 : node[OFFSET_TOP];
463 if (xy[0] !== null) { // from setX
464 setStyle(node, LEFT, xy[0] - currentXY[0] + delta[0] + 'px');
467 if (xy[1] !== null) { // from setY
468 setStyle(node, TOP, xy[1] - currentXY[1] + delta[1] + 'px');
472 newXY = Y.Dom._getXY(node);
474 // if retry is true, try one more time if we miss
475 if ( (xy[0] !== null && newXY[0] != xy[0]) ||
476 (xy[1] !== null && newXY[1] != xy[1]) ) {
477 Y.Dom._setXY(node, { pos: xy, noRetry: true });
484 * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
485 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
487 * @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.
488 * @param {Int} x The value to use as the X coordinate for the element(s).
490 setX: function(el, x) {
491 Y.Dom.setXY(el, [x, null]);
495 * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
496 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
498 * @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.
499 * @param {Int} x To use as the Y coordinate for the element(s).
501 setY: function(el, y) {
502 Y.Dom.setXY(el, [null, y]);
506 * Returns the region position of the given element.
507 * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
509 * @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.
510 * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
512 getRegion: function(el) {
513 var f = function(el) {
515 if ( Y.Dom._canPosition(el) ) {
516 region = Y.Region.getRegion(el);
523 return Y.Dom.batch(el, f, Y.Dom, true);
527 * Returns the width of the client (viewport).
528 * @method getClientWidth
529 * @deprecated Now using getViewportWidth. This interface left intact for back compat.
530 * @return {Int} The width of the viewable area of the page.
532 getClientWidth: function() {
533 return Y.Dom.getViewportWidth();
537 * Returns the height of the client (viewport).
538 * @method getClientHeight
539 * @deprecated Now using getViewportHeight. This interface left intact for back compat.
540 * @return {Int} The height of the viewable area of the page.
542 getClientHeight: function() {
543 return Y.Dom.getViewportHeight();
547 * Returns a array of HTMLElements with the given class.
548 * For optimized performance, include a tag and/or root node when possible.
549 * Note: This method operates against a live collection, so modifying the
550 * collection in the callback (removing/appending nodes, etc.) will have
551 * side effects. Instead you should iterate the returned nodes array,
552 * as you would with the native "getElementsByTagName" method.
553 * @method getElementsByClassName
554 * @param {String} className The class name to match against
555 * @param {String} tag (optional) The tag name of the elements being collected
556 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
557 * @param {Function} apply (optional) A function to apply to each element when found
558 * @param {Any} o (optional) An optional arg that is passed to the supplied method
559 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
560 * @return {Array} An array of elements that have the given class name
562 getElementsByClassName: function(className, tag, root, apply, o, overrides) {
563 className = lang.trim(className);
565 root = (root) ? Y.Dom.get(root) : null || document;
571 elements = root.getElementsByTagName(tag),
572 hasClass = Y.Dom.hasClass;
574 for (var i = 0, len = elements.length; i < len; ++i) {
575 if ( hasClass(elements[i], className) ) {
576 nodes[nodes.length] = elements[i];
581 Y.Dom.batch(nodes, apply, o, overrides);
588 * Determines whether an HTMLElement has the given className.
590 * @param {String | HTMLElement | Array} el The element or collection to test
591 * @param {String} className the class name to search for
592 * @return {Boolean | Array} A boolean value or array of boolean values
594 hasClass: function(el, className) {
595 return Y.Dom.batch(el, Y.Dom._hasClass, className);
598 _hasClass: function(el, className) {
602 if (el && className) {
603 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
604 if (className.exec) {
605 ret = className.test(current);
607 ret = className && (SPACE + current + SPACE).
608 indexOf(SPACE + className + SPACE) > -1;
617 * Adds a class name to a given element or collection of elements.
619 * @param {String | HTMLElement | Array} el The element or collection to add the class to
620 * @param {String} className the class name to add to the class attribute
621 * @return {Boolean | Array} A pass/fail boolean or array of booleans
623 addClass: function(el, className) {
624 return Y.Dom.batch(el, Y.Dom._addClass, className);
627 _addClass: function(el, className) {
631 if (el && className) {
632 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
633 if ( !Y.Dom._hasClass(el, className) ) {
634 Y.Dom.setAttribute(el, CLASS_NAME, trim(current + SPACE + className));
644 * Removes a class name from a given element or collection of elements.
645 * @method removeClass
646 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
647 * @param {String} className the class name to remove from the class attribute
648 * @return {Boolean | Array} A pass/fail boolean or array of booleans
650 removeClass: function(el, className) {
651 return Y.Dom.batch(el, Y.Dom._removeClass, className);
654 _removeClass: function(el, className) {
660 if (el && className) {
661 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
662 Y.Dom.setAttribute(el, CLASS_NAME, current.replace(Y.Dom._getClassRegex(className), EMPTY));
664 newClass = Y.Dom.getAttribute(el, CLASS_NAME);
665 if (current !== newClass) { // else nothing changed
666 Y.Dom.setAttribute(el, CLASS_NAME, trim(newClass)); // trim after comparing to current class
669 if (Y.Dom.getAttribute(el, CLASS_NAME) === '') { // remove class attribute if empty
670 attr = (el.hasAttribute && el.hasAttribute(_CLASS)) ? _CLASS : CLASS_NAME;
671 el.removeAttribute(attr);
682 * Replace a class with another class for a given element or collection of elements.
683 * If no oldClassName is present, the newClassName is simply added.
684 * @method replaceClass
685 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
686 * @param {String} oldClassName the class name to be replaced
687 * @param {String} newClassName the class name that will be replacing the old class name
688 * @return {Boolean | Array} A pass/fail boolean or array of booleans
690 replaceClass: function(el, oldClassName, newClassName) {
691 return Y.Dom.batch(el, Y.Dom._replaceClass, { from: oldClassName, to: newClassName });
694 _replaceClass: function(el, classObj) {
701 if (el && classObj) {
702 from = classObj.from;
707 } else if (!from) { // just add if no "from"
708 ret = Y.Dom._addClass(el, classObj.to);
709 } else if (from !== to) { // else nothing to replace
710 // May need to lead with DBLSPACE?
711 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
712 className = (SPACE + current.replace(Y.Dom._getClassRegex(from), SPACE + to)).
713 split(Y.Dom._getClassRegex(to));
715 // insert to into what would have been the first occurrence slot
716 className.splice(1, 0, SPACE + to);
717 Y.Dom.setAttribute(el, CLASS_NAME, trim(className.join(EMPTY)));
727 * Returns an ID and applies it to the element "el", if provided.
729 * @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).
730 * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
731 * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
733 generateId: function(el, prefix) {
734 prefix = prefix || 'yui-gen';
736 var f = function(el) {
737 if (el && el.id) { // do not override existing ID
741 var id = prefix + YAHOO.env._id_counter++;
744 if (el[OWNER_DOCUMENT].getElementById(id)) { // in case one already exists
745 // use failed id plus prefix to help ensure uniqueness
746 return Y.Dom.generateId(el, id + prefix);
754 // batch fails when no element, so just generate and return single ID
755 return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
759 * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
761 * @param {String | HTMLElement} haystack The possible ancestor
762 * @param {String | HTMLElement} needle The possible descendent
763 * @return {Boolean} Whether or not the haystack is an ancestor of needle
765 isAncestor: function(haystack, needle) {
766 haystack = Y.Dom.get(haystack);
767 needle = Y.Dom.get(needle);
771 if ( (haystack && needle) && (haystack[NODE_TYPE] && needle[NODE_TYPE]) ) {
772 if (haystack.contains && haystack !== needle) { // contains returns true when equal
773 ret = haystack.contains(needle);
775 else if (haystack.compareDocumentPosition) { // gecko
776 ret = !!(haystack.compareDocumentPosition(needle) & 16);
784 * Determines whether an HTMLElement is present in the current document.
786 * @param {String | HTMLElement} el The element to search for
787 * @param {Object} doc An optional document to search, defaults to element's owner document
788 * @return {Boolean} Whether or not the element is present in the current document
790 inDocument: function(el, doc) {
791 return Y.Dom._inDoc(Y.Dom.get(el), doc);
794 _inDoc: function(el, doc) {
796 if (el && el[TAG_NAME]) {
797 doc = doc || el[OWNER_DOCUMENT];
798 ret = Y.Dom.isAncestor(doc[DOCUMENT_ELEMENT], el);
805 * Returns a array of HTMLElements that pass the test applied by supplied boolean method.
806 * For optimized performance, include a tag and/or root node when possible.
807 * Note: This method operates against a live collection, so modifying the
808 * collection in the callback (removing/appending nodes, etc.) will have
809 * side effects. Instead you should iterate the returned nodes array,
810 * as you would with the native "getElementsByTagName" method.
811 * @method getElementsBy
812 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
813 * @param {String} tag (optional) The tag name of the elements being collected
814 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
815 * @param {Function} apply (optional) A function to apply to each element when found
816 * @param {Any} o (optional) An optional arg that is passed to the supplied method
817 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
818 * @return {Array} Array of HTMLElements
820 getElementsBy: function(method, tag, root, apply, o, overrides, firstOnly) {
822 root = (root) ? Y.Dom.get(root) : null || document;
829 elements = root.getElementsByTagName(tag);
831 for (var i = 0, len = elements.length; i < len; ++i) {
832 if ( method(elements[i]) ) {
837 nodes[nodes.length] = elements[i];
843 Y.Dom.batch(nodes, apply, o, overrides);
851 * Returns the first HTMLElement that passes the test applied by the supplied boolean method.
852 * @method getElementBy
853 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
854 * @param {String} tag (optional) The tag name of the elements being collected
855 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
856 * @return {HTMLElement}
858 getElementBy: function(method, tag, root) {
859 return Y.Dom.getElementsBy(method, tag, root, null, null, null, true);
863 * Runs the supplied method against each item in the Collection/Array.
864 * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
866 * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
867 * @param {Function} method The method to apply to the element(s)
868 * @param {Any} o (optional) An optional arg that is passed to the supplied method
869 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
870 * @return {Any | Array} The return value(s) from the supplied method
872 batch: function(el, method, o, overrides) {
874 scope = (overrides) ? o : window;
876 el = (el && (el[TAG_NAME] || el.item)) ? el : Y.Dom.get(el); // skip get() when possible
878 if (el[TAG_NAME] || el.length === undefined) { // element or not array-like
879 return method.call(scope, el, o);
882 for (var i = 0; i < el.length; ++i) {
883 collection[collection.length] = method.call(scope, el[i], o);
892 * Returns the height of the document.
893 * @method getDocumentHeight
894 * @return {Int} The height of the actual document (which includes the body and its margin).
896 getDocumentHeight: function() {
897 var scrollHeight = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollHeight : documentElement.scrollHeight,
898 h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
904 * Returns the width of the document.
905 * @method getDocumentWidth
906 * @return {Int} The width of the actual document (which includes the body and its margin).
908 getDocumentWidth: function() {
909 var scrollWidth = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollWidth : documentElement.scrollWidth,
910 w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
915 * Returns the current height of the viewport.
916 * @method getViewportHeight
917 * @return {Int} The height of the viewable area of the page (excludes scrollbars).
919 getViewportHeight: function() {
920 var height = self.innerHeight, // Safari, Opera
921 mode = document[COMPAT_MODE];
923 if ( (mode || isIE) && !isOpera ) { // IE, Gecko
924 height = (mode == CSS1_COMPAT) ?
925 documentElement.clientHeight : // Standards
926 document.body.clientHeight; // Quirks
933 * Returns the current width of the viewport.
934 * @method getViewportWidth
935 * @return {Int} The width of the viewable area of the page (excludes scrollbars).
938 getViewportWidth: function() {
939 var width = self.innerWidth, // Safari
940 mode = document[COMPAT_MODE];
942 if (mode || isIE) { // IE, Gecko, Opera
943 width = (mode == CSS1_COMPAT) ?
944 documentElement.clientWidth : // Standards
945 document.body.clientWidth; // Quirks
951 * Returns the nearest ancestor that passes the test applied by supplied boolean method.
952 * For performance reasons, IDs are not accepted and argument validation omitted.
953 * @method getAncestorBy
954 * @param {HTMLElement} node The HTMLElement to use as the starting point
955 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
956 * @return {Object} HTMLElement or null if not found
958 getAncestorBy: function(node, method) {
959 while ( (node = node[PARENT_NODE]) ) { // NOTE: assignment
960 if ( Y.Dom._testElement(node, method) ) {
969 * Returns the nearest ancestor with the given className.
970 * @method getAncestorByClassName
971 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
972 * @param {String} className
973 * @return {Object} HTMLElement
975 getAncestorByClassName: function(node, className) {
976 node = Y.Dom.get(node);
980 var method = function(el) { return Y.Dom.hasClass(el, className); };
981 return Y.Dom.getAncestorBy(node, method);
985 * Returns the nearest ancestor with the given tagName.
986 * @method getAncestorByTagName
987 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
988 * @param {String} tagName
989 * @return {Object} HTMLElement
991 getAncestorByTagName: function(node, tagName) {
992 node = Y.Dom.get(node);
996 var method = function(el) {
997 return el[TAG_NAME] && el[TAG_NAME].toUpperCase() == tagName.toUpperCase();
1000 return Y.Dom.getAncestorBy(node, method);
1004 * Returns the previous sibling that is an HTMLElement.
1005 * For performance reasons, IDs are not accepted and argument validation omitted.
1006 * Returns the nearest HTMLElement sibling if no method provided.
1007 * @method getPreviousSiblingBy
1008 * @param {HTMLElement} node The HTMLElement to use as the starting point
1009 * @param {Function} method A boolean function used to test siblings
1010 * that receives the sibling node being tested as its only argument
1011 * @return {Object} HTMLElement or null if not found
1013 getPreviousSiblingBy: function(node, method) {
1015 node = node.previousSibling;
1016 if ( Y.Dom._testElement(node, method) ) {
1024 * Returns the previous sibling that is an HTMLElement
1025 * @method getPreviousSibling
1026 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1027 * @return {Object} HTMLElement or null if not found
1029 getPreviousSibling: function(node) {
1030 node = Y.Dom.get(node);
1035 return Y.Dom.getPreviousSiblingBy(node);
1039 * Returns the next HTMLElement sibling that passes the boolean method.
1040 * For performance reasons, IDs are not accepted and argument validation omitted.
1041 * Returns the nearest HTMLElement sibling if no method provided.
1042 * @method getNextSiblingBy
1043 * @param {HTMLElement} node The HTMLElement to use as the starting point
1044 * @param {Function} method A boolean function used to test siblings
1045 * that receives the sibling node being tested as its only argument
1046 * @return {Object} HTMLElement or null if not found
1048 getNextSiblingBy: function(node, method) {
1050 node = node.nextSibling;
1051 if ( Y.Dom._testElement(node, method) ) {
1059 * Returns the next sibling that is an HTMLElement
1060 * @method getNextSibling
1061 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1062 * @return {Object} HTMLElement or null if not found
1064 getNextSibling: function(node) {
1065 node = Y.Dom.get(node);
1070 return Y.Dom.getNextSiblingBy(node);
1074 * Returns the first HTMLElement child that passes the test method.
1075 * @method getFirstChildBy
1076 * @param {HTMLElement} node The HTMLElement to use as the starting point
1077 * @param {Function} method A boolean function used to test children
1078 * that receives the node being tested as its only argument
1079 * @return {Object} HTMLElement or null if not found
1081 getFirstChildBy: function(node, method) {
1082 var child = ( Y.Dom._testElement(node.firstChild, method) ) ? node.firstChild : null;
1083 return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
1087 * Returns the first HTMLElement child.
1088 * @method getFirstChild
1089 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1090 * @return {Object} HTMLElement or null if not found
1092 getFirstChild: function(node, method) {
1093 node = Y.Dom.get(node);
1097 return Y.Dom.getFirstChildBy(node);
1101 * Returns the last HTMLElement child that passes the test method.
1102 * @method getLastChildBy
1103 * @param {HTMLElement} node The HTMLElement to use as the starting point
1104 * @param {Function} method A boolean function used to test children
1105 * that receives the node being tested as its only argument
1106 * @return {Object} HTMLElement or null if not found
1108 getLastChildBy: function(node, method) {
1112 var child = ( Y.Dom._testElement(node.lastChild, method) ) ? node.lastChild : null;
1113 return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
1117 * Returns the last HTMLElement child.
1118 * @method getLastChild
1119 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1120 * @return {Object} HTMLElement or null if not found
1122 getLastChild: function(node) {
1123 node = Y.Dom.get(node);
1124 return Y.Dom.getLastChildBy(node);
1128 * Returns an array of HTMLElement childNodes that pass the test method.
1129 * @method getChildrenBy
1130 * @param {HTMLElement} node The HTMLElement to start from
1131 * @param {Function} method A boolean function used to test children
1132 * that receives the node being tested as its only argument
1133 * @return {Array} A static array of HTMLElements
1135 getChildrenBy: function(node, method) {
1136 var child = Y.Dom.getFirstChildBy(node, method),
1137 children = child ? [child] : [];
1139 Y.Dom.getNextSiblingBy(child, function(node) {
1140 if ( !method || method(node) ) {
1141 children[children.length] = node;
1143 return false; // fail test to collect all children
1150 * Returns an array of HTMLElement childNodes.
1151 * @method getChildren
1152 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1153 * @return {Array} A static array of HTMLElements
1155 getChildren: function(node) {
1156 node = Y.Dom.get(node);
1160 return Y.Dom.getChildrenBy(node);
1164 * Returns the left scroll value of the document
1165 * @method getDocumentScrollLeft
1166 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1167 * @return {Int} The amount that the document is scrolled to the left
1169 getDocumentScrollLeft: function(doc) {
1170 doc = doc || document;
1171 return Math.max(doc[DOCUMENT_ELEMENT].scrollLeft, doc.body.scrollLeft);
1175 * Returns the top scroll value of the document
1176 * @method getDocumentScrollTop
1177 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1178 * @return {Int} The amount that the document is scrolled to the top
1180 getDocumentScrollTop: function(doc) {
1181 doc = doc || document;
1182 return Math.max(doc[DOCUMENT_ELEMENT].scrollTop, doc.body.scrollTop);
1186 * Inserts the new node as the previous sibling of the reference node
1187 * @method insertBefore
1188 * @param {String | HTMLElement} newNode The node to be inserted
1189 * @param {String | HTMLElement} referenceNode The node to insert the new node before
1190 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1192 insertBefore: function(newNode, referenceNode) {
1193 newNode = Y.Dom.get(newNode);
1194 referenceNode = Y.Dom.get(referenceNode);
1196 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1200 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode);
1204 * Inserts the new node as the next sibling of the reference node
1205 * @method insertAfter
1206 * @param {String | HTMLElement} newNode The node to be inserted
1207 * @param {String | HTMLElement} referenceNode The node to insert the new node after
1208 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1210 insertAfter: function(newNode, referenceNode) {
1211 newNode = Y.Dom.get(newNode);
1212 referenceNode = Y.Dom.get(referenceNode);
1214 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1218 if (referenceNode.nextSibling) {
1219 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode.nextSibling);
1221 return referenceNode[PARENT_NODE].appendChild(newNode);
1226 * Creates a Region based on the viewport relative to the document.
1227 * @method getClientRegion
1228 * @return {Region} A Region object representing the viewport which accounts for document scroll
1230 getClientRegion: function() {
1231 var t = Y.Dom.getDocumentScrollTop(),
1232 l = Y.Dom.getDocumentScrollLeft(),
1233 r = Y.Dom.getViewportWidth() + l,
1234 b = Y.Dom.getViewportHeight() + t;
1236 return new Y.Region(t, r, b, l);
1240 * Provides a normalized attribute interface.
1241 * @method setAttibute
1242 * @param {String | HTMLElement} el The target element for the attribute.
1243 * @param {String} attr The attribute to set.
1244 * @param {String} val The value of the attribute.
1246 setAttribute: function(el, attr, val) {
1247 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1248 el.setAttribute(attr, val);
1253 * Provides a normalized attribute interface.
1254 * @method getAttibute
1255 * @param {String | HTMLElement} el The target element for the attribute.
1256 * @param {String} attr The attribute to get.
1257 * @return {String} The current value of the attribute.
1259 getAttribute: function(el, attr) {
1260 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1261 return el.getAttribute(attr);
1264 _toCamel: function(property) {
1265 var c = propertyCache;
1268 return l.toUpperCase();
1271 return c[property] || (c[property] = property.indexOf('-') === -1 ?
1273 property.replace( /-([a-z])/gi, tU ));
1276 _getClassRegex: function(className) {
1278 if (className !== undefined) { // allow empty string to pass
1279 if (className.exec) { // already a RegExp
1282 re = reCache[className];
1284 // escape special chars (".", "[", etc.)
1285 className = className.replace(Y.Dom._patterns.CLASS_RE_TOKENS, '\\$1');
1286 re = reCache[className] = new RegExp(C_START + className + C_END, G);
1294 ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
1295 CLASS_RE_TOKENS: /([\.\(\)\^\$\*\+\?\|\[\]\{\}])/g
1299 _testElement: function(node, method) {
1300 return node && node[NODE_TYPE] == 1 && ( !method || method(node) );
1303 _calcBorders: function(node, xy2) {
1304 var t = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_TOP_WIDTH), 10) || 0,
1305 l = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_LEFT_WIDTH), 10) || 0;
1307 if (RE_TABLE.test(node[TAG_NAME])) {
1318 var _getComputedStyle = Y.Dom[GET_COMPUTED_STYLE];
1319 // fix opera computedStyle default color unit (convert to rgb)
1321 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1322 var val = _getComputedStyle(node, att);
1323 if (RE_COLOR.test(att)) {
1324 val = Y.Dom.Color.toRGB(val);
1332 // safari converts transparent to rgba(), others use "transparent"
1334 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1335 var val = _getComputedStyle(node, att);
1337 if (val === 'rgba(0, 0, 0, 0)') {
1338 val = 'transparent';
1347 * A region is a representation of an object on a grid. It is defined
1348 * by the top, right, bottom, left extents, so is rectangular by default. If
1349 * other shapes are required, this class could be extended to support it.
1350 * @namespace YAHOO.util
1352 * @param {Int} t the top extent
1353 * @param {Int} r the right extent
1354 * @param {Int} b the bottom extent
1355 * @param {Int} l the left extent
1358 YAHOO.util.Region = function(t, r, b, l) {
1361 * The region's top extent
1368 * The region's top extent
1375 * The region's top extent as index, for symmetry with set/getXY
1382 * The region's right extent
1389 * The region's bottom extent
1396 * The region's left extent
1403 * The region's left extent
1410 * The region's left extent as index, for symmetry with set/getXY
1417 * The region's total width
1421 this.width = this.right - this.left;
1424 * The region's total height
1428 this.height = this.bottom - this.top;
1432 * Returns true if this region contains the region passed in
1434 * @param {Region} region The region to evaluate
1435 * @return {Boolean} True if the region is contained with this region,
1438 YAHOO.util.Region.prototype.contains = function(region) {
1439 return ( region.left >= this.left &&
1440 region.right <= this.right &&
1441 region.top >= this.top &&
1442 region.bottom <= this.bottom );
1447 * Returns the area of the region
1449 * @return {Int} the region's area
1451 YAHOO.util.Region.prototype.getArea = function() {
1452 return ( (this.bottom - this.top) * (this.right - this.left) );
1456 * Returns the region where the passed in region overlaps with this one
1458 * @param {Region} region The region that intersects
1459 * @return {Region} The overlap region, or null if there is no overlap
1461 YAHOO.util.Region.prototype.intersect = function(region) {
1462 var t = Math.max( this.top, region.top ),
1463 r = Math.min( this.right, region.right ),
1464 b = Math.min( this.bottom, region.bottom ),
1465 l = Math.max( this.left, region.left );
1467 if (b >= t && r >= l) {
1468 return new YAHOO.util.Region(t, r, b, l);
1475 * Returns the region representing the smallest region that can contain both
1476 * the passed in region and this region.
1478 * @param {Region} region The region that to create the union with
1479 * @return {Region} The union region
1481 YAHOO.util.Region.prototype.union = function(region) {
1482 var t = Math.min( this.top, region.top ),
1483 r = Math.max( this.right, region.right ),
1484 b = Math.max( this.bottom, region.bottom ),
1485 l = Math.min( this.left, region.left );
1487 return new YAHOO.util.Region(t, r, b, l);
1493 * @return string the region properties
1495 YAHOO.util.Region.prototype.toString = function() {
1496 return ( "Region {" +
1497 "top: " + this.top +
1498 ", right: " + this.right +
1499 ", bottom: " + this.bottom +
1500 ", left: " + this.left +
1501 ", height: " + this.height +
1502 ", width: " + this.width +
1507 * Returns a region that is occupied by the DOM element
1509 * @param {HTMLElement} el The element
1510 * @return {Region} The region that the element occupies
1513 YAHOO.util.Region.getRegion = function(el) {
1514 var p = YAHOO.util.Dom.getXY(el),
1516 r = p[0] + el.offsetWidth,
1517 b = p[1] + el.offsetHeight,
1520 return new YAHOO.util.Region(t, r, b, l);
1523 /////////////////////////////////////////////////////////////////////////////
1527 * A point is a region that is special in that it represents a single point on
1529 * @namespace YAHOO.util
1531 * @param {Int} x The X position of the point
1532 * @param {Int} y The Y position of the point
1534 * @extends YAHOO.util.Region
1536 YAHOO.util.Point = function(x, y) {
1537 if (YAHOO.lang.isArray(x)) { // accept input from Dom.getXY, Event.getXY, etc.
1538 y = x[1]; // dont blow away x yet
1542 YAHOO.util.Point.superclass.constructor.call(this, y, x, y, x);
1545 YAHOO.extend(YAHOO.util.Point, YAHOO.util.Region);
1549 * Add style management functionality to DOM.
1555 CLIENT_TOP = 'clientTop',
1556 CLIENT_LEFT = 'clientLeft',
1557 PARENT_NODE = 'parentNode',
1559 HAS_LAYOUT = 'hasLayout',
1561 OPACITY = 'opacity',
1563 BORDER_LEFT_WIDTH = 'borderLeftWidth',
1564 BORDER_TOP_WIDTH = 'borderTopWidth',
1565 BORDER_RIGHT_WIDTH = 'borderRightWidth',
1566 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
1567 VISIBLE = 'visible',
1568 TRANSPARENT = 'transparent',
1572 CURRENT_STYLE = 'currentStyle',
1574 // IE getComputedStyle
1575 // TODO: unit-less lineHeight (e.g. 1.22)
1576 re_size = /^width|height$/,
1577 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,
1580 get: function(el, property) {
1582 current = el[CURRENT_STYLE][property];
1584 if (property === OPACITY) {
1585 value = Y.Dom.getStyle(el, OPACITY);
1586 } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
1588 } else if (Y.Dom.IE_COMPUTED[property]) { // use compute function
1589 value = Y.Dom.IE_COMPUTED[property](el, property);
1590 } else if (re_unit.test(current)) { // convert to pixel
1591 value = Y.Dom.IE.ComputedStyle.getPixel(el, property);
1599 getOffset: function(el, prop) {
1600 var current = el[CURRENT_STYLE][prop], // value of "width", "top", etc.
1601 capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
1602 offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
1603 pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
1607 if (current == AUTO) {
1608 actual = el[offset]; // offsetHeight/Top etc.
1609 if (actual === undefined) { // likely "right" or "bottom"
1614 if (re_size.test(prop)) { // account for box model diff
1615 el[STYLE][prop] = actual;
1616 if (el[offset] > actual) {
1617 // the difference is padding + border (works in Standards & Quirks modes)
1618 value = actual - (el[offset] - actual);
1620 el[STYLE][prop] = AUTO; // revert to auto
1622 } else { // convert units to px
1623 if (!el[STYLE][pixel] && !el[STYLE][prop]) { // need to map style.width to currentStyle (no currentStyle.pixelWidth)
1624 el[STYLE][prop] = current; // no style.pixelWidth if no style.width
1626 value = el[STYLE][pixel];
1631 getBorderWidth: function(el, property) {
1632 // clientHeight/Width = paddingBox (e.g. offsetWidth - borderWidth)
1633 // clientTop/Left = borderWidth
1635 if (!el[CURRENT_STYLE][HAS_LAYOUT]) { // TODO: unset layout?
1636 el[STYLE].zoom = 1; // need layout to measure client
1640 case BORDER_TOP_WIDTH:
1641 value = el[CLIENT_TOP];
1643 case BORDER_BOTTOM_WIDTH:
1644 value = el.offsetHeight - el.clientHeight - el[CLIENT_TOP];
1646 case BORDER_LEFT_WIDTH:
1647 value = el[CLIENT_LEFT];
1649 case BORDER_RIGHT_WIDTH:
1650 value = el.offsetWidth - el.clientWidth - el[CLIENT_LEFT];
1656 getPixel: function(node, att) {
1657 // use pixelRight to convert to px
1659 styleRight = node[CURRENT_STYLE][RIGHT],
1660 current = node[CURRENT_STYLE][att];
1662 node[STYLE][RIGHT] = current;
1663 val = node[STYLE].pixelRight;
1664 node[STYLE][RIGHT] = styleRight; // revert
1669 getMargin: function(node, att) {
1671 if (node[CURRENT_STYLE][att] == AUTO) {
1674 val = Y.Dom.IE.ComputedStyle.getPixel(node, att);
1679 getVisibility: function(node, att) {
1681 while ( (current = node[CURRENT_STYLE]) && current[att] == 'inherit') { // NOTE: assignment in test
1682 node = node[PARENT_NODE];
1684 return (current) ? current[att] : VISIBLE;
1687 getColor: function(node, att) {
1688 return Y.Dom.Color.toRGB(node[CURRENT_STYLE][att]) || TRANSPARENT;
1691 getBorderColor: function(node, att) {
1692 var current = node[CURRENT_STYLE],
1693 val = current[att] || current.color;
1694 return Y.Dom.Color.toRGB(Y.Dom.Color.toHex(val));
1699 //fontSize: getPixelFont,
1702 IEComputed.top = IEComputed.right = IEComputed.bottom = IEComputed.left =
1703 IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
1705 IEComputed.color = ComputedStyle.getColor;
1707 IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
1708 IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
1709 ComputedStyle.getBorderWidth;
1711 IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
1712 IEComputed.marginLeft = ComputedStyle.getMargin;
1714 IEComputed.visibility = ComputedStyle.getVisibility;
1715 IEComputed.borderColor = IEComputed.borderTopColor =
1716 IEComputed.borderRightColor = IEComputed.borderBottomColor =
1717 IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
1719 Y.Dom.IE_COMPUTED = IEComputed;
1720 Y.Dom.IE_ComputedStyle = ComputedStyle;
1724 * Add style management functionality to DOM.
1729 var TO_STRING = 'toString',
1730 PARSE_INT = parseInt,
1754 re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
1755 re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
1756 re_hex3: /([0-9A-F])/gi,
1758 toRGB: function(val) {
1759 if (!Y.Dom.Color.re_RGB.test(val)) {
1760 val = Y.Dom.Color.toHex(val);
1763 if(Y.Dom.Color.re_hex.exec(val)) {
1765 PARSE_INT(RE.$1, 16),
1766 PARSE_INT(RE.$2, 16),
1767 PARSE_INT(RE.$3, 16)
1773 toHex: function(val) {
1774 val = Y.Dom.Color.KEYWORDS[val] || val;
1775 if (Y.Dom.Color.re_RGB.exec(val)) {
1776 var r = (RE.$1.length === 1) ? '0' + RE.$1 : Number(RE.$1),
1777 g = (RE.$2.length === 1) ? '0' + RE.$2 : Number(RE.$2),
1778 b = (RE.$3.length === 1) ? '0' + RE.$3 : Number(RE.$3);
1787 if (val.length < 6) {
1788 val = val.replace(Y.Dom.Color.re_hex3, '$1$1');
1791 if (val !== 'transparent' && val.indexOf('#') < 0) {
1795 return val.toLowerCase();
1799 YAHOO.register("dom", YAHOO.util.Dom, {version: "2.7.0", build: "1799"});