2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
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
91 * Returns an HTMLElement reference.
93 * @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.
94 * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
97 var id, nodes, c, i, len, attr, ret = null;
100 if (typeof el == 'string' || typeof el == 'number') { // id
102 el = document.getElementById(el);
103 attr = (el) ? el.attributes : null;
104 if (el && attr && attr.id && attr.id.value === id) { // IE: avoid false match on "name" attribute
106 } else if (el && document.all) { // filter by name
108 nodes = document.all[id];
109 if (nodes && nodes.length) {
110 for (i = 0, len = nodes.length; i < len; ++i) {
111 if (nodes[i].id === id) {
117 } else if (Y.Element && el instanceof Y.Element) {
118 el = el.get('element');
119 } else if (!el.nodeType && 'length' in el) { // array-like
121 for (i = 0, len = el.length; i < len; ++i) {
122 c[c.length] = Y.Dom.get(el[i]);
134 getComputedStyle: function(el, property) {
135 if (window[GET_COMPUTED_STYLE]) {
136 return el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null)[property];
137 } else if (el[CURRENT_STYLE]) {
138 return Y.Dom.IE_ComputedStyle.get(el, property);
143 * Normalizes currentStyle and ComputedStyle.
145 * @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.
146 * @param {String} property The style property whose value is returned.
147 * @return {String | Array} The current value of the style property for the element(s).
149 getStyle: function(el, property) {
150 return Y.Dom.batch(el, Y.Dom._getStyle, property);
153 // branching at load instead of runtime
154 _getStyle: function() {
155 if (window[GET_COMPUTED_STYLE]) { // W3C DOM method
156 return function(el, property) {
157 property = (property === 'float') ? property = 'cssFloat' :
158 Y.Dom._toCamel(property);
160 var value = el.style[property],
164 computed = el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null);
165 if (computed) { // test computed before touching for safari
166 value = computed[property];
172 } else if (documentElement[CURRENT_STYLE]) {
173 return function(el, property) {
177 case 'opacity' :// IE opacity uses filter
179 try { // will error if no DXImageTransform
180 value = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
183 try { // make sure its in the document
184 value = el.filters('alpha').opacity;
189 case 'float': // fix reserved word
190 property = 'styleFloat'; // fall through
192 property = Y.Dom._toCamel(property);
193 value = el[CURRENT_STYLE] ? el[CURRENT_STYLE][property] : null;
194 return ( el.style[property] || value );
201 * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
203 * @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.
204 * @param {String} property The style property to be set.
205 * @param {String} val The value to apply to the given property.
207 setStyle: function(el, property, val) {
208 Y.Dom.batch(el, Y.Dom._setStyle, { prop: property, val: val });
211 _setStyle: function() {
212 if (!window.getComputedStyle && document.documentElement.currentStyle) {
213 return function(el, args) {
214 var property = Y.Dom._toCamel(args.prop),
220 // remove filter if unsetting or full opacity
221 if (val === '' || val === null || val === 1) {
222 el.style.removeAttribute('filter');
223 } else if ( lang.isString(el.style.filter) ) { // in case not appended
224 el.style.filter = 'alpha(opacity=' + val * 100 + ')';
226 if (!el[CURRENT_STYLE] || !el[CURRENT_STYLE].hasLayout) {
227 el.style.zoom = 1; // when no layout or cant tell
232 property = 'styleFloat';
234 el.style[property] = val;
240 return function(el, args) {
241 var property = Y.Dom._toCamel(args.prop),
244 if (property == 'float') {
245 property = 'cssFloat';
247 el.style[property] = val;
256 * Gets the current position of an element based on page coordinates.
257 * Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
259 * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM
260 * reference, or an Array of IDs and/or HTMLElements
261 * @return {Array} The XY position of the element(s)
263 getXY: function(el) {
264 return Y.Dom.batch(el, Y.Dom._getXY);
267 _canPosition: function(el) {
268 return ( Y.Dom._getStyle(el, 'display') !== 'none' && Y.Dom._inDoc(el) );
271 _getXY: function(node) {
272 var scrollLeft, scrollTop, box, doc,
273 clientTop, clientLeft,
274 round = Math.round, // 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 = [box[LEFT], box[TOP]];
284 // remove IE default documentElement offset (border)
285 if (clientTop || clientLeft) {
290 if ((scrollTop || scrollLeft)) {
295 // gecko may return sub-pixel (non-int) values
296 xy[0] = round(xy[0]);
297 xy[1] = round(xy[1]);
305 * 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).
307 * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
308 * @return {Number | Array} The X position of the element(s)
311 var f = function(el) {
312 return Y.Dom.getXY(el)[0];
315 return Y.Dom.batch(el, f, Y.Dom, true);
319 * 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).
321 * @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
322 * @return {Number | Array} The Y position of the element(s)
325 var f = function(el) {
326 return Y.Dom.getXY(el)[1];
329 return Y.Dom.batch(el, f, Y.Dom, true);
333 * Set the position of an html element in page coordinates, regardless of how the element is positioned.
334 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
336 * @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
337 * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
338 * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
340 setXY: function(el, pos, noRetry) {
341 Y.Dom.batch(el, Y.Dom._setXY, { pos: pos, noRetry: noRetry });
344 _setXY: function(node, args) {
345 var pos = Y.Dom._getStyle(node, POSITION),
346 setStyle = Y.Dom.setStyle,
348 noRetry = args.noRetry,
350 delta = [ // assuming pixels; if not we will have to retry
351 parseInt( Y.Dom.getComputedStyle(node, LEFT), 10 ),
352 parseInt( Y.Dom.getComputedStyle(node, TOP), 10 )
358 currentXY = Y.Dom._getXY(node);
360 if (!xy || currentXY === false) { // has to be part of doc to have xy
364 if (pos == 'static') { // default to relative
366 setStyle(node, POSITION, pos);
369 if ( isNaN(delta[0]) ) {// in case of 'auto'
370 delta[0] = (pos == RELATIVE) ? 0 : node[OFFSET_LEFT];
372 if ( isNaN(delta[1]) ) { // in case of 'auto'
373 delta[1] = (pos == RELATIVE) ? 0 : node[OFFSET_TOP];
376 if (xy[0] !== null) { // from setX
377 setStyle(node, LEFT, xy[0] - currentXY[0] + delta[0] + 'px');
380 if (xy[1] !== null) { // from setY
381 setStyle(node, TOP, xy[1] - currentXY[1] + delta[1] + 'px');
385 newXY = Y.Dom._getXY(node);
387 // if retry is true, try one more time if we miss
388 if ( (xy[0] !== null && newXY[0] != xy[0]) ||
389 (xy[1] !== null && newXY[1] != xy[1]) ) {
390 Y.Dom._setXY(node, { pos: xy, noRetry: true });
397 * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
398 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
400 * @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.
401 * @param {Int} x The value to use as the X coordinate for the element(s).
403 setX: function(el, x) {
404 Y.Dom.setXY(el, [x, null]);
408 * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
409 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
411 * @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.
412 * @param {Int} x To use as the Y coordinate for the element(s).
414 setY: function(el, y) {
415 Y.Dom.setXY(el, [null, y]);
419 * Returns the region position of the given element.
420 * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
422 * @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.
423 * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
425 getRegion: function(el) {
426 var f = function(el) {
428 if ( Y.Dom._canPosition(el) ) {
429 region = Y.Region.getRegion(el);
436 return Y.Dom.batch(el, f, Y.Dom, true);
440 * Returns the width of the client (viewport).
441 * @method getClientWidth
442 * @deprecated Now using getViewportWidth. This interface left intact for back compat.
443 * @return {Int} The width of the viewable area of the page.
445 getClientWidth: function() {
446 return Y.Dom.getViewportWidth();
450 * Returns the height of the client (viewport).
451 * @method getClientHeight
452 * @deprecated Now using getViewportHeight. This interface left intact for back compat.
453 * @return {Int} The height of the viewable area of the page.
455 getClientHeight: function() {
456 return Y.Dom.getViewportHeight();
460 * Returns an array of HTMLElements with the given class.
461 * For optimized performance, include a tag and/or root node when possible.
462 * Note: This method operates against a live collection, so modifying the
463 * collection in the callback (removing/appending nodes, etc.) will have
464 * side effects. Instead you should iterate the returned nodes array,
465 * as you would with the native "getElementsByTagName" method.
466 * @method getElementsByClassName
467 * @param {String} className The class name to match against
468 * @param {String} tag (optional) The tag name of the elements being collected
469 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point.
470 * This element is not included in the className scan.
471 * @param {Function} apply (optional) A function to apply to each element when found
472 * @param {Any} o (optional) An optional arg that is passed to the supplied method
473 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
474 * @return {Array} An array of elements that have the given class name
476 getElementsByClassName: function(className, tag, root, apply, o, overrides) {
478 root = (root) ? Y.Dom.get(root) : null || document;
484 elements = root.getElementsByTagName(tag),
485 hasClass = Y.Dom.hasClass;
487 for (var i = 0, len = elements.length; i < len; ++i) {
488 if ( hasClass(elements[i], className) ) {
489 nodes[nodes.length] = elements[i];
494 Y.Dom.batch(nodes, apply, o, overrides);
501 * Determines whether an HTMLElement has the given className.
503 * @param {String | HTMLElement | Array} el The element or collection to test
504 * @param {String | RegExp} className the class name to search for, or a regular
505 * expression to match against
506 * @return {Boolean | Array} A boolean value or array of boolean values
508 hasClass: function(el, className) {
509 return Y.Dom.batch(el, Y.Dom._hasClass, className);
512 _hasClass: function(el, className) {
516 if (el && className) {
517 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
518 if (current) { // convert line breaks, tabs and other delims to spaces
519 current = current.replace(/\s+/g, SPACE);
522 if (className.exec) {
523 ret = className.test(current);
525 ret = className && (SPACE + current + SPACE).
526 indexOf(SPACE + className + SPACE) > -1;
535 * Adds a class name to a given element or collection of elements.
537 * @param {String | HTMLElement | Array} el The element or collection to add the class to
538 * @param {String} className the class name to add to the class attribute
539 * @return {Boolean | Array} A pass/fail boolean or array of booleans
541 addClass: function(el, className) {
542 return Y.Dom.batch(el, Y.Dom._addClass, className);
545 _addClass: function(el, className) {
549 if (el && className) {
550 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
551 if ( !Y.Dom._hasClass(el, className) ) {
552 Y.Dom.setAttribute(el, CLASS_NAME, trim(current + SPACE + className));
562 * Removes a class name from a given element or collection of elements.
563 * @method removeClass
564 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
565 * @param {String} className the class name to remove from the class attribute
566 * @return {Boolean | Array} A pass/fail boolean or array of booleans
568 removeClass: function(el, className) {
569 return Y.Dom.batch(el, Y.Dom._removeClass, className);
572 _removeClass: function(el, className) {
578 if (el && className) {
579 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
580 Y.Dom.setAttribute(el, CLASS_NAME, current.replace(Y.Dom._getClassRegex(className), EMPTY));
582 newClass = Y.Dom._getAttribute(el, CLASS_NAME);
583 if (current !== newClass) { // else nothing changed
584 Y.Dom.setAttribute(el, CLASS_NAME, trim(newClass)); // trim after comparing to current class
587 if (Y.Dom._getAttribute(el, CLASS_NAME) === '') { // remove class attribute if empty
588 attr = (el.hasAttribute && el.hasAttribute(_CLASS)) ? _CLASS : CLASS_NAME;
589 el.removeAttribute(attr);
600 * Replace a class with another class for a given element or collection of elements.
601 * If no oldClassName is present, the newClassName is simply added.
602 * @method replaceClass
603 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
604 * @param {String} oldClassName the class name to be replaced
605 * @param {String} newClassName the class name that will be replacing the old class name
606 * @return {Boolean | Array} A pass/fail boolean or array of booleans
608 replaceClass: function(el, oldClassName, newClassName) {
609 return Y.Dom.batch(el, Y.Dom._replaceClass, { from: oldClassName, to: newClassName });
612 _replaceClass: function(el, classObj) {
619 if (el && classObj) {
620 from = classObj.from;
625 } else if (!from) { // just add if no "from"
626 ret = Y.Dom._addClass(el, classObj.to);
627 } else if (from !== to) { // else nothing to replace
628 // May need to lead with DBLSPACE?
629 current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
630 className = (SPACE + current.replace(Y.Dom._getClassRegex(from), SPACE + to).
631 replace(/\s+/g, SPACE)). // normalize white space
632 split(Y.Dom._getClassRegex(to));
634 // insert to into what would have been the first occurrence slot
635 className.splice(1, 0, SPACE + to);
636 Y.Dom.setAttribute(el, CLASS_NAME, trim(className.join(EMPTY)));
646 * Returns an ID and applies it to the element "el", if provided.
648 * @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).
649 * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
650 * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
652 generateId: function(el, prefix) {
653 prefix = prefix || 'yui-gen';
655 var f = function(el) {
656 if (el && el.id) { // do not override existing ID
660 var id = prefix + YAHOO.env._id_counter++;
663 if (el[OWNER_DOCUMENT] && el[OWNER_DOCUMENT].getElementById(id)) { // in case one already exists
664 // use failed id plus prefix to help ensure uniqueness
665 return Y.Dom.generateId(el, id + prefix);
673 // batch fails when no element, so just generate and return single ID
674 return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
678 * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
680 * @param {String | HTMLElement} haystack The possible ancestor
681 * @param {String | HTMLElement} needle The possible descendent
682 * @return {Boolean} Whether or not the haystack is an ancestor of needle
684 isAncestor: function(haystack, needle) {
685 haystack = Y.Dom.get(haystack);
686 needle = Y.Dom.get(needle);
690 if ( (haystack && needle) && (haystack[NODE_TYPE] && needle[NODE_TYPE]) ) {
691 if (haystack.contains && haystack !== needle) { // contains returns true when equal
692 ret = haystack.contains(needle);
694 else if (haystack.compareDocumentPosition) { // gecko
695 ret = !!(haystack.compareDocumentPosition(needle) & 16);
703 * Determines whether an HTMLElement is present in the current document.
705 * @param {String | HTMLElement} el The element to search for
706 * @param {Object} doc An optional document to search, defaults to element's owner document
707 * @return {Boolean} Whether or not the element is present in the current document
709 inDocument: function(el, doc) {
710 return Y.Dom._inDoc(Y.Dom.get(el), doc);
713 _inDoc: function(el, doc) {
715 if (el && el[TAG_NAME]) {
716 doc = doc || el[OWNER_DOCUMENT];
717 ret = Y.Dom.isAncestor(doc[DOCUMENT_ELEMENT], el);
724 * Returns an array of HTMLElements that pass the test applied by supplied boolean method.
725 * For optimized performance, include a tag and/or root node when possible.
726 * Note: This method operates against a live collection, so modifying the
727 * collection in the callback (removing/appending nodes, etc.) will have
728 * side effects. Instead you should iterate the returned nodes array,
729 * as you would with the native "getElementsByTagName" method.
730 * @method getElementsBy
731 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
732 * @param {String} tag (optional) The tag name of the elements being collected
733 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
734 * @param {Function} apply (optional) A function to apply to each element when found
735 * @param {Any} o (optional) An optional arg that is passed to the supplied method
736 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
737 * @return {Array} Array of HTMLElements
739 getElementsBy: function(method, tag, root, apply, o, overrides, firstOnly) {
741 root = (root) ? Y.Dom.get(root) : null || document;
743 var ret = (firstOnly) ? null : [],
746 // in case Dom.get() returns null
748 elements = root.getElementsByTagName(tag);
749 for (var i = 0, len = elements.length; i < len; ++i) {
750 if ( method(elements[i]) ) {
755 ret[ret.length] = elements[i];
761 Y.Dom.batch(ret, apply, o, overrides);
770 * Returns the first HTMLElement that passes the test applied by the supplied boolean method.
771 * @method getElementBy
772 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
773 * @param {String} tag (optional) The tag name of the elements being collected
774 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
775 * @return {HTMLElement}
777 getElementBy: function(method, tag, root) {
778 return Y.Dom.getElementsBy(method, tag, root, null, null, null, true);
782 * Runs the supplied method against each item in the Collection/Array.
783 * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
785 * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
786 * @param {Function} method The method to apply to the element(s)
787 * @param {Any} o (optional) An optional arg that is passed to the supplied method
788 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
789 * @return {Any | Array} The return value(s) from the supplied method
791 batch: function(el, method, o, overrides) {
793 scope = (overrides) ? o : null;
795 el = (el && (el[TAG_NAME] || el.item)) ? el : Y.Dom.get(el); // skip get() when possible
797 if (el[TAG_NAME] || el.length === undefined) { // element or not array-like
798 return method.call(scope, el, o);
801 for (var i = 0; i < el.length; ++i) {
802 collection[collection.length] = method.call(scope || el[i], el[i], o);
811 * Returns the height of the document.
812 * @method getDocumentHeight
813 * @return {Int} The height of the actual document (which includes the body and its margin).
815 getDocumentHeight: function() {
816 var scrollHeight = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollHeight : documentElement.scrollHeight,
817 h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
823 * Returns the width of the document.
824 * @method getDocumentWidth
825 * @return {Int} The width of the actual document (which includes the body and its margin).
827 getDocumentWidth: function() {
828 var scrollWidth = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollWidth : documentElement.scrollWidth,
829 w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
834 * Returns the current height of the viewport.
835 * @method getViewportHeight
836 * @return {Int} The height of the viewable area of the page (excludes scrollbars).
838 getViewportHeight: function() {
839 var height = self.innerHeight, // Safari, Opera
840 mode = document[COMPAT_MODE];
842 if ( (mode || isIE) && !isOpera ) { // IE, Gecko
843 height = (mode == CSS1_COMPAT) ?
844 documentElement.clientHeight : // Standards
845 document.body.clientHeight; // Quirks
852 * Returns the current width of the viewport.
853 * @method getViewportWidth
854 * @return {Int} The width of the viewable area of the page (excludes scrollbars).
857 getViewportWidth: function() {
858 var width = self.innerWidth, // Safari
859 mode = document[COMPAT_MODE];
861 if (mode || isIE) { // IE, Gecko, Opera
862 width = (mode == CSS1_COMPAT) ?
863 documentElement.clientWidth : // Standards
864 document.body.clientWidth; // Quirks
870 * Returns the nearest ancestor that passes the test applied by supplied boolean method.
871 * For performance reasons, IDs are not accepted and argument validation omitted.
872 * @method getAncestorBy
873 * @param {HTMLElement} node The HTMLElement to use as the starting point
874 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
875 * @return {Object} HTMLElement or null if not found
877 getAncestorBy: function(node, method) {
878 while ( (node = node[PARENT_NODE]) ) { // NOTE: assignment
879 if ( Y.Dom._testElement(node, method) ) {
888 * Returns the nearest ancestor with the given className.
889 * @method getAncestorByClassName
890 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
891 * @param {String} className
892 * @return {Object} HTMLElement
894 getAncestorByClassName: function(node, className) {
895 node = Y.Dom.get(node);
899 var method = function(el) { return Y.Dom.hasClass(el, className); };
900 return Y.Dom.getAncestorBy(node, method);
904 * Returns the nearest ancestor with the given tagName.
905 * @method getAncestorByTagName
906 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
907 * @param {String} tagName
908 * @return {Object} HTMLElement
910 getAncestorByTagName: function(node, tagName) {
911 node = Y.Dom.get(node);
915 var method = function(el) {
916 return el[TAG_NAME] && el[TAG_NAME].toUpperCase() == tagName.toUpperCase();
919 return Y.Dom.getAncestorBy(node, method);
923 * Returns the previous sibling that is an HTMLElement.
924 * For performance reasons, IDs are not accepted and argument validation omitted.
925 * Returns the nearest HTMLElement sibling if no method provided.
926 * @method getPreviousSiblingBy
927 * @param {HTMLElement} node The HTMLElement to use as the starting point
928 * @param {Function} method A boolean function used to test siblings
929 * that receives the sibling node being tested as its only argument
930 * @return {Object} HTMLElement or null if not found
932 getPreviousSiblingBy: function(node, method) {
934 node = node.previousSibling;
935 if ( Y.Dom._testElement(node, method) ) {
943 * Returns the previous sibling that is an HTMLElement
944 * @method getPreviousSibling
945 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
946 * @return {Object} HTMLElement or null if not found
948 getPreviousSibling: function(node) {
949 node = Y.Dom.get(node);
954 return Y.Dom.getPreviousSiblingBy(node);
958 * Returns the next HTMLElement sibling that passes the boolean method.
959 * For performance reasons, IDs are not accepted and argument validation omitted.
960 * Returns the nearest HTMLElement sibling if no method provided.
961 * @method getNextSiblingBy
962 * @param {HTMLElement} node The HTMLElement to use as the starting point
963 * @param {Function} method A boolean function used to test siblings
964 * that receives the sibling node being tested as its only argument
965 * @return {Object} HTMLElement or null if not found
967 getNextSiblingBy: function(node, method) {
969 node = node.nextSibling;
970 if ( Y.Dom._testElement(node, method) ) {
978 * Returns the next sibling that is an HTMLElement
979 * @method getNextSibling
980 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
981 * @return {Object} HTMLElement or null if not found
983 getNextSibling: function(node) {
984 node = Y.Dom.get(node);
989 return Y.Dom.getNextSiblingBy(node);
993 * Returns the first HTMLElement child that passes the test method.
994 * @method getFirstChildBy
995 * @param {HTMLElement} node The HTMLElement to use as the starting point
996 * @param {Function} method A boolean function used to test children
997 * that receives the node being tested as its only argument
998 * @return {Object} HTMLElement or null if not found
1000 getFirstChildBy: function(node, method) {
1001 var child = ( Y.Dom._testElement(node.firstChild, method) ) ? node.firstChild : null;
1002 return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
1006 * Returns the first HTMLElement child.
1007 * @method getFirstChild
1008 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1009 * @return {Object} HTMLElement or null if not found
1011 getFirstChild: function(node, method) {
1012 node = Y.Dom.get(node);
1016 return Y.Dom.getFirstChildBy(node);
1020 * Returns the last HTMLElement child that passes the test method.
1021 * @method getLastChildBy
1022 * @param {HTMLElement} node The HTMLElement to use as the starting point
1023 * @param {Function} method A boolean function used to test children
1024 * that receives the node being tested as its only argument
1025 * @return {Object} HTMLElement or null if not found
1027 getLastChildBy: function(node, method) {
1031 var child = ( Y.Dom._testElement(node.lastChild, method) ) ? node.lastChild : null;
1032 return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
1036 * Returns the last HTMLElement child.
1037 * @method getLastChild
1038 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1039 * @return {Object} HTMLElement or null if not found
1041 getLastChild: function(node) {
1042 node = Y.Dom.get(node);
1043 return Y.Dom.getLastChildBy(node);
1047 * Returns an array of HTMLElement childNodes that pass the test method.
1048 * @method getChildrenBy
1049 * @param {HTMLElement} node The HTMLElement to start from
1050 * @param {Function} method A boolean function used to test children
1051 * that receives the node being tested as its only argument
1052 * @return {Array} A static array of HTMLElements
1054 getChildrenBy: function(node, method) {
1055 var child = Y.Dom.getFirstChildBy(node, method),
1056 children = child ? [child] : [];
1058 Y.Dom.getNextSiblingBy(child, function(node) {
1059 if ( !method || method(node) ) {
1060 children[children.length] = node;
1062 return false; // fail test to collect all children
1069 * Returns an array of HTMLElement childNodes.
1070 * @method getChildren
1071 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1072 * @return {Array} A static array of HTMLElements
1074 getChildren: function(node) {
1075 node = Y.Dom.get(node);
1079 return Y.Dom.getChildrenBy(node);
1083 * Returns the left scroll value of the document
1084 * @method getDocumentScrollLeft
1085 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1086 * @return {Int} The amount that the document is scrolled to the left
1088 getDocumentScrollLeft: function(doc) {
1089 doc = doc || document;
1090 return Math.max(doc[DOCUMENT_ELEMENT].scrollLeft, doc.body.scrollLeft);
1094 * Returns the top scroll value of the document
1095 * @method getDocumentScrollTop
1096 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1097 * @return {Int} The amount that the document is scrolled to the top
1099 getDocumentScrollTop: function(doc) {
1100 doc = doc || document;
1101 return Math.max(doc[DOCUMENT_ELEMENT].scrollTop, doc.body.scrollTop);
1105 * Inserts the new node as the previous sibling of the reference node
1106 * @method insertBefore
1107 * @param {String | HTMLElement} newNode The node to be inserted
1108 * @param {String | HTMLElement} referenceNode The node to insert the new node before
1109 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1111 insertBefore: function(newNode, referenceNode) {
1112 newNode = Y.Dom.get(newNode);
1113 referenceNode = Y.Dom.get(referenceNode);
1115 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1119 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode);
1123 * Inserts the new node as the next sibling of the reference node
1124 * @method insertAfter
1125 * @param {String | HTMLElement} newNode The node to be inserted
1126 * @param {String | HTMLElement} referenceNode The node to insert the new node after
1127 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1129 insertAfter: function(newNode, referenceNode) {
1130 newNode = Y.Dom.get(newNode);
1131 referenceNode = Y.Dom.get(referenceNode);
1133 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1137 if (referenceNode.nextSibling) {
1138 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode.nextSibling);
1140 return referenceNode[PARENT_NODE].appendChild(newNode);
1145 * Creates a Region based on the viewport relative to the document.
1146 * @method getClientRegion
1147 * @return {Region} A Region object representing the viewport which accounts for document scroll
1149 getClientRegion: function() {
1150 var t = Y.Dom.getDocumentScrollTop(),
1151 l = Y.Dom.getDocumentScrollLeft(),
1152 r = Y.Dom.getViewportWidth() + l,
1153 b = Y.Dom.getViewportHeight() + t;
1155 return new Y.Region(t, r, b, l);
1159 * Provides a normalized attribute interface.
1160 * @method setAttribute
1161 * @param {String | HTMLElement} el The target element for the attribute.
1162 * @param {String} attr The attribute to set.
1163 * @param {String} val The value of the attribute.
1165 setAttribute: function(el, attr, val) {
1166 Y.Dom.batch(el, Y.Dom._setAttribute, { attr: attr, val: val });
1169 _setAttribute: function(el, args) {
1170 var attr = Y.Dom._toCamel(args.attr),
1173 if (el && el.setAttribute) {
1174 // set as DOM property, except for BUTTON, which errors on property setter
1175 if (Y.Dom.DOT_ATTRIBUTES[attr] && el.tagName && el.tagName != 'BUTTON') {
1178 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1179 el.setAttribute(attr, val);
1186 * Provides a normalized attribute interface.
1187 * @method getAttribute
1188 * @param {String | HTMLElement} el The target element for the attribute.
1189 * @param {String} attr The attribute to get.
1190 * @return {String} The current value of the attribute.
1192 getAttribute: function(el, attr) {
1193 return Y.Dom.batch(el, Y.Dom._getAttribute, attr);
1197 _getAttribute: function(el, attr) {
1199 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1201 if (Y.Dom.DOT_ATTRIBUTES[attr]) {
1203 } else if (el && 'getAttribute' in el) {
1204 if (/^(?:href|src)$/.test(attr)) { // use IE flag to return exact value
1205 val = el.getAttribute(attr, 2);
1207 val = el.getAttribute(attr);
1215 _toCamel: function(property) {
1216 var c = propertyCache;
1219 return l.toUpperCase();
1222 return c[property] || (c[property] = property.indexOf('-') === -1 ?
1224 property.replace( /-([a-z])/gi, tU ));
1227 _getClassRegex: function(className) {
1229 if (className !== undefined) { // allow empty string to pass
1230 if (className.exec) { // already a RegExp
1233 re = reCache[className];
1235 // escape special chars (".", "[", etc.)
1236 className = className.replace(Y.Dom._patterns.CLASS_RE_TOKENS, '\\$1');
1237 className = className.replace(/\s+/g, SPACE); // convert line breaks and other delims
1238 re = reCache[className] = new RegExp(C_START + className + C_END, G);
1246 ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
1247 CLASS_RE_TOKENS: /([\.\(\)\^\$\*\+\?\|\[\]\{\}\\])/g
1251 _testElement: function(node, method) {
1252 return node && node[NODE_TYPE] == 1 && ( !method || method(node) );
1255 _calcBorders: function(node, xy2) {
1256 var t = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_TOP_WIDTH), 10) || 0,
1257 l = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_LEFT_WIDTH), 10) || 0;
1259 if (RE_TABLE.test(node[TAG_NAME])) {
1270 var _getComputedStyle = Y.Dom[GET_COMPUTED_STYLE];
1271 // fix opera computedStyle default color unit (convert to rgb)
1273 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1274 var val = _getComputedStyle(node, att);
1275 if (RE_COLOR.test(att)) {
1276 val = Y.Dom.Color.toRGB(val);
1284 // safari converts transparent to rgba(), others use "transparent"
1286 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1287 var val = _getComputedStyle(node, att);
1289 if (val === 'rgba(0, 0, 0, 0)') {
1290 val = 'transparent';
1298 if (UA.ie && UA.ie >= 8) {
1299 Y.Dom.DOT_ATTRIBUTES.type = true; // IE 8 errors on input.setAttribute('type')
1303 * A region is a representation of an object on a grid. It is defined
1304 * by the top, right, bottom, left extents, so is rectangular by default. If
1305 * other shapes are required, this class could be extended to support it.
1306 * @namespace YAHOO.util
1308 * @param {Int} t the top extent
1309 * @param {Int} r the right extent
1310 * @param {Int} b the bottom extent
1311 * @param {Int} l the left extent
1314 YAHOO.util.Region = function(t, r, b, l) {
1317 * The region's top extent
1324 * The region's top extent
1331 * The region's top extent as index, for symmetry with set/getXY
1338 * The region's right extent
1345 * The region's bottom extent
1352 * The region's left extent
1359 * The region's left extent
1366 * The region's left extent as index, for symmetry with set/getXY
1373 * The region's total width
1377 this.width = this.right - this.left;
1380 * The region's total height
1384 this.height = this.bottom - this.top;
1388 * Returns true if this region contains the region passed in
1390 * @param {Region} region The region to evaluate
1391 * @return {Boolean} True if the region is contained with this region,
1394 YAHOO.util.Region.prototype.contains = function(region) {
1395 return ( region.left >= this.left &&
1396 region.right <= this.right &&
1397 region.top >= this.top &&
1398 region.bottom <= this.bottom );
1403 * Returns the area of the region
1405 * @return {Int} the region's area
1407 YAHOO.util.Region.prototype.getArea = function() {
1408 return ( (this.bottom - this.top) * (this.right - this.left) );
1412 * Returns the region where the passed in region overlaps with this one
1414 * @param {Region} region The region that intersects
1415 * @return {Region} The overlap region, or null if there is no overlap
1417 YAHOO.util.Region.prototype.intersect = function(region) {
1418 var t = Math.max( this.top, region.top ),
1419 r = Math.min( this.right, region.right ),
1420 b = Math.min( this.bottom, region.bottom ),
1421 l = Math.max( this.left, region.left );
1423 if (b >= t && r >= l) {
1424 return new YAHOO.util.Region(t, r, b, l);
1431 * Returns the region representing the smallest region that can contain both
1432 * the passed in region and this region.
1434 * @param {Region} region The region that to create the union with
1435 * @return {Region} The union region
1437 YAHOO.util.Region.prototype.union = function(region) {
1438 var t = Math.min( this.top, region.top ),
1439 r = Math.max( this.right, region.right ),
1440 b = Math.max( this.bottom, region.bottom ),
1441 l = Math.min( this.left, region.left );
1443 return new YAHOO.util.Region(t, r, b, l);
1449 * @return string the region properties
1451 YAHOO.util.Region.prototype.toString = function() {
1452 return ( "Region {" +
1453 "top: " + this.top +
1454 ", right: " + this.right +
1455 ", bottom: " + this.bottom +
1456 ", left: " + this.left +
1457 ", height: " + this.height +
1458 ", width: " + this.width +
1463 * Returns a region that is occupied by the DOM element
1465 * @param {HTMLElement} el The element
1466 * @return {Region} The region that the element occupies
1469 YAHOO.util.Region.getRegion = function(el) {
1470 var p = YAHOO.util.Dom.getXY(el),
1472 r = p[0] + el.offsetWidth,
1473 b = p[1] + el.offsetHeight,
1476 return new YAHOO.util.Region(t, r, b, l);
1479 /////////////////////////////////////////////////////////////////////////////
1483 * A point is a region that is special in that it represents a single point on
1485 * @namespace YAHOO.util
1487 * @param {Int} x The X position of the point
1488 * @param {Int} y The Y position of the point
1490 * @extends YAHOO.util.Region
1492 YAHOO.util.Point = function(x, y) {
1493 if (YAHOO.lang.isArray(x)) { // accept input from Dom.getXY, Event.getXY, etc.
1494 y = x[1]; // dont blow away x yet
1498 YAHOO.util.Point.superclass.constructor.call(this, y, x, y, x);
1501 YAHOO.extend(YAHOO.util.Point, YAHOO.util.Region);
1505 * Internal methods used to add style management functionality to DOM.
1508 * @namespace YAHOO.util.Dom
1512 CLIENT_TOP = 'clientTop',
1513 CLIENT_LEFT = 'clientLeft',
1514 PARENT_NODE = 'parentNode',
1516 HAS_LAYOUT = 'hasLayout',
1518 OPACITY = 'opacity',
1520 BORDER_LEFT_WIDTH = 'borderLeftWidth',
1521 BORDER_TOP_WIDTH = 'borderTopWidth',
1522 BORDER_RIGHT_WIDTH = 'borderRightWidth',
1523 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
1524 VISIBLE = 'visible',
1525 TRANSPARENT = 'transparent',
1529 CURRENT_STYLE = 'currentStyle',
1531 // IE getComputedStyle
1532 // TODO: unit-less lineHeight (e.g. 1.22)
1533 re_size = /^width|height$/,
1534 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,
1539 * @description Method used by DOM to get style information for IE
1540 * @param {HTMLElement} el The element to check
1541 * @param {String} property The property to check
1542 * @returns {String} The computed style
1544 get: function(el, property) {
1546 current = el[CURRENT_STYLE][property];
1548 if (property === OPACITY) {
1549 value = Y.Dom.getStyle(el, OPACITY);
1550 } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
1552 } else if (Y.Dom.IE_COMPUTED[property]) { // use compute function
1553 value = Y.Dom.IE_COMPUTED[property](el, property);
1554 } else if (re_unit.test(current)) { // convert to pixel
1555 value = Y.Dom.IE.ComputedStyle.getPixel(el, property);
1564 * @description Determine the offset of an element
1565 * @param {HTMLElement} el The element to check
1566 * @param {String} prop The property to check.
1567 * @return {String} The offset
1569 getOffset: function(el, prop) {
1570 var current = el[CURRENT_STYLE][prop], // value of "width", "top", etc.
1571 capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
1572 offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
1573 pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
1577 if (current == AUTO) {
1578 actual = el[offset]; // offsetHeight/Top etc.
1579 if (actual === undefined) { // likely "right" or "bottom"
1584 if (re_size.test(prop)) { // account for box model diff
1585 el[STYLE][prop] = actual;
1586 if (el[offset] > actual) {
1587 // the difference is padding + border (works in Standards & Quirks modes)
1588 value = actual - (el[offset] - actual);
1590 el[STYLE][prop] = AUTO; // revert to auto
1592 } else { // convert units to px
1593 if (!el[STYLE][pixel] && !el[STYLE][prop]) { // need to map style.width to currentStyle (no currentStyle.pixelWidth)
1594 el[STYLE][prop] = current; // no style.pixelWidth if no style.width
1596 value = el[STYLE][pixel];
1601 * @method getBorderWidth
1602 * @description Try to determine the width of an elements border
1603 * @param {HTMLElement} el The element to check
1604 * @param {String} property The property to check
1605 * @return {String} The elements border width
1607 getBorderWidth: function(el, property) {
1608 // clientHeight/Width = paddingBox (e.g. offsetWidth - borderWidth)
1609 // clientTop/Left = borderWidth
1611 if (!el[CURRENT_STYLE][HAS_LAYOUT]) { // TODO: unset layout?
1612 el[STYLE].zoom = 1; // need layout to measure client
1616 case BORDER_TOP_WIDTH:
1617 value = el[CLIENT_TOP];
1619 case BORDER_BOTTOM_WIDTH:
1620 value = el.offsetHeight - el.clientHeight - el[CLIENT_TOP];
1622 case BORDER_LEFT_WIDTH:
1623 value = el[CLIENT_LEFT];
1625 case BORDER_RIGHT_WIDTH:
1626 value = el.offsetWidth - el.clientWidth - el[CLIENT_LEFT];
1633 * @description Get the pixel value from a style property
1634 * @param {HTMLElement} node The element to check
1635 * @param {String} att The attribute to check
1636 * @return {String} The pixel value
1638 getPixel: function(node, att) {
1639 // use pixelRight to convert to px
1641 styleRight = node[CURRENT_STYLE][RIGHT],
1642 current = node[CURRENT_STYLE][att];
1644 node[STYLE][RIGHT] = current;
1645 val = node[STYLE].pixelRight;
1646 node[STYLE][RIGHT] = styleRight; // revert
1653 * @description Get the margin value from a style property
1654 * @param {HTMLElement} node The element to check
1655 * @param {String} att The attribute to check
1656 * @return {String} The margin value
1658 getMargin: function(node, att) {
1660 if (node[CURRENT_STYLE][att] == AUTO) {
1663 val = Y.Dom.IE.ComputedStyle.getPixel(node, att);
1669 * @method getVisibility
1670 * @description Get the visibility of an element
1671 * @param {HTMLElement} node The element to check
1672 * @param {String} att The attribute to check
1673 * @return {String} The value
1675 getVisibility: function(node, att) {
1677 while ( (current = node[CURRENT_STYLE]) && current[att] == 'inherit') { // NOTE: assignment in test
1678 node = node[PARENT_NODE];
1680 return (current) ? current[att] : VISIBLE;
1685 * @description Get the color of an element
1686 * @param {HTMLElement} node The element to check
1687 * @param {String} att The attribute to check
1688 * @return {String} The value
1690 getColor: function(node, att) {
1691 return Y.Dom.Color.toRGB(node[CURRENT_STYLE][att]) || TRANSPARENT;
1695 * @method getBorderColor
1696 * @description Get the bordercolor of an element
1697 * @param {HTMLElement} node The element to check
1698 * @param {String} att The attribute to check
1699 * @return {String} The value
1701 getBorderColor: function(node, att) {
1702 var current = node[CURRENT_STYLE],
1703 val = current[att] || current.color;
1704 return Y.Dom.Color.toRGB(Y.Dom.Color.toHex(val));
1709 //fontSize: getPixelFont,
1712 IEComputed.top = IEComputed.right = IEComputed.bottom = IEComputed.left =
1713 IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
1715 IEComputed.color = ComputedStyle.getColor;
1717 IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
1718 IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
1719 ComputedStyle.getBorderWidth;
1721 IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
1722 IEComputed.marginLeft = ComputedStyle.getMargin;
1724 IEComputed.visibility = ComputedStyle.getVisibility;
1725 IEComputed.borderColor = IEComputed.borderTopColor =
1726 IEComputed.borderRightColor = IEComputed.borderBottomColor =
1727 IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
1729 Y.Dom.IE_COMPUTED = IEComputed;
1730 Y.Dom.IE_ComputedStyle = ComputedStyle;
1734 * Add style management functionality to DOM.
1737 * @namespace YAHOO.util.Dom
1740 var TO_STRING = 'toString',
1741 PARSE_INT = parseInt,
1747 * @property KEYWORDS
1749 * @description Color keywords used when converting to Hex
1773 * @description Regex to parse rgb(0,0,0) formatted strings
1775 re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
1780 * @description Regex to parse #123456 formatted strings
1782 re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
1787 * @description Regex to parse #123 formatted strings
1789 re_hex3: /([0-9A-F])/gi,
1792 * @description Converts a hex or color string to an rgb string: rgb(0,0,0)
1793 * @param {String} val The string to convert to RGB notation.
1794 * @returns {String} The converted string
1796 toRGB: function(val) {
1797 if (!Y.Dom.Color.re_RGB.test(val)) {
1798 val = Y.Dom.Color.toHex(val);
1801 if(Y.Dom.Color.re_hex.exec(val)) {
1803 PARSE_INT(RE.$1, 16),
1804 PARSE_INT(RE.$2, 16),
1805 PARSE_INT(RE.$3, 16)
1812 * @description Converts an rgb or color string to a hex string: #123456
1813 * @param {String} val The string to convert to hex notation.
1814 * @returns {String} The converted string
1816 toHex: function(val) {
1817 val = Y.Dom.Color.KEYWORDS[val] || val;
1818 if (Y.Dom.Color.re_RGB.exec(val)) {
1820 Number(RE.$1).toString(16),
1821 Number(RE.$2).toString(16),
1822 Number(RE.$3).toString(16)
1825 for (var i = 0; i < val.length; i++) {
1826 if (val[i].length < 2) {
1827 val[i] = '0' + val[i];
1834 if (val.length < 6) {
1835 val = val.replace(Y.Dom.Color.re_hex3, '$1$1');
1838 if (val !== 'transparent' && val.indexOf('#') < 0) {
1842 return val.toUpperCase();
1846 YAHOO.register("dom", YAHOO.util.Dom, {version: "2.9.0", build: "2800"});