dom-base-debug.js revision 931e86d9347e296b2faa2e896a679f787f0edba6
998276643802ff9fb197fe220cbd9552da00a624Luke SmithYUI.add('dom-base', function(Y) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith(function(Y) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith/**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * The DOM utility provides a cross-browser abtraction layer
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * normalizing DOM tasks, and adds extra helper functionality
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * for other common tasks.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @module dom
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @submodule dom-base
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @for DOM
998276643802ff9fb197fe220cbd9552da00a624Luke Smith *
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith/**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Provides DOM helper methods.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @class DOM
998276643802ff9fb197fe220cbd9552da00a624Luke Smith *
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smithvar NODE_TYPE = 'nodeType',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith OWNER_DOCUMENT = 'ownerDocument',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith DOCUMENT_ELEMENT = 'documentElement',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith DEFAULT_VIEW = 'defaultView',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith PARENT_WINDOW = 'parentWindow',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith TAG_NAME = 'tagName',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith PARENT_NODE = 'parentNode',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith FIRST_CHILD = 'firstChild',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith PREVIOUS_SIBLING = 'previousSibling',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith NEXT_SIBLING = 'nextSibling',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith CONTAINS = 'contains',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith COMPARE_DOCUMENT_POSITION = 'compareDocumentPosition',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith EMPTY_STRING = '',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith documentElement = Y.config.doc.documentElement,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith re_tag = /<([a-z]+)/i;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke SmithY.DOM = {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Returns the HTMLElement with the given ID (Wrapper for document.getElementById).
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method byId
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String} id the id attribute
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Object} doc optional The document to search. Defaults to current document
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {HTMLElement | null} The HTMLElement with the id, or null if none found.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith byId: function(id, doc) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // handle dupe IDs and IE name collision
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return Y.DOM.allById(id, doc)[0] || null;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // @deprecated
998276643802ff9fb197fe220cbd9552da00a624Luke Smith children: function(node, tag) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = [];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (node) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith tag = tag || '*';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = Y.Selector.query('> ' + tag, node);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret;
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith },
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith // @deprecated
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith firstByTag: function(tag, root) {
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith var ret;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith root = root || Y.config.doc;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (tag && root.getElementsByTagName) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = root.getElementsByTagName(tag)[0];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret || null;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Returns the text content of the HTMLElement.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method getText
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {String} The text content of the element (includes text of any descending elements).
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith getText: (documentElement.textContent !== undefined) ?
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function(element) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = '';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (element) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = element.textContent;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret || '';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } : function(element) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = '';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (element) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = element.innerText;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret || '';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Sets the text content of the HTMLElement.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method setText
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String} content The content to add.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith setText: (documentElement.textContent !== undefined) ?
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function(element, content) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (element) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith element.textContent = content;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } : function(element, content) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (element) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith element.innerText = content;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /*
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Finds the previous sibling of the element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method previous
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @deprecated Use elementByAxis
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Function} fn optional An optional boolean test to apply.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * The optional function is passed the current DOM node being tested as its only argument.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * If no function is given, the first sibling is returned.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Boolean} all optional Whether all node types should be scanned, or just element nodes.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {HTMLElement | null} The matching DOM node or null if none found.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith previous: function(element, fn, all) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return Y.DOM.elementByAxis(element, PREVIOUS_SIBLING, fn, all);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /*
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Finds the next sibling of the element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method next
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @deprecated Use elementByAxis
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Function} fn optional An optional boolean test to apply.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * The optional function is passed the current DOM node being tested as its only argument.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * If no function is given, the first sibling is returned.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Boolean} all optional Whether all node types should be scanned, or just element nodes.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {HTMLElement | null} The matching DOM node or null if none found.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith next: function(element, fn, all) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return Y.DOM.elementByAxis(element, NEXT_SIBLING, fn, all);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /*
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Finds the ancestor of the element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method ancestor
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @deprecated Use elementByAxis
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Function} fn optional An optional boolean test to apply.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * The optional function is passed the current DOM node being tested as its only argument.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * If no function is given, the parentNode is returned.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Boolean} testSelf optional Whether or not to include the element in the scan
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {HTMLElement | null} The matching DOM node or null if none found.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ancestor: function(element, fn, testSelf) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = null;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (testSelf) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = (!fn || fn(element)) ? element : null;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret || Y.DOM.elementByAxis(element, PARENT_NODE, fn, null);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Searches the element by the given axis for the first matching element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method elementByAxis
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String} axis The axis to search (parentNode, nextSibling, previousSibling).
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Function} fn optional An optional boolean test to apply.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {Boolean} all optional Whether all node types should be returned, or just element nodes.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * The optional function is passed the current HTMLElement being tested as its only argument.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * If no function is given, the first element is returned.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {HTMLElement | null} The matching element or null if none found.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith elementByAxis: function(element, axis, fn, all) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith while (element && (element = element[axis])) { // NOTE: assignment
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if ( (all || element[TAG_NAME]) && (!fn || fn(element)) ) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return element;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return null;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Determines whether or not one HTMLElement is or contains another HTMLElement.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method contains
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The containing html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} needle The html element that may be contained.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {Boolean} Whether or not the element is or contains the needle.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith contains: function(element, needle) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = false;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if ( !needle || !element || !needle[NODE_TYPE] || !element[NODE_TYPE]) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = false;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (element[CONTAINS]) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (Y.UA.opera || needle[NODE_TYPE] === 1) { // IE & SAF contains fail if needle not an ELEMENT_NODE
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = element[CONTAINS](needle);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = Y.DOM._bruteContains(element, needle);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (element[COMPARE_DOCUMENT_POSITION]) { // gecko
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (element === needle || !!(element[COMPARE_DOCUMENT_POSITION](needle) & 16)) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = true;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Determines whether or not the HTMLElement is part of the document.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method inDoc
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} element The containing html element.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} doc optional The document to check.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {Boolean} Whether or not the element is attached to the document.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith inDoc: function(element, doc) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = false,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith rootNode;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (element && element.nodeType) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith (doc) || (doc = element[OWNER_DOCUMENT]);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith rootNode = doc[DOCUMENT_ELEMENT];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // contains only works with HTML_ELEMENT
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (rootNode && rootNode.contains && element.tagName) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = rootNode.contains(element);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = Y.DOM.contains(rootNode, element);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith allById: function(id, root) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith root = root || Y.config.doc;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var nodes = [],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = [],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith i,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith node;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (root.querySelectorAll) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = root.querySelectorAll('[id="' + id + '"]');
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (root.all) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodes = root.all(id);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (nodes && nodes.nodeType) { // root.all may return one or many
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodes = [nodes];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (nodes && nodes.length) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith for (i = 0; node = nodes[i++];) { // check for a match
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (node.attributes && node.attributes.id
998276643802ff9fb197fe220cbd9552da00a624Luke Smith && node.attributes.id.value === id) { // avoid false positive for node.name & form.id
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret.push(node);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = [Y.DOM._getDoc(root).getElementById(id)];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Creates a new dom node using the provided markup string.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method create
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String} html The markup used to create the element
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLDocument} doc An optional document context
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {HTMLElement|DocumentFragment} returns a single HTMLElement
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * when creating one node, and a documentFragment when creating
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * multiple nodes.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith create: function(html, doc) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (typeof html === 'string') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith html = Y.Lang.trim(html); // match IE which trims whitespace from innerHTML
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith doc = doc || Y.config.doc;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var m = re_tag.exec(html),
998276643802ff9fb197fe220cbd9552da00a624Luke Smith create = Y.DOM._create,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith custom = Y.DOM.creators,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = null,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith tag, nodes;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (html != undefined) { // not undefined or null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (m && custom[m[1]]) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (typeof custom[m[1]] === 'function') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith create = custom[m[1]];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith tag = custom[m[1]];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodes = create(html, doc, tag).childNodes;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (nodes.length === 1) { // return single node, breaking parentNode ref from "fragment"
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = nodes[0].parentNode.removeChild(nodes[0]);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (nodes[0] && nodes[0].className === 'yui3-big-dummy') { // using dummy node to preserve some attributes (e.g. OPTION not selected)
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (nodes.length === 2) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = nodes[0].nextSibling;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodes[0].parentNode.removeChild(nodes[0]);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = Y.DOM._nl2frag(nodes, doc);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else { // return multiple nodes as a fragment
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = Y.DOM._nl2frag(nodes, doc);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith _nl2frag: function(nodes, doc) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = null,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith i, len;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (nodes && (nodes.push || nodes.item) && nodes[0]) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith doc = doc || nodes[0].ownerDocument;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = doc.createDocumentFragment();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (nodes.item) { // convert live list to static array
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodes = Y.Array(nodes, 0, true);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith for (i = 0, len = nodes.length; i < len; i++) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret.appendChild(nodes[i]);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } // else inline with log for minification
998276643802ff9fb197fe220cbd9552da00a624Luke Smith else { Y.log('unable to convert ' + nodes + ' to fragment', 'warn', 'dom'); }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 'for': 'htmlFor',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 'class': 'className'
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } : { // w3c
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 'htmlFor': 'for',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 'className': 'class'
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Provides a normalized attribute interface.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method setAttibute
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String | HTMLElement} el The target element for the attribute.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String} attr The attribute to set.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String} val The value of the attribute.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith setAttribute: function(el, attr, val, ieAttr) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (el && el.setAttribute) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith attr = Y.DOM.CUSTOM_ATTRIBUTES[attr] || attr;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith el.setAttribute(attr, val, ieAttr);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Provides a normalized attribute interface.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method getAttibute
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String | HTMLElement} el The target element for the attribute.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String} attr The attribute to get.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @return {String} The current value of the attribute.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith getAttribute: function(el, attr, ieAttr) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = '';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (el && el.getAttribute) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith attr = Y.DOM.CUSTOM_ATTRIBUTES[attr] || attr;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = el.getAttribute(attr, ieAttr);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (ret === null) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = ''; // per DOM spec
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return ret;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith isWindow: function(obj) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return obj.alert && obj.document;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith _fragClones: {},
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith _create: function(html, doc, tag) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith tag = tag || 'div';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var frag = Y.DOM._fragClones[tag];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (frag) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith frag = frag.cloneNode(false);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith frag = Y.DOM._fragClones[tag] = doc.createElement(tag);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith frag.innerHTML = html;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return frag;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith _removeChildNodes: function(node) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith while (node.firstChild) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith node.removeChild(node.firstChild);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /**
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Inserts content in a node at the given location
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @method addHTML
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {HTMLElement} node The node to insert into
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String | HTMLElement} content The content to be inserted
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * @param {String | HTMLElement} where Where to insert the content
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * If no "where" is given, content is appended to the node
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Possible values for "where"
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dl>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dt>HTMLElement</dt>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dd>The element to insert before</dd>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dt>"replace"</dt>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dd>Replaces the existing HTML</dd>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dt>"before"</dt>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dd>Inserts before the existing HTML</dd>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dt>"before"</dt>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dd>Inserts content before the node</dd>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dt>"after"</dt>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * <dd>Inserts content after the node</dd>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * </dl>
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith addHTML: function(node, content, where) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var nodeParent = node.parentNode,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith newNode;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (content !== undefined && content !== null) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (content.nodeType) { // domNode
998276643802ff9fb197fe220cbd9552da00a624Luke Smith newNode = content;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else { // create from string and cache
998276643802ff9fb197fe220cbd9552da00a624Luke Smith newNode = Y.DOM.create(content);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (where) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (where.nodeType) { // insert regardless of relationship to node
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // TODO: check if node.contains(where)?
998276643802ff9fb197fe220cbd9552da00a624Luke Smith where.parentNode.insertBefore(newNode, where);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith switch (where) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith case 'replace':
998276643802ff9fb197fe220cbd9552da00a624Luke Smith while (node.firstChild) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith node.removeChild(node.firstChild);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (newNode) { // allow empty content to clear node
998276643802ff9fb197fe220cbd9552da00a624Luke Smith node.appendChild(newNode);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith break;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith case 'before':
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodeParent.insertBefore(newNode, node);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith break;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith case 'after':
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (node.nextSibling) { // IE errors if refNode is null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodeParent.insertBefore(newNode, node.nextSibling);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nodeParent.appendChild(newNode);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith break;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith default:
998276643802ff9fb197fe220cbd9552da00a624Luke Smith node.appendChild(newNode);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith node.appendChild(newNode);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return newNode;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith VALUE_SETTERS: {},
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith VALUE_GETTERS: {},
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith getValue: function(node) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var ret = '', // TODO: return null?
998276643802ff9fb197fe220cbd9552da00a624Luke Smith getter;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (node && node[TAG_NAME]) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith getter = Y.DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (getter) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = getter(node);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = node.value;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // workaround for IE8 JSON stringify bug
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // which converts empty string values to null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (ret === EMPTY_STRING) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ret = EMPTY_STRING; // for real
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return (typeof ret === 'string') ? ret : '';
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith setValue: function(node, val) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var setter;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (node && node[TAG_NAME]) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith setter = Y.DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (setter) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith setter(node, val);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith node.value = val;
}
}
},
siblings: function(node, fn) {
var nodes = [],
sibling = node;
while ((sibling = sibling[PREVIOUS_SIBLING])) {
if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
nodes.unshift(sibling);
}
}
sibling = node;
while ((sibling = sibling[NEXT_SIBLING])) {
if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
nodes.push(sibling);
}
}
return nodes;
},
/**
* Brute force version of contains.
* Used for browsers without contains support for non-HTMLElement Nodes (textNodes, etc).
* @method _bruteContains
* @private
* @param {HTMLElement} element The containing html element.
* @param {HTMLElement} needle The html element that may be contained.
* @return {Boolean} Whether or not the element is or contains the needle.
*/
_bruteContains: function(element, needle) {
while (needle) {
if (element === needle) {
return true;
}
needle = needle.parentNode;
}
return false;
},
// TODO: move to Lang?
/**
* Memoizes dynamic regular expressions to boost runtime performance.
* @method _getRegExp
* @private
* @param {String} str The string to convert to a regular expression.
* @param {String} flags optional An optinal string of flags.
* @return {RegExp} An instance of RegExp
*/
_getRegExp: function(str, flags) {
flags = flags || '';
Y.DOM._regexCache = Y.DOM._regexCache || {};
if (!Y.DOM._regexCache[str + flags]) {
Y.DOM._regexCache[str + flags] = new RegExp(str, flags);
}
return Y.DOM._regexCache[str + flags];
},
// TODO: make getDoc/Win true privates?
/**
* returns the appropriate document.
* @method _getDoc
* @private
* @param {HTMLElement} element optional Target element.
* @return {Object} The document for the given element or the default document.
*/
_getDoc: function(element) {
var doc = Y.config.doc;
if (element) {
doc = (element[NODE_TYPE] === 9) ? element : // element === document
element[OWNER_DOCUMENT] || // element === DOM node
element.document || // element === window
Y.config.doc; // default
}
return doc;
},
/**
* returns the appropriate window.
* @method _getWin
* @private
* @param {HTMLElement} element optional Target element.
* @return {Object} The window for the given element or the default window.
*/
_getWin: function(element) {
var doc = Y.DOM._getDoc(element);
return doc[DEFAULT_VIEW] || doc[PARENT_WINDOW] || Y.config.win;
},
_batch: function(nodes, fn, arg1, arg2, arg3, etc) {
fn = (typeof name === 'string') ? Y.DOM[fn] : fn;
var result,
ret = [];
if (fn && nodes) {
Y.each(nodes, function(node) {
if ((result = fn.call(Y.DOM, node, arg1, arg2, arg3, etc)) !== undefined) {
ret[ret.length] = result;
}
});
}
return ret.length ? ret : nodes;
},
creators: {},
_IESimpleCreate: function(html, doc) {
doc = doc || Y.config.doc;
return doc.createElement(html);
}
};
(function(Y) {
var creators = Y.DOM.creators,
create = Y.DOM.create,
re_tbody = /(?:\/(?:thead|tfoot|tbody|caption|col|colgroup)>)+\s*<tbody/,
TABLE_OPEN = '<table>',
TABLE_CLOSE = '</table>';
if (Y.UA.ie) {
Y.mix(creators, {
// TODO: thead/tfoot with nested tbody
// IE adds TBODY when creating TABLE elements (which may share this impl)
tbody: function(html, doc) {
var frag = create(TABLE_OPEN + html + TABLE_CLOSE, doc),
tb = frag.children.tags('tbody')[0];
if (frag.children.length > 1 && tb && !re_tbody.test(html)) {
tb[PARENT_NODE].removeChild(tb); // strip extraneous tbody
}
return frag;
},
script: function(html, doc) {
var frag = doc.createElement('div');
frag.innerHTML = '-' + html;
frag.removeChild(frag[FIRST_CHILD]);
return frag;
}
}, true);
Y.mix(Y.DOM.VALUE_GETTERS, {
button: function(node) {
return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
}
});
Y.mix(Y.DOM.VALUE_SETTERS, {
// IE: node.value changes the button text, which should be handled via innerHTML
button: function(node, val) {
var attr = node.attributes.value;
if (!attr) {
attr = node[OWNER_DOCUMENT].createAttribute('value');
node.setAttributeNode(attr);
}
attr.value = val;
},
select: function(node, val) {
for (var i = 0, options = node.getElementsByTagName('option'), option;
option = options[i++];) {
if (Y.DOM.getValue(option) === val) {
Y.DOM.setAttribute(option, 'selected', true);
break;
}
}
}
});
Y.DOM.creators.link = Y.DOM.creators.style = Y.DOM.creators.script;
}
if (Y.UA.gecko || Y.UA.ie) {
Y.mix(creators, {
option: function(html, doc) {
return create('<select><option class="yui3-big-dummy" selected></option>' + html + '</select>', doc);
},
tr: function(html, doc) {
return create('<tbody>' + html + '</tbody>', doc);
},
td: function(html, doc) {
return create('<tr>' + html + '</tr>', doc);
},
tbody: function(html, doc) {
return create(TABLE_OPEN + html + TABLE_CLOSE, doc);
}
});
Y.mix(creators, {
legend: 'fieldset',
th: creators.td,
thead: creators.tbody,
tfoot: creators.tbody,
caption: creators.tbody,
colgroup: creators.tbody,
col: creators.tbody,
optgroup: creators.option
});
}
Y.mix(Y.DOM.VALUE_GETTERS, {
option: function(node) {
var attrs = node.attributes;
return (attrs.value && attrs.value.specified) ? node.value : node.text;
},
select: function(node) {
var val = node.value,
options = node.options;
if (options && options.length && val === '') {
// TODO: implement multipe select
if (node.multiple) {
Y.log('multiple select normalization not implemented', 'warn', 'DOM');
} else {
val = Y.DOM.getValue(options[node.selectedIndex]);
}
}
return val;
}
});
})(Y);
})(Y);
var addClass, hasClass, removeClass;
Y.mix(Y.DOM, {
/**
* Determines whether a DOM element has the given className.
* @method hasClass
* @for DOM
* @param {HTMLElement} element The DOM element.
* @param {String} className the class name to search for
* @return {Boolean} Whether or not the element has the given class.
*/
hasClass: function(node, className) {
var re = Y.DOM._getRegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
return re.test(node.className);
},
/**
* Adds a class name to a given DOM element.
* @method addClass
* @for DOM
* @param {HTMLElement} element The DOM element.
* @param {String} className the class name to add to the class attribute
*/
addClass: function(node, className) {
if (!Y.DOM.hasClass(node, className)) { // skip if already present
node.className = Y.Lang.trim([node.className, className].join(' '));
}
},
/**
* Removes a class name from a given element.
* @method removeClass
* @for DOM
* @param {HTMLElement} element The DOM element.
* @param {String} className the class name to remove from the class attribute
*/
removeClass: function(node, className) {
if (className && hasClass(node, className)) {
node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
className + '(?:\\s+|$)'), ' '));
if ( hasClass(node, className) ) { // in case of multiple adjacent
removeClass(node, className);
}
}
},
/**
* Replace a class with another class for a given element.
* If no oldClassName is present, the newClassName is simply added.
* @method replaceClass
* @for DOM
* @param {HTMLElement} element The DOM element
* @param {String} oldClassName the class name to be replaced
* @param {String} newClassName the class name that will be replacing the old class name
*/
replaceClass: function(node, oldC, newC) {
//Y.log('replaceClass replacing ' + oldC + ' with ' + newC, 'info', 'Node');
removeClass(node, oldC); // remove first in case oldC === newC
addClass(node, newC);
},
/**
* If the className exists on the node it is removed, if it doesn't exist it is added.
* @method toggleClass
* @for DOM
* @param {HTMLElement} element The DOM element
* @param {String} className the class name to be toggled
* @param {Boolean} addClass optional boolean to indicate whether class
* should be added or removed regardless of current state
*/
toggleClass: function(node, className, force) {
var add = (force !== undefined) ? force :
!(hasClass(node, className));
if (add) {
addClass(node, className);
} else {
removeClass(node, className);
}
}
});
hasClass = Y.DOM.hasClass;
removeClass = Y.DOM.removeClass;
addClass = Y.DOM.addClass;
Y.mix(Y.DOM, {
/**
* Sets the width of the element to the given size, regardless
* of box model, border, padding, etc.
* @method setWidth
* @param {HTMLElement} element The DOM element.
* @param {String|Int} size The pixel height to size to
*/
setWidth: function(node, size) {
Y.DOM._setSize(node, 'width', size);
},
/**
* Sets the height of the element to the given size, regardless
* of box model, border, padding, etc.
* @method setHeight
* @param {HTMLElement} element The DOM element.
* @param {String|Int} size The pixel height to size to
*/
setHeight: function(node, size) {
Y.DOM._setSize(node, 'height', size);
},
_setSize: function(node, prop, val) {
val = (val > 0) ? val : 0;
var size = 0;
node.style[prop] = val + 'px';
size = (prop === 'height') ? node.offsetHeight : node.offsetWidth;
if (size > val) {
val = val - (size - val);
if (val < 0) {
val = 0;
}
node.style[prop] = val + 'px';
}
}
});
}, '@VERSION@' ,{requires:['oop']});