dom-base.js revision c31e28da00e9fe86d7e9aebaa5ff25933fec30b4
* for other common tasks. * Provides DOM helper methods. * Returns the HTMLElement with the given ID (Wrapper for document.getElementById). * @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. // handle dupe IDs and IE name collision * Returns the text content of the HTMLElement. * @param {HTMLElement} element The html element. * @return {String} The text content of the element (includes text of any descending elements). * Sets the text content of the HTMLElement. * @param {HTMLElement} element The html element. * @param {String} content The content to add. * Finds the previous sibling of the element. * @deprecated Use elementByAxis * @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. * @deprecated Use elementByAxis * @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. * @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} testSelf optional Whether or not to include the element in the scan * @return {HTMLElement | null} The matching DOM node or null if none found. * Searches the element by the given axis for the first matching element. * @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. * Determines whether or not one HTMLElement is or contains another HTMLElement. * @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. * Determines whether or not the HTMLElement is part of the document. * @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. // contains only works with HTML_ELEMENT for (i =
0;
node =
nodes[i++];) {
// check for a match * Creates a new dom node using the provided markup string. * @param {String} html The markup used to create the element * @param {HTMLDocument} doc An optional document context * @return {HTMLElement|DocumentFragment} returns a single HTMLElement * when creating one node, and a documentFragment when creating if (
typeof html ===
'string') {
if (
typeof custom[m[
1]] ===
'function') {
if (
nodes.
length ===
1) {
// return single node, breaking parentNode ref from "fragment" }
else if (
nodes[
0] &&
nodes[
0].
className ===
'yui3-big-dummy') {
// using dummy node to preserve some attributes (e.g. OPTION not selected) }
else {
// return multiple nodes as a fragment if (
nodes.
item) {
// convert live list to static array }
// else inline with log for minification * Provides a normalized attribute interface. * @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. * @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. ret =
'';
// per DOM spec * Inserts content in a node at the given location * @param {HTMLElement} node The node to insert into * @param {String | HTMLElement} content The content to be inserted * @param {String | HTMLElement} where Where to insert the content * If no "where" is given, content is appended to the node * Possible values for "where" * <dd>The element to insert before</dd> * <dd>Replaces the existing HTML</dd> * <dd>Inserts before the existing HTML</dd> * <dd>Inserts content before the node</dd> * <dd>Inserts content after the node</dd> }
else {
// create from string and cache if (
where.
nodeType) {
// insert regardless of relationship to node // TODO: check if node.contains(where)? if (
newNode) {
// allow empty content to clear node var ret =
'',
// TODO: return null? // workaround for IE8 JSON stringify bug // which converts empty string values to null return (
typeof ret ===
'string') ?
ret :
'';
* Brute force version of contains. * Used for browsers without contains support for non-HTMLElement Nodes (textNodes, etc). * @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. * Memoizes dynamic regular expressions to boost runtime performance. * @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. * @param {HTMLElement} element optional Target element. * @return {Object} The document for the given element or the default document. * returns the appropriate window. * @param {HTMLElement} element optional Target element. * @return {Object} The window for the given element or the default window. if (
typeof result !==
'undefined') {
// IE adds TBODY when creating TABLE elements (which may share this impl) // IE: node.value changes the button text, which should be handled via innerHTML return create(
'<select><option class="yui3-big-dummy" selected></option>' +
html +
'</select>',
doc);
// TODO: implement multipe select * Determines whether a DOM element has the given className. * @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. * Adds a class name to a given DOM element. * @param {HTMLElement} element The DOM element. * @param {String} className the class name to add to the class attribute * Removes a class name from a given element. * @param {HTMLElement} element The DOM element. * @param {String} className the class name to remove from the class attribute * Replace a class with another class for a given element. * If no oldClassName is present, the newClassName is simply added. * @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 * If the className exists on the node it is removed, if it doesn't exist it is added. * @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 * Sets the width of the element to the given size, regardless * of box model, border, padding, etc. * @param {HTMLElement} element The DOM element. * @param {String|Int} size The pixel height to size to * Sets the height of the element to the given size, regardless * of box model, border, padding, etc. * @param {HTMLElement} element The DOM element. * @param {String|Int} size The pixel height to size to