dom.js revision 8b86a0d734324d75110ac5d262f6d06d48baf899
/**
* The DOM utility provides a cross-browser abtraction layer
* normalizing DOM tasks, and adds extra helper functionality
* for other common tasks.
* @module dom
* @submodule dom-base
*
*/
/**
* Provides DOM helper methods.
* @class DOM
*
*/
var NODE_TYPE = 'nodeType',
OWNER_DOCUMENT = 'ownerDocument',
DOCUMENT_ELEMENT = 'documentElement',
DEFAULT_VIEW = 'defaultView',
PARENT_WINDOW = 'parentWindow',
TAG_NAME = 'tagName',
PARENT_NODE = 'parentNode',
FIRST_CHILD = 'firstChild',
LAST_CHILD = 'lastChild',
PREVIOUS_SIBLING = 'previousSibling',
NEXT_SIBLING = 'nextSibling',
CONTAINS = 'contains',
COMPARE_DOCUMENT_POSITION = 'compareDocumentPosition',
INNER_TEXT = 'innerText',
TEXT_CONTENT = 'textContent',
LENGTH = 'length',
re_tag = /<([a-z]+)/i;
Y.DOM = {
/**
* Returns the HTMLElement with the given ID (Wrapper for document.getElementById).
* @method byId
* @param {String} id the id attribute
* @param {Object} doc optional The document to search. Defaults to current document
* @return {HTMLElement | null} The HTMLElement with the id, or null if none found.
*/
// TODO: IE Name
},
/**
* Returns the text content of the HTMLElement.
* @method getText
* @param {HTMLElement} element The html element.
* @return {String} The text content of the element (includes text of any descending elements).
*/
}
return text || '';
},
// TODO: pull out sugar (rely on _childBy, byAxis, etc)?
/**
* Finds the firstChild of the given HTMLElement.
* @method firstChild
* @param {HTMLElement} element The html element.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current HTMLElement being tested as its only argument.
* If no function is given, the first found is returned.
* @return {HTMLElement | null} The first matching child html element.
*/
},
},
/**
* Finds the lastChild of the given HTMLElement.
* @method lastChild
* @param {HTMLElement} element The html element.
* @param {String} tag The tag to search for.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current HTMLElement being tested as its only argument.
* If no function is given, the first found is returned.
* @return {HTMLElement | null} The first matching child html element.
*/
},
},
/*
* Finds all HTMLElement childNodes matching the given tag.
* @method childrenByTag
* @param {HTMLElement} element The html element.
* @param {String} tag The tag to search for.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current HTMLElement being tested as its only argument.
* If no function is given, all children with the given tag are collected.
* @return {Array} The collection of child elements.
* TODO: deprecate? Webkit children.tags() returns grandchildren
*/
_childrenByTag: function() {
var elements = [],
if (element) {
} else {
if (tag) {
}
}
}
}
return elements;
};
} else {
var elements = [],
if (element) {
if (tag) { // wrap fn and add tag test TODO: allow tag in filterElementsBy?
};
}
}
return elements;
};
}
}(),
/**
* Finds all HTMLElement childNodes.
* @method children
* @param {HTMLElement} element The html element.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current HTMLElement being tested as its only argument.
* If no function is given, all children are collected.
* @return {Array} The collection of child elements.
*/
},
/**
* Finds the previous sibling of the element.
* @method previous
* @param {HTMLElement} element The html element.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current DOM node being tested as its only argument.
* If no function is given, the first sibling is returned.
* @param {Boolean} all optional Whether all node types should be scanned, or just element nodes.
* @return {HTMLElement | null} The matching DOM node or null if none found.
*/
},
/**
* Finds the next sibling of the element.
* @method next
* @param {HTMLElement} element The html element.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current DOM node being tested as its only argument.
* If no function is given, the first sibling is returned.
* @param {Boolean} all optional Whether all node types should be scanned, or just element nodes.
* @return {HTMLElement | null} The matching DOM node or null if none found.
*/
},
/**
* Finds the ancestor of the element.
* @method ancestor
* @param {HTMLElement} element The html element.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current DOM node being tested as its only argument.
* If no function is given, the parentNode is returned.
* @param {Boolean} all optional Whether all node types should be scanned, or just element nodes.
* @return {HTMLElement | null} The matching DOM node or null if none found.
*/
// TODO: optional stopAt node?
},
/**
* Searches the element by the given axis for the first matching element.
* @method elementByAxis
* @param {HTMLElement} element The html element.
* @param {String} axis The axis to search (parentNode, nextSibling, previousSibling).
* @param {Function} fn optional An optional boolean test to apply.
* @param {Boolean} all optional Whether all node types should be returned, or just element nodes.
* The optional function is passed the current HTMLElement being tested as its only argument.
* If no function is given, the first element is returned.
* @return {HTMLElement | null} The matching element or null if none found.
*/
return element;
}
}
return null;
},
/**
* Finds all elements with the given tag.
* @method byTag
* @param {String} tag The tag being search for.
* @param {HTMLElement} root optional An optional root element to start from.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current HTMLElement being tested as its only argument.
* If no function is given, all elements with the given tag are returned.
* @return {Array} The collection of matching elements.
*/
retNodes = [];
}
}
return retNodes;
},
/**
* Finds the first element with the given tag.
* @method firstByTag
* @param {String} tag The tag being search for.
* @param {HTMLElement} root optional An optional root element to start from.
* @param {Function} fn optional An optional boolean test to apply.
* The optional function is passed the current HTMLElement being tested as its only argument.
* If no function is given, the first match is returned.
* @return {HTMLElement} The matching element.
*/
ret = null;
break;
}
}
return ret;
},
/**
* Filters a collection of HTMLElements by the given attributes.
* @method filterElementsBy
* @param {Array} elements The collection of HTMLElements to filter.
* @param {Function} fn A boolean test to apply.
* The function is passed the current HTMLElement being tested as its only argument.
* If no function is given, all HTMLElements are kept.
* @return {Array} The filtered collection of HTMLElements.
*/
if (firstOnly) {
break;
} else {
}
}
}
return ret;
},
/**
* Determines whether or not one HTMLElement is or contains another HTMLElement.
* @method contains
* @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.
*/
var ret = false;
ret = false;
if (Y.UA.opera || needle[NODE_TYPE] === 1) { // IE & SAF contains fail if needle not an ELEMENT_NODE
} else {
}
ret = true;
}
}
return ret;
},
/**
* Determines whether or not the HTMLElement is part of the document.
* @method inDoc
* @param {HTMLElement} element The containing html element.
* @param {HTMLElement} doc optional The document to check.
* @return {Boolean} Whether or not the element is attached to the document.
*/
},
/**
* Inserts the new node as the previous sibling of the reference node
* @method insertBefore
* @param {String | HTMLElement} newNode The node to be inserted
* @param {String | HTMLElement} referenceNode The node to insert the new node before
* @return {HTMLElement} The node that was inserted (or null if insert fails)
*/
return null;
}
},
/**
* Inserts the new node as the next sibling of the reference node
* @method insertAfter
* @param {String | HTMLElement} newNode The node to be inserted
* @param {String | HTMLElement} referenceNode The node to insert the new node after
* @return {HTMLElement} The node that was inserted (or null if insert fails)
*/
return null;
}
if (referenceNode[NEXT_SIBLING]) {
} else {
}
},
/**
* Creates a new dom node using the provided markup string.
* @method create
* @param {String} html The markup used to create the element
* @param {HTMLDocument} doc An optional document context
* @param {Boolean} execScripts Whether or not any provided scripts should be executed.
* If execScripts is false, all scripts are stripped.
*/
if (m && custom[m[1]]) {
} else {
}
}
return node;
},
'for': 'htmlFor',
'class': 'className'
} : { // w3c
'htmlFor': 'for',
'className': 'class'
},
/**
* Provides a normalized attribute interface.
* @method setAttibute
* @param {String | HTMLElement} el The target element for the attribute.
* @param {String} attr The attribute to set.
* @param {String} val The value of the attribute.
*/
}
},
/**
* Provides a normalized attribute interface.
* @method getAttibute
* @param {String | HTMLElement} el The target element for the attribute.
* @param {String} attr The attribute to get.
* @return {String} The current value of the attribute.
*/
var ret = '';
} else {
}
if (ret === null) {
}
}
return ret;
},
function(node) {
} :
function(node) {
},
},
},
var scripts,
switch(where) {
case 'innerHTML':
break;
case 'beforeBegin':
break;
case 'afterBegin':
break;
case 'afterEnd':
break;
default: // and 'beforeEnd'
}
if (execScripts) {
} else {
}
} else { // prevent any scripts from being injected
}
return newNode;
},
_stripScripts: function(node) {
}
},
var newScript;
// "pause" while loading to ensure exec order
// FF reports typeof onload as "undefined", so try IE first
newScript.onreadystatechange = function() {
// timer to help ensure exec order
}
};
} else {
};
}
return; // NOTE: early return to chain async loading
}
}
},
/**
* 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.
*/
while (needle) {
return true;
}
}
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
*/
}
},
/**
* 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.
*/
},
/**
* 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.
*/
},
// TODO: document this
var ret = null,
if (element) {
if (rev) {
} else {
axis = NEXT_SIBLING;
}
} else { // need to scan nextSibling axis of firstChild to find matching element
}
}
return ret;
},
},
creators: {},
}
};
(function() {
TABLE_OPEN = '<table>',
TABLE_CLOSE = '</table>';
return frag[FIRST_CHILD];
},
return frag[FIRST_CHILD];
},
return frag[FIRST_CHILD];
},
return frag[FIRST_CHILD];
},
legend: 'fieldset'
});
}
}
return frag;
},
}
});
}
});
}
})();