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;
187 YAHOO.log('getStyle: IE filter failed',
192 case 'float': // fix reserved word
193 property = 'styleFloat'; // fall through
195 property = Y.Dom._toCamel(property);
196 value = el[CURRENT_STYLE] ? el[CURRENT_STYLE][property] : null;
197 return ( el.style[property] || value );
204 * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
206 * @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.
207 * @param {String} property The style property to be set.
208 * @param {String} val The value to apply to the given property.
210 setStyle: function(el, property, val) {
211 Y.Dom.batch(el, Y.Dom._setStyle, { prop: property, val: val });
214 _setStyle: function() {
216 return function(el, args) {
217 var property = Y.Dom._toCamel(args.prop),
223 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;
237 YAHOO.log('element ' + el + ' is undefined', 'error', 'Dom');
241 return function(el, args) {
242 var property = Y.Dom._toCamel(args.prop),
245 if (property == 'float') {
246 property = 'cssFloat';
248 el.style[property] = val;
250 YAHOO.log('element ' + el + ' is undefined', 'error', 'Dom');
258 * Gets the current position of an element based on page coordinates.
259 * Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
261 * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM
262 * reference, or an Array of IDs and/or HTMLElements
263 * @return {Array} The XY position of the element(s)
265 getXY: function(el) {
266 return Y.Dom.batch(el, Y.Dom._getXY);
269 _canPosition: function(el) {
270 return ( Y.Dom._getStyle(el, 'display') !== 'none' && Y.Dom._inDoc(el) );
274 if (document[DOCUMENT_ELEMENT][GET_BOUNDING_CLIENT_RECT]) {
275 return function(node) {
276 var scrollLeft, scrollTop, box, doc,
277 off1, off2, mode, bLeft, bTop,
278 floor = Math.floor, // TODO: round?
281 if (Y.Dom._canPosition(node)) {
282 box = node[GET_BOUNDING_CLIENT_RECT]();
283 doc = node[OWNER_DOCUMENT];
284 scrollLeft = Y.Dom.getDocumentScrollLeft(doc);
285 scrollTop = Y.Dom.getDocumentScrollTop(doc);
286 xy = [floor(box[LEFT]), floor(box[TOP])];
288 if (isIE && UA.ie < 8) { // IE < 8: viewport off by 2
291 mode = doc[COMPAT_MODE];
292 bLeft = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_LEFT_WIDTH);
293 bTop = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_TOP_WIDTH);
296 if (mode !== _BACK_COMPAT) {
302 if ((mode == _BACK_COMPAT)) {
303 if (bLeft !== MEDIUM) {
304 off1 = parseInt(bLeft, 10);
306 if (bTop !== MEDIUM) {
307 off2 = parseInt(bTop, 10);
316 if ((scrollTop || scrollLeft)) {
321 // gecko may return sub-pixel (non-int) values
322 xy[0] = floor(xy[0]);
323 xy[1] = floor(xy[1]);
325 YAHOO.log('getXY failed: element not positionable (either not in a document or not displayed)', 'error', 'Dom');
331 return function(node) { // ff2, safari: manually calculate by crawling up offsetParents
332 var docScrollLeft, docScrollTop,
333 scrollTop, scrollLeft,
338 if (Y.Dom._canPosition(node) ) {
339 xy = [node[OFFSET_LEFT], node[OFFSET_TOP]];
340 docScrollLeft = Y.Dom.getDocumentScrollLeft(node[OWNER_DOCUMENT]);
341 docScrollTop = Y.Dom.getDocumentScrollTop(node[OWNER_DOCUMENT]);
343 // TODO: refactor with !! or just falsey
344 bCheck = ((isGecko || UA.webkit > 519) ? true : false);
346 // TODO: worth refactoring for TOP/LEFT only?
347 while ((parentNode = parentNode[OFFSET_PARENT])) {
348 xy[0] += parentNode[OFFSET_LEFT];
349 xy[1] += parentNode[OFFSET_TOP];
351 xy = Y.Dom._calcBorders(parentNode, xy);
355 // account for any scrolled ancestors
356 if (Y.Dom._getStyle(node, POSITION) !== FIXED) {
359 while ((parentNode = parentNode[PARENT_NODE]) && parentNode[TAG_NAME]) {
360 scrollTop = parentNode[SCROLL_TOP];
361 scrollLeft = parentNode[SCROLL_LEFT];
363 //Firefox does something funky with borders when overflow is not visible.
364 if (isGecko && (Y.Dom._getStyle(parentNode, 'overflow') !== 'visible')) {
365 xy = Y.Dom._calcBorders(parentNode, xy);
368 if (scrollTop || scrollLeft) {
373 xy[0] += docScrollLeft;
374 xy[1] += docScrollTop;
377 //Fix FIXED position -- add scrollbars
379 xy[0] -= docScrollLeft;
380 xy[1] -= docScrollTop;
381 } else if (isSafari || isGecko) {
382 xy[0] += docScrollLeft;
383 xy[1] += docScrollTop;
386 //Round the numbers so we get sane data back
387 xy[0] = Math.floor(xy[0]);
388 xy[1] = Math.floor(xy[1]);
390 YAHOO.log('getXY failed: element not positionable (either not in a document or not displayed)', 'error', 'Dom');
395 }(), // NOTE: Executing for loadtime branching
398 * 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).
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 * @return {Number | Array} The X position of the element(s)
404 var f = function(el) {
405 return Y.Dom.getXY(el)[0];
408 return Y.Dom.batch(el, f, Y.Dom, true);
412 * 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).
414 * @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
415 * @return {Number | Array} The Y position of the element(s)
418 var f = function(el) {
419 return Y.Dom.getXY(el)[1];
422 return Y.Dom.batch(el, f, Y.Dom, true);
426 * Set the position of an html element in page coordinates, regardless of how the element is positioned.
427 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
429 * @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
430 * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
431 * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
433 setXY: function(el, pos, noRetry) {
434 Y.Dom.batch(el, Y.Dom._setXY, { pos: pos, noRetry: noRetry });
437 _setXY: function(node, args) {
438 var pos = Y.Dom._getStyle(node, POSITION),
439 setStyle = Y.Dom.setStyle,
441 noRetry = args.noRetry,
443 delta = [ // assuming pixels; if not we will have to retry
444 parseInt( Y.Dom.getComputedStyle(node, LEFT), 10 ),
445 parseInt( Y.Dom.getComputedStyle(node, TOP), 10 )
451 if (pos == 'static') { // default to relative
453 setStyle(node, POSITION, pos);
456 currentXY = Y.Dom._getXY(node);
458 if (!xy || currentXY === false) { // has to be part of doc to have xy
459 YAHOO.log('xy failed: node not available', 'error', 'Node');
463 if ( isNaN(delta[0]) ) {// in case of 'auto'
464 delta[0] = (pos == RELATIVE) ? 0 : node[OFFSET_LEFT];
466 if ( isNaN(delta[1]) ) { // in case of 'auto'
467 delta[1] = (pos == RELATIVE) ? 0 : node[OFFSET_TOP];
470 if (xy[0] !== null) { // from setX
471 setStyle(node, LEFT, xy[0] - currentXY[0] + delta[0] + 'px');
474 if (xy[1] !== null) { // from setY
475 setStyle(node, TOP, xy[1] - currentXY[1] + delta[1] + 'px');
479 newXY = Y.Dom._getXY(node);
481 // if retry is true, try one more time if we miss
482 if ( (xy[0] !== null && newXY[0] != xy[0]) ||
483 (xy[1] !== null && newXY[1] != xy[1]) ) {
484 Y.Dom._setXY(node, { pos: xy, noRetry: true });
488 YAHOO.log('setXY setting position to ' + xy, 'info', 'Node');
492 * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
493 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
495 * @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.
496 * @param {Int} x The value to use as the X coordinate for the element(s).
498 setX: function(el, x) {
499 Y.Dom.setXY(el, [x, null]);
503 * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
504 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
506 * @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.
507 * @param {Int} x To use as the Y coordinate for the element(s).
509 setY: function(el, y) {
510 Y.Dom.setXY(el, [null, y]);
514 * Returns the region position of the given element.
515 * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
517 * @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.
518 * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
520 getRegion: function(el) {
521 var f = function(el) {
523 if ( Y.Dom._canPosition(el) ) {
524 region = Y.Region.getRegion(el);
525 YAHOO.log('getRegion returning ' + region, 'info', 'Dom');
527 YAHOO.log('getRegion failed: element not positionable (either not in a document or not displayed)', 'error', 'Dom');
533 return Y.Dom.batch(el, f, Y.Dom, true);
537 * Returns the width of the client (viewport).
538 * @method getClientWidth
539 * @deprecated Now using getViewportWidth. This interface left intact for back compat.
540 * @return {Int} The width of the viewable area of the page.
542 getClientWidth: function() {
543 return Y.Dom.getViewportWidth();
547 * Returns the height of the client (viewport).
548 * @method getClientHeight
549 * @deprecated Now using getViewportHeight. This interface left intact for back compat.
550 * @return {Int} The height of the viewable area of the page.
552 getClientHeight: function() {
553 return Y.Dom.getViewportHeight();
557 * Returns a array of HTMLElements with the given class.
558 * For optimized performance, include a tag and/or root node when possible.
559 * Note: This method operates against a live collection, so modifying the
560 * collection in the callback (removing/appending nodes, etc.) will have
561 * side effects. Instead you should iterate the returned nodes array,
562 * as you would with the native "getElementsByTagName" method.
563 * @method getElementsByClassName
564 * @param {String} className The class name to match against
565 * @param {String} tag (optional) The tag name of the elements being collected
566 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
567 * @param {Function} apply (optional) A function to apply to each element when found
568 * @param {Any} o (optional) An optional arg that is passed to the supplied method
569 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
570 * @return {Array} An array of elements that have the given class name
572 getElementsByClassName: function(className, tag, root, apply, o, overrides) {
573 className = lang.trim(className);
575 root = (root) ? Y.Dom.get(root) : null || document;
581 elements = root.getElementsByTagName(tag),
582 hasClass = Y.Dom.hasClass;
584 for (var i = 0, len = elements.length; i < len; ++i) {
585 if ( hasClass(elements[i], className) ) {
586 nodes[nodes.length] = elements[i];
591 Y.Dom.batch(nodes, apply, o, overrides);
598 * Determines whether an HTMLElement has the given className.
600 * @param {String | HTMLElement | Array} el The element or collection to test
601 * @param {String} className the class name to search for
602 * @return {Boolean | Array} A boolean value or array of boolean values
604 hasClass: function(el, className) {
605 return Y.Dom.batch(el, Y.Dom._hasClass, className);
608 _hasClass: function(el, className) {
612 if (el && className) {
613 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
614 if (className.exec) {
615 ret = className.test(current);
617 ret = className && (SPACE + current + SPACE).
618 indexOf(SPACE + className + SPACE) > -1;
621 YAHOO.log('hasClass called with invalid arguments', 'warn', 'Dom');
628 * Adds a class name to a given element or collection of elements.
630 * @param {String | HTMLElement | Array} el The element or collection to add the class to
631 * @param {String} className the class name to add to the class attribute
632 * @return {Boolean | Array} A pass/fail boolean or array of booleans
634 addClass: function(el, className) {
635 return Y.Dom.batch(el, Y.Dom._addClass, className);
638 _addClass: function(el, className) {
642 if (el && className) {
643 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
644 if ( !Y.Dom._hasClass(el, className) ) {
645 Y.Dom.setAttribute(el, CLASS_NAME, trim(current + SPACE + className));
649 YAHOO.log('addClass called with invalid arguments', 'warn', 'Dom');
656 * Removes a class name from a given element or collection of elements.
657 * @method removeClass
658 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
659 * @param {String} className the class name to remove from the class attribute
660 * @return {Boolean | Array} A pass/fail boolean or array of booleans
662 removeClass: function(el, className) {
663 return Y.Dom.batch(el, Y.Dom._removeClass, className);
666 _removeClass: function(el, className) {
672 if (el && className) {
673 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
674 Y.Dom.setAttribute(el, CLASS_NAME, current.replace(Y.Dom._getClassRegex(className), EMPTY));
676 newClass = Y.Dom.getAttribute(el, CLASS_NAME);
677 if (current !== newClass) { // else nothing changed
678 Y.Dom.setAttribute(el, CLASS_NAME, trim(newClass)); // trim after comparing to current class
681 if (Y.Dom.getAttribute(el, CLASS_NAME) === '') { // remove class attribute if empty
682 attr = (el.hasAttribute && el.hasAttribute(_CLASS)) ? _CLASS : CLASS_NAME;
683 YAHOO.log('removeClass removing empty class attribute', 'info', 'Dom');
684 el.removeAttribute(attr);
689 YAHOO.log('removeClass called with invalid arguments', 'warn', 'Dom');
696 * Replace a class with another class for a given element or collection of elements.
697 * If no oldClassName is present, the newClassName is simply added.
698 * @method replaceClass
699 * @param {String | HTMLElement | Array} el The element or collection to remove the class from
700 * @param {String} oldClassName the class name to be replaced
701 * @param {String} newClassName the class name that will be replacing the old class name
702 * @return {Boolean | Array} A pass/fail boolean or array of booleans
704 replaceClass: function(el, oldClassName, newClassName) {
705 return Y.Dom.batch(el, Y.Dom._replaceClass, { from: oldClassName, to: newClassName });
708 _replaceClass: function(el, classObj) {
715 if (el && classObj) {
716 from = classObj.from;
721 } else if (!from) { // just add if no "from"
722 ret = Y.Dom._addClass(el, classObj.to);
723 } else if (from !== to) { // else nothing to replace
724 // May need to lead with DBLSPACE?
725 current = Y.Dom.getAttribute(el, CLASS_NAME) || EMPTY;
726 className = (SPACE + current.replace(Y.Dom._getClassRegex(from), SPACE + to)).
727 split(Y.Dom._getClassRegex(to));
729 // insert to into what would have been the first occurrence slot
730 className.splice(1, 0, SPACE + to);
731 Y.Dom.setAttribute(el, CLASS_NAME, trim(className.join(EMPTY)));
735 YAHOO.log('replaceClass called with invalid arguments', 'warn', 'Dom');
742 * Returns an ID and applies it to the element "el", if provided.
744 * @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).
745 * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
746 * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
748 generateId: function(el, prefix) {
749 prefix = prefix || 'yui-gen';
751 var f = function(el) {
752 if (el && el.id) { // do not override existing ID
753 YAHOO.log('generateId returning existing id ' + el.id, 'info', 'Dom');
757 var id = prefix + YAHOO.env._id_counter++;
758 YAHOO.log('generateId generating ' + id, 'info', 'Dom');
761 if (el[OWNER_DOCUMENT].getElementById(id)) { // in case one already exists
762 // use failed id plus prefix to help ensure uniqueness
763 return Y.Dom.generateId(el, id + prefix);
771 // batch fails when no element, so just generate and return single ID
772 return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
776 * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
778 * @param {String | HTMLElement} haystack The possible ancestor
779 * @param {String | HTMLElement} needle The possible descendent
780 * @return {Boolean} Whether or not the haystack is an ancestor of needle
782 isAncestor: function(haystack, needle) {
783 haystack = Y.Dom.get(haystack);
784 needle = Y.Dom.get(needle);
788 if ( (haystack && needle) && (haystack[NODE_TYPE] && needle[NODE_TYPE]) ) {
789 if (haystack.contains && haystack !== needle) { // contains returns true when equal
790 ret = haystack.contains(needle);
792 else if (haystack.compareDocumentPosition) { // gecko
793 ret = !!(haystack.compareDocumentPosition(needle) & 16);
796 YAHOO.log('isAncestor failed; invalid input: ' + haystack + ',' + needle, 'error', 'Dom');
798 YAHOO.log('isAncestor(' + haystack + ',' + needle + ' returning ' + ret, 'info', 'Dom');
803 * Determines whether an HTMLElement is present in the current document.
805 * @param {String | HTMLElement} el The element to search for
806 * @param {Object} doc An optional document to search, defaults to element's owner document
807 * @return {Boolean} Whether or not the element is present in the current document
809 inDocument: function(el, doc) {
810 return Y.Dom._inDoc(Y.Dom.get(el), doc);
813 _inDoc: function(el, doc) {
815 if (el && el[TAG_NAME]) {
816 doc = doc || el[OWNER_DOCUMENT];
817 ret = Y.Dom.isAncestor(doc[DOCUMENT_ELEMENT], el);
819 YAHOO.log('inDocument failed: invalid input', 'error', 'Dom');
825 * Returns a array of HTMLElements that pass the test applied by supplied boolean method.
826 * For optimized performance, include a tag and/or root node when possible.
827 * Note: This method operates against a live collection, so modifying the
828 * collection in the callback (removing/appending nodes, etc.) will have
829 * side effects. Instead you should iterate the returned nodes array,
830 * as you would with the native "getElementsByTagName" method.
831 * @method getElementsBy
832 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
833 * @param {String} tag (optional) The tag name of the elements being collected
834 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
835 * @param {Function} apply (optional) A function to apply to each element when found
836 * @param {Any} o (optional) An optional arg that is passed to the supplied method
837 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
838 * @return {Array} Array of HTMLElements
840 getElementsBy: function(method, tag, root, apply, o, overrides, firstOnly) {
842 root = (root) ? Y.Dom.get(root) : null || document;
849 elements = root.getElementsByTagName(tag);
851 for (var i = 0, len = elements.length; i < len; ++i) {
852 if ( method(elements[i]) ) {
857 nodes[nodes.length] = elements[i];
863 Y.Dom.batch(nodes, apply, o, overrides);
866 YAHOO.log('getElementsBy returning ' + nodes, 'info', 'Dom');
872 * Returns the first HTMLElement that passes the test applied by the supplied boolean method.
873 * @method getElementBy
874 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
875 * @param {String} tag (optional) The tag name of the elements being collected
876 * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
877 * @return {HTMLElement}
879 getElementBy: function(method, tag, root) {
880 return Y.Dom.getElementsBy(method, tag, root, null, null, null, true);
884 * Runs the supplied method against each item in the Collection/Array.
885 * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
887 * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
888 * @param {Function} method The method to apply to the element(s)
889 * @param {Any} o (optional) An optional arg that is passed to the supplied method
890 * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
891 * @return {Any | Array} The return value(s) from the supplied method
893 batch: function(el, method, o, overrides) {
895 scope = (overrides) ? o : window;
897 el = (el && (el[TAG_NAME] || el.item)) ? el : Y.Dom.get(el); // skip get() when possible
899 if (el[TAG_NAME] || el.length === undefined) { // element or not array-like
900 return method.call(scope, el, o);
903 for (var i = 0; i < el.length; ++i) {
904 collection[collection.length] = method.call(scope, el[i], o);
907 YAHOO.log('batch called with invalid arguments', 'warn', 'Dom');
914 * Returns the height of the document.
915 * @method getDocumentHeight
916 * @return {Int} The height of the actual document (which includes the body and its margin).
918 getDocumentHeight: function() {
919 var scrollHeight = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollHeight : documentElement.scrollHeight,
920 h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
922 YAHOO.log('getDocumentHeight returning ' + h, 'info', 'Dom');
927 * Returns the width of the document.
928 * @method getDocumentWidth
929 * @return {Int} The width of the actual document (which includes the body and its margin).
931 getDocumentWidth: function() {
932 var scrollWidth = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollWidth : documentElement.scrollWidth,
933 w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
934 YAHOO.log('getDocumentWidth returning ' + w, 'info', 'Dom');
939 * Returns the current height of the viewport.
940 * @method getViewportHeight
941 * @return {Int} The height of the viewable area of the page (excludes scrollbars).
943 getViewportHeight: function() {
944 var height = self.innerHeight, // Safari, Opera
945 mode = document[COMPAT_MODE];
947 if ( (mode || isIE) && !isOpera ) { // IE, Gecko
948 height = (mode == CSS1_COMPAT) ?
949 documentElement.clientHeight : // Standards
950 document.body.clientHeight; // Quirks
953 YAHOO.log('getViewportHeight returning ' + height, 'info', 'Dom');
958 * Returns the current width of the viewport.
959 * @method getViewportWidth
960 * @return {Int} The width of the viewable area of the page (excludes scrollbars).
963 getViewportWidth: function() {
964 var width = self.innerWidth, // Safari
965 mode = document[COMPAT_MODE];
967 if (mode || isIE) { // IE, Gecko, Opera
968 width = (mode == CSS1_COMPAT) ?
969 documentElement.clientWidth : // Standards
970 document.body.clientWidth; // Quirks
972 YAHOO.log('getViewportWidth returning ' + width, 'info', 'Dom');
977 * Returns the nearest ancestor that passes the test applied by supplied boolean method.
978 * For performance reasons, IDs are not accepted and argument validation omitted.
979 * @method getAncestorBy
980 * @param {HTMLElement} node The HTMLElement to use as the starting point
981 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
982 * @return {Object} HTMLElement or null if not found
984 getAncestorBy: function(node, method) {
985 while ( (node = node[PARENT_NODE]) ) { // NOTE: assignment
986 if ( Y.Dom._testElement(node, method) ) {
987 YAHOO.log('getAncestorBy returning ' + node, 'info', 'Dom');
992 YAHOO.log('getAncestorBy returning null (no ancestor passed test)', 'error', 'Dom');
997 * Returns the nearest ancestor with the given className.
998 * @method getAncestorByClassName
999 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1000 * @param {String} className
1001 * @return {Object} HTMLElement
1003 getAncestorByClassName: function(node, className) {
1004 node = Y.Dom.get(node);
1006 YAHOO.log('getAncestorByClassName failed: invalid node argument', 'error', 'Dom');
1009 var method = function(el) { return Y.Dom.hasClass(el, className); };
1010 return Y.Dom.getAncestorBy(node, method);
1014 * Returns the nearest ancestor with the given tagName.
1015 * @method getAncestorByTagName
1016 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1017 * @param {String} tagName
1018 * @return {Object} HTMLElement
1020 getAncestorByTagName: function(node, tagName) {
1021 node = Y.Dom.get(node);
1023 YAHOO.log('getAncestorByTagName failed: invalid node argument', 'error', 'Dom');
1026 var method = function(el) {
1027 return el[TAG_NAME] && el[TAG_NAME].toUpperCase() == tagName.toUpperCase();
1030 return Y.Dom.getAncestorBy(node, method);
1034 * Returns the previous sibling that is an HTMLElement.
1035 * For performance reasons, IDs are not accepted and argument validation omitted.
1036 * Returns the nearest HTMLElement sibling if no method provided.
1037 * @method getPreviousSiblingBy
1038 * @param {HTMLElement} node The HTMLElement to use as the starting point
1039 * @param {Function} method A boolean function used to test siblings
1040 * that receives the sibling node being tested as its only argument
1041 * @return {Object} HTMLElement or null if not found
1043 getPreviousSiblingBy: function(node, method) {
1045 node = node.previousSibling;
1046 if ( Y.Dom._testElement(node, method) ) {
1054 * Returns the previous sibling that is an HTMLElement
1055 * @method getPreviousSibling
1056 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1057 * @return {Object} HTMLElement or null if not found
1059 getPreviousSibling: function(node) {
1060 node = Y.Dom.get(node);
1062 YAHOO.log('getPreviousSibling failed: invalid node argument', 'error', 'Dom');
1066 return Y.Dom.getPreviousSiblingBy(node);
1070 * Returns the next HTMLElement sibling that passes the boolean method.
1071 * For performance reasons, IDs are not accepted and argument validation omitted.
1072 * Returns the nearest HTMLElement sibling if no method provided.
1073 * @method getNextSiblingBy
1074 * @param {HTMLElement} node The HTMLElement to use as the starting point
1075 * @param {Function} method A boolean function used to test siblings
1076 * that receives the sibling node being tested as its only argument
1077 * @return {Object} HTMLElement or null if not found
1079 getNextSiblingBy: function(node, method) {
1081 node = node.nextSibling;
1082 if ( Y.Dom._testElement(node, method) ) {
1090 * Returns the next sibling that is an HTMLElement
1091 * @method getNextSibling
1092 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1093 * @return {Object} HTMLElement or null if not found
1095 getNextSibling: function(node) {
1096 node = Y.Dom.get(node);
1098 YAHOO.log('getNextSibling failed: invalid node argument', 'error', 'Dom');
1102 return Y.Dom.getNextSiblingBy(node);
1106 * Returns the first HTMLElement child that passes the test method.
1107 * @method getFirstChildBy
1108 * @param {HTMLElement} node The HTMLElement to use as the starting point
1109 * @param {Function} method A boolean function used to test children
1110 * that receives the node being tested as its only argument
1111 * @return {Object} HTMLElement or null if not found
1113 getFirstChildBy: function(node, method) {
1114 var child = ( Y.Dom._testElement(node.firstChild, method) ) ? node.firstChild : null;
1115 return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
1119 * Returns the first HTMLElement child.
1120 * @method getFirstChild
1121 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1122 * @return {Object} HTMLElement or null if not found
1124 getFirstChild: function(node, method) {
1125 node = Y.Dom.get(node);
1127 YAHOO.log('getFirstChild failed: invalid node argument', 'error', 'Dom');
1130 return Y.Dom.getFirstChildBy(node);
1134 * Returns the last HTMLElement child that passes the test method.
1135 * @method getLastChildBy
1136 * @param {HTMLElement} node The HTMLElement to use as the starting point
1137 * @param {Function} method A boolean function used to test children
1138 * that receives the node being tested as its only argument
1139 * @return {Object} HTMLElement or null if not found
1141 getLastChildBy: function(node, method) {
1143 YAHOO.log('getLastChild failed: invalid node argument', 'error', 'Dom');
1146 var child = ( Y.Dom._testElement(node.lastChild, method) ) ? node.lastChild : null;
1147 return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
1151 * Returns the last HTMLElement child.
1152 * @method getLastChild
1153 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1154 * @return {Object} HTMLElement or null if not found
1156 getLastChild: function(node) {
1157 node = Y.Dom.get(node);
1158 return Y.Dom.getLastChildBy(node);
1162 * Returns an array of HTMLElement childNodes that pass the test method.
1163 * @method getChildrenBy
1164 * @param {HTMLElement} node The HTMLElement to start from
1165 * @param {Function} method A boolean function used to test children
1166 * that receives the node being tested as its only argument
1167 * @return {Array} A static array of HTMLElements
1169 getChildrenBy: function(node, method) {
1170 var child = Y.Dom.getFirstChildBy(node, method),
1171 children = child ? [child] : [];
1173 Y.Dom.getNextSiblingBy(child, function(node) {
1174 if ( !method || method(node) ) {
1175 children[children.length] = node;
1177 return false; // fail test to collect all children
1184 * Returns an array of HTMLElement childNodes.
1185 * @method getChildren
1186 * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
1187 * @return {Array} A static array of HTMLElements
1189 getChildren: function(node) {
1190 node = Y.Dom.get(node);
1192 YAHOO.log('getChildren failed: invalid node argument', 'error', 'Dom');
1195 return Y.Dom.getChildrenBy(node);
1199 * Returns the left scroll value of the document
1200 * @method getDocumentScrollLeft
1201 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1202 * @return {Int} The amount that the document is scrolled to the left
1204 getDocumentScrollLeft: function(doc) {
1205 doc = doc || document;
1206 return Math.max(doc[DOCUMENT_ELEMENT].scrollLeft, doc.body.scrollLeft);
1210 * Returns the top scroll value of the document
1211 * @method getDocumentScrollTop
1212 * @param {HTMLDocument} document (optional) The document to get the scroll value of
1213 * @return {Int} The amount that the document is scrolled to the top
1215 getDocumentScrollTop: function(doc) {
1216 doc = doc || document;
1217 return Math.max(doc[DOCUMENT_ELEMENT].scrollTop, doc.body.scrollTop);
1221 * Inserts the new node as the previous sibling of the reference node
1222 * @method insertBefore
1223 * @param {String | HTMLElement} newNode The node to be inserted
1224 * @param {String | HTMLElement} referenceNode The node to insert the new node before
1225 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1227 insertBefore: function(newNode, referenceNode) {
1228 newNode = Y.Dom.get(newNode);
1229 referenceNode = Y.Dom.get(referenceNode);
1231 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1232 YAHOO.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
1236 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode);
1240 * Inserts the new node as the next sibling of the reference node
1241 * @method insertAfter
1242 * @param {String | HTMLElement} newNode The node to be inserted
1243 * @param {String | HTMLElement} referenceNode The node to insert the new node after
1244 * @return {HTMLElement} The node that was inserted (or null if insert fails)
1246 insertAfter: function(newNode, referenceNode) {
1247 newNode = Y.Dom.get(newNode);
1248 referenceNode = Y.Dom.get(referenceNode);
1250 if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
1251 YAHOO.log('insertAfter failed: missing or invalid arg(s)', 'error', 'Dom');
1255 if (referenceNode.nextSibling) {
1256 return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode.nextSibling);
1258 return referenceNode[PARENT_NODE].appendChild(newNode);
1263 * Creates a Region based on the viewport relative to the document.
1264 * @method getClientRegion
1265 * @return {Region} A Region object representing the viewport which accounts for document scroll
1267 getClientRegion: function() {
1268 var t = Y.Dom.getDocumentScrollTop(),
1269 l = Y.Dom.getDocumentScrollLeft(),
1270 r = Y.Dom.getViewportWidth() + l,
1271 b = Y.Dom.getViewportHeight() + t;
1273 return new Y.Region(t, r, b, l);
1277 * Provides a normalized attribute interface.
1278 * @method setAttibute
1279 * @param {String | HTMLElement} el The target element for the attribute.
1280 * @param {String} attr The attribute to set.
1281 * @param {String} val The value of the attribute.
1283 setAttribute: function(el, attr, val) {
1284 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1285 el.setAttribute(attr, val);
1290 * Provides a normalized attribute interface.
1291 * @method getAttibute
1292 * @param {String | HTMLElement} el The target element for the attribute.
1293 * @param {String} attr The attribute to get.
1294 * @return {String} The current value of the attribute.
1296 getAttribute: function(el, attr) {
1297 attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
1298 return el.getAttribute(attr);
1301 _toCamel: function(property) {
1302 var c = propertyCache;
1305 return l.toUpperCase();
1308 return c[property] || (c[property] = property.indexOf('-') === -1 ?
1310 property.replace( /-([a-z])/gi, tU ));
1313 _getClassRegex: function(className) {
1315 if (className !== undefined) { // allow empty string to pass
1316 if (className.exec) { // already a RegExp
1319 re = reCache[className];
1321 // escape special chars (".", "[", etc.)
1322 className = className.replace(Y.Dom._patterns.CLASS_RE_TOKENS, '\\$1');
1323 re = reCache[className] = new RegExp(C_START + className + C_END, G);
1331 ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
1332 CLASS_RE_TOKENS: /([\.\(\)\^\$\*\+\?\|\[\]\{\}])/g
1336 _testElement: function(node, method) {
1337 return node && node[NODE_TYPE] == 1 && ( !method || method(node) );
1340 _calcBorders: function(node, xy2) {
1341 var t = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_TOP_WIDTH), 10) || 0,
1342 l = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_LEFT_WIDTH), 10) || 0;
1344 if (RE_TABLE.test(node[TAG_NAME])) {
1355 var _getComputedStyle = Y.Dom[GET_COMPUTED_STYLE];
1356 // fix opera computedStyle default color unit (convert to rgb)
1358 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1359 var val = _getComputedStyle(node, att);
1360 if (RE_COLOR.test(att)) {
1361 val = Y.Dom.Color.toRGB(val);
1369 // safari converts transparent to rgba(), others use "transparent"
1371 Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
1372 var val = _getComputedStyle(node, att);
1374 if (val === 'rgba(0, 0, 0, 0)') {
1375 val = 'transparent';
1384 * A region is a representation of an object on a grid. It is defined
1385 * by the top, right, bottom, left extents, so is rectangular by default. If
1386 * other shapes are required, this class could be extended to support it.
1387 * @namespace YAHOO.util
1389 * @param {Int} t the top extent
1390 * @param {Int} r the right extent
1391 * @param {Int} b the bottom extent
1392 * @param {Int} l the left extent
1395 YAHOO.util.Region = function(t, r, b, l) {
1398 * The region's top extent
1405 * The region's top extent
1412 * The region's top extent as index, for symmetry with set/getXY
1419 * The region's right extent
1426 * The region's bottom extent
1433 * The region's left extent
1440 * The region's left extent
1447 * The region's left extent as index, for symmetry with set/getXY
1454 * The region's total width
1458 this.width = this.right - this.left;
1461 * The region's total height
1465 this.height = this.bottom - this.top;
1469 * Returns true if this region contains the region passed in
1471 * @param {Region} region The region to evaluate
1472 * @return {Boolean} True if the region is contained with this region,
1475 YAHOO.util.Region.prototype.contains = function(region) {
1476 return ( region.left >= this.left &&
1477 region.right <= this.right &&
1478 region.top >= this.top &&
1479 region.bottom <= this.bottom );
1481 // this.logger.debug("does " + this + " contain " + region + " ... " + ret);
1485 * Returns the area of the region
1487 * @return {Int} the region's area
1489 YAHOO.util.Region.prototype.getArea = function() {
1490 return ( (this.bottom - this.top) * (this.right - this.left) );
1494 * Returns the region where the passed in region overlaps with this one
1496 * @param {Region} region The region that intersects
1497 * @return {Region} The overlap region, or null if there is no overlap
1499 YAHOO.util.Region.prototype.intersect = function(region) {
1500 var t = Math.max( this.top, region.top ),
1501 r = Math.min( this.right, region.right ),
1502 b = Math.min( this.bottom, region.bottom ),
1503 l = Math.max( this.left, region.left );
1505 if (b >= t && r >= l) {
1506 return new YAHOO.util.Region(t, r, b, l);
1513 * Returns the region representing the smallest region that can contain both
1514 * the passed in region and this region.
1516 * @param {Region} region The region that to create the union with
1517 * @return {Region} The union region
1519 YAHOO.util.Region.prototype.union = function(region) {
1520 var t = Math.min( this.top, region.top ),
1521 r = Math.max( this.right, region.right ),
1522 b = Math.max( this.bottom, region.bottom ),
1523 l = Math.min( this.left, region.left );
1525 return new YAHOO.util.Region(t, r, b, l);
1531 * @return string the region properties
1533 YAHOO.util.Region.prototype.toString = function() {
1534 return ( "Region {" +
1535 "top: " + this.top +
1536 ", right: " + this.right +
1537 ", bottom: " + this.bottom +
1538 ", left: " + this.left +
1539 ", height: " + this.height +
1540 ", width: " + this.width +
1545 * Returns a region that is occupied by the DOM element
1547 * @param {HTMLElement} el The element
1548 * @return {Region} The region that the element occupies
1551 YAHOO.util.Region.getRegion = function(el) {
1552 var p = YAHOO.util.Dom.getXY(el),
1554 r = p[0] + el.offsetWidth,
1555 b = p[1] + el.offsetHeight,
1558 return new YAHOO.util.Region(t, r, b, l);
1561 /////////////////////////////////////////////////////////////////////////////
1565 * A point is a region that is special in that it represents a single point on
1567 * @namespace YAHOO.util
1569 * @param {Int} x The X position of the point
1570 * @param {Int} y The Y position of the point
1572 * @extends YAHOO.util.Region
1574 YAHOO.util.Point = function(x, y) {
1575 if (YAHOO.lang.isArray(x)) { // accept input from Dom.getXY, Event.getXY, etc.
1576 y = x[1]; // dont blow away x yet
1580 YAHOO.util.Point.superclass.constructor.call(this, y, x, y, x);
1583 YAHOO.extend(YAHOO.util.Point, YAHOO.util.Region);
1587 * Add style management functionality to DOM.
1593 CLIENT_TOP = 'clientTop',
1594 CLIENT_LEFT = 'clientLeft',
1595 PARENT_NODE = 'parentNode',
1597 HAS_LAYOUT = 'hasLayout',
1599 OPACITY = 'opacity',
1601 BORDER_LEFT_WIDTH = 'borderLeftWidth',
1602 BORDER_TOP_WIDTH = 'borderTopWidth',
1603 BORDER_RIGHT_WIDTH = 'borderRightWidth',
1604 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
1605 VISIBLE = 'visible',
1606 TRANSPARENT = 'transparent',
1610 CURRENT_STYLE = 'currentStyle',
1612 // IE getComputedStyle
1613 // TODO: unit-less lineHeight (e.g. 1.22)
1614 re_size = /^width|height$/,
1615 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,
1618 get: function(el, property) {
1620 current = el[CURRENT_STYLE][property];
1622 if (property === OPACITY) {
1623 value = Y.Dom.getStyle(el, OPACITY);
1624 } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
1626 } else if (Y.Dom.IE_COMPUTED[property]) { // use compute function
1627 value = Y.Dom.IE_COMPUTED[property](el, property);
1628 } else if (re_unit.test(current)) { // convert to pixel
1629 value = Y.Dom.IE.ComputedStyle.getPixel(el, property);
1637 getOffset: function(el, prop) {
1638 var current = el[CURRENT_STYLE][prop], // value of "width", "top", etc.
1639 capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
1640 offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
1641 pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
1645 if (current == AUTO) {
1646 actual = el[offset]; // offsetHeight/Top etc.
1647 if (actual === undefined) { // likely "right" or "bottom"
1652 if (re_size.test(prop)) { // account for box model diff
1653 el[STYLE][prop] = actual;
1654 if (el[offset] > actual) {
1655 // the difference is padding + border (works in Standards & Quirks modes)
1656 value = actual - (el[offset] - actual);
1658 el[STYLE][prop] = AUTO; // revert to auto
1660 } else { // convert units to px
1661 if (!el[STYLE][pixel] && !el[STYLE][prop]) { // need to map style.width to currentStyle (no currentStyle.pixelWidth)
1662 el[STYLE][prop] = current; // no style.pixelWidth if no style.width
1664 value = el[STYLE][pixel];
1669 getBorderWidth: function(el, property) {
1670 // clientHeight/Width = paddingBox (e.g. offsetWidth - borderWidth)
1671 // clientTop/Left = borderWidth
1673 if (!el[CURRENT_STYLE][HAS_LAYOUT]) { // TODO: unset layout?
1674 el[STYLE].zoom = 1; // need layout to measure client
1678 case BORDER_TOP_WIDTH:
1679 value = el[CLIENT_TOP];
1681 case BORDER_BOTTOM_WIDTH:
1682 value = el.offsetHeight - el.clientHeight - el[CLIENT_TOP];
1684 case BORDER_LEFT_WIDTH:
1685 value = el[CLIENT_LEFT];
1687 case BORDER_RIGHT_WIDTH:
1688 value = el.offsetWidth - el.clientWidth - el[CLIENT_LEFT];
1694 getPixel: function(node, att) {
1695 // use pixelRight to convert to px
1697 styleRight = node[CURRENT_STYLE][RIGHT],
1698 current = node[CURRENT_STYLE][att];
1700 node[STYLE][RIGHT] = current;
1701 val = node[STYLE].pixelRight;
1702 node[STYLE][RIGHT] = styleRight; // revert
1707 getMargin: function(node, att) {
1709 if (node[CURRENT_STYLE][att] == AUTO) {
1712 val = Y.Dom.IE.ComputedStyle.getPixel(node, att);
1717 getVisibility: function(node, att) {
1719 while ( (current = node[CURRENT_STYLE]) && current[att] == 'inherit') { // NOTE: assignment in test
1720 node = node[PARENT_NODE];
1722 return (current) ? current[att] : VISIBLE;
1725 getColor: function(node, att) {
1726 return Y.Dom.Color.toRGB(node[CURRENT_STYLE][att]) || TRANSPARENT;
1729 getBorderColor: function(node, att) {
1730 var current = node[CURRENT_STYLE],
1731 val = current[att] || current.color;
1732 return Y.Dom.Color.toRGB(Y.Dom.Color.toHex(val));
1737 //fontSize: getPixelFont,
1740 IEComputed.top = IEComputed.right = IEComputed.bottom = IEComputed.left =
1741 IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
1743 IEComputed.color = ComputedStyle.getColor;
1745 IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
1746 IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
1747 ComputedStyle.getBorderWidth;
1749 IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
1750 IEComputed.marginLeft = ComputedStyle.getMargin;
1752 IEComputed.visibility = ComputedStyle.getVisibility;
1753 IEComputed.borderColor = IEComputed.borderTopColor =
1754 IEComputed.borderRightColor = IEComputed.borderBottomColor =
1755 IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
1757 Y.Dom.IE_COMPUTED = IEComputed;
1758 Y.Dom.IE_ComputedStyle = ComputedStyle;
1762 * Add style management functionality to DOM.
1767 var TO_STRING = 'toString',
1768 PARSE_INT = parseInt,
1792 re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
1793 re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
1794 re_hex3: /([0-9A-F])/gi,
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)
1811 toHex: function(val) {
1812 val = Y.Dom.Color.KEYWORDS[val] || val;
1813 if (Y.Dom.Color.re_RGB.exec(val)) {
1814 var r = (RE.$1.length === 1) ? '0' + RE.$1 : Number(RE.$1),
1815 g = (RE.$2.length === 1) ? '0' + RE.$2 : Number(RE.$2),
1816 b = (RE.$3.length === 1) ? '0' + RE.$3 : Number(RE.$3);
1825 if (val.length < 6) {
1826 val = val.replace(Y.Dom.Color.re_hex3, '$1$1');
1829 if (val !== 'transparent' && val.indexOf('#') < 0) {
1833 return val.toLowerCase();
1837 YAHOO.register("dom", YAHOO.util.Dom, {version: "2.7.0", build: "1799"});