1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('dom-core', function (Y, NAME) {
4
var NODE_TYPE = 'nodeType',
5
OWNER_DOCUMENT = 'ownerDocument',
6
DOCUMENT_ELEMENT = 'documentElement',
7
DEFAULT_VIEW = 'defaultView',
8
PARENT_WINDOW = 'parentWindow',
10
PARENT_NODE = 'parentNode',
11
PREVIOUS_SIBLING = 'previousSibling',
12
NEXT_SIBLING = 'nextSibling',
13
CONTAINS = 'contains',
14
COMPARE_DOCUMENT_POSITION = 'compareDocumentPosition',
17
// IE < 8 throws on node.contains(textNode)
18
supportsContainsTextNode = (function() {
19
var node = Y.config.doc.createElement('div'),
20
textNode = node.appendChild(Y.config.doc.createTextNode('')),
24
result = node.contains(textNode);
31
* The DOM utility provides a cross-browser abtraction layer
32
* normalizing DOM tasks, and adds extra helper functionality
33
* for other common tasks.
42
* Provides DOM helper methods.
49
* Returns the HTMLElement with the given ID (Wrapper for document.getElementById).
51
* @param {String} id the id attribute
52
* @param {Object} doc optional The document to search. Defaults to current document
53
* @return {HTMLElement | null} The HTMLElement with the id, or null if none found.
55
byId: function(id, doc) {
56
// handle dupe IDs and IE name collision
57
return Y_DOM.allById(id, doc)[0] || null;
60
getId: function(node) {
62
// HTMLElement returned from FORM when INPUT name === "id"
63
// IE < 8: HTMLCollection returned when INPUT id === "id"
64
// via both getAttribute and form.id
65
if (node.id && !node.id.tagName && !node.id.item) {
67
} else if (node.attributes && node.attributes.id) {
68
id = node.attributes.id.value;
74
setId: function(node, id) {
75
if (node.setAttribute) {
76
node.setAttribute('id', id);
83
* Finds the ancestor of the element.
85
* @param {HTMLElement} element The html element.
86
* @param {Function} fn optional An optional boolean test to apply.
87
* The optional function is passed the current DOM node being tested as its only argument.
88
* If no function is given, the parentNode is returned.
89
* @param {Boolean} testSelf optional Whether or not to include the element in the scan
90
* @return {HTMLElement | null} The matching DOM node or null if none found.
92
ancestor: function(element, fn, testSelf, stopFn) {
95
ret = (!fn || fn(element)) ? element : null;
98
return ret || Y_DOM.elementByAxis(element, PARENT_NODE, fn, null, stopFn);
102
* Finds the ancestors of the element.
104
* @param {HTMLElement} element The html element.
105
* @param {Function} fn optional An optional boolean test to apply.
106
* The optional function is passed the current DOM node being tested as its only argument.
107
* If no function is given, all ancestors are returned.
108
* @param {Boolean} testSelf optional Whether or not to include the element in the scan
109
* @return {Array} An array containing all matching DOM nodes.
111
ancestors: function(element, fn, testSelf, stopFn) {
112
var ancestor = element,
115
while ((ancestor = Y_DOM.ancestor(ancestor, fn, testSelf, stopFn))) {
118
ret.unshift(ancestor);
120
if (stopFn && stopFn(ancestor)) {
130
* Searches the element by the given axis for the first matching element.
131
* @method elementByAxis
132
* @param {HTMLElement} element The html element.
133
* @param {String} axis The axis to search (parentNode, nextSibling, previousSibling).
134
* @param {Function} fn optional An optional boolean test to apply.
135
* @param {Boolean} all optional Whether all node types should be returned, or just element nodes.
136
* The optional function is passed the current HTMLElement being tested as its only argument.
137
* If no function is given, the first element is returned.
138
* @return {HTMLElement | null} The matching element or null if none found.
140
elementByAxis: function(element, axis, fn, all, stopAt) {
141
while (element && (element = element[axis])) { // NOTE: assignment
142
if ( (all || element[TAG_NAME]) && (!fn || fn(element)) ) {
146
if (stopAt && stopAt(element)) {
154
* Determines whether or not one HTMLElement is or contains another HTMLElement.
156
* @param {HTMLElement} element The containing html element.
157
* @param {HTMLElement} needle The html element that may be contained.
158
* @return {Boolean} Whether or not the element is or contains the needle.
160
contains: function(element, needle) {
163
if ( !needle || !element || !needle[NODE_TYPE] || !element[NODE_TYPE]) {
165
} else if (element[CONTAINS] &&
166
// IE < 8 throws on node.contains(textNode) so fall back to brute.
167
// Falling back for other nodeTypes as well.
168
(needle[NODE_TYPE] === 1 || supportsContainsTextNode)) {
169
ret = element[CONTAINS](needle);
170
} else if (element[COMPARE_DOCUMENT_POSITION]) {
171
// Match contains behavior (node.contains(node) === true).
172
// Needed for Firefox < 4.
173
if (element === needle || !!(element[COMPARE_DOCUMENT_POSITION](needle) & 16)) {
177
ret = Y_DOM._bruteContains(element, needle);
184
* Determines whether or not the HTMLElement is part of the document.
186
* @param {HTMLElement} element The containing html element.
187
* @param {HTMLElement} doc optional The document to check.
188
* @return {Boolean} Whether or not the element is attached to the document.
190
inDoc: function(element, doc) {
194
if (element && element.nodeType) {
195
(doc) || (doc = element[OWNER_DOCUMENT]);
197
rootNode = doc[DOCUMENT_ELEMENT];
199
// contains only works with HTML_ELEMENT
200
if (rootNode && rootNode.contains && element.tagName) {
201
ret = rootNode.contains(element);
203
ret = Y_DOM.contains(rootNode, element);
211
allById: function(id, root) {
212
root = root || Y.config.doc;
218
if (root.querySelectorAll) {
219
ret = root.querySelectorAll('[id="' + id + '"]');
220
} else if (root.all) {
221
nodes = root.all(id);
224
// root.all may return HTMLElement or HTMLCollection.
225
// some elements are also HTMLCollection (FORM, SELECT).
226
if (nodes.nodeName) {
227
if (nodes.id === id) { // avoid false positive on name
229
nodes = EMPTY_ARRAY; // done, no need to filter
230
} else { // prep for filtering
236
// filter out matches on node.name
237
// and element.id as reference to element with id === 'id'
238
for (i = 0; node = nodes[i++];) {
239
if (node.id === id ||
240
(node.attributes && node.attributes.id &&
241
node.attributes.id.value === id)) {
248
ret = [Y_DOM._getDoc(root).getElementById(id)];
255
isWindow: function(obj) {
256
return !!(obj && obj.scrollTo && obj.document);
259
_removeChildNodes: function(node) {
260
while (node.firstChild) {
261
node.removeChild(node.firstChild);
265
siblings: function(node, fn) {
269
while ((sibling = sibling[PREVIOUS_SIBLING])) {
270
if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
271
nodes.unshift(sibling);
276
while ((sibling = sibling[NEXT_SIBLING])) {
277
if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
286
* Brute force version of contains.
287
* Used for browsers without contains support for non-HTMLElement Nodes (textNodes, etc).
288
* @method _bruteContains
290
* @param {HTMLElement} element The containing html element.
291
* @param {HTMLElement} needle The html element that may be contained.
292
* @return {Boolean} Whether or not the element is or contains the needle.
294
_bruteContains: function(element, needle) {
296
if (element === needle) {
299
needle = needle.parentNode;
304
// TODO: move to Lang?
306
* Memoizes dynamic regular expressions to boost runtime performance.
309
* @param {String} str The string to convert to a regular expression.
310
* @param {String} flags optional An optinal string of flags.
311
* @return {RegExp} An instance of RegExp
313
_getRegExp: function(str, flags) {
315
Y_DOM._regexCache = Y_DOM._regexCache || {};
316
if (!Y_DOM._regexCache[str + flags]) {
317
Y_DOM._regexCache[str + flags] = new RegExp(str, flags);
319
return Y_DOM._regexCache[str + flags];
322
// TODO: make getDoc/Win true privates?
324
* returns the appropriate document.
327
* @param {HTMLElement} element optional Target element.
328
* @return {Object} The document for the given element or the default document.
330
_getDoc: function(element) {
331
var doc = Y.config.doc;
333
doc = (element[NODE_TYPE] === 9) ? element : // element === document
334
element[OWNER_DOCUMENT] || // element === DOM node
335
element.document || // element === window
336
Y.config.doc; // default
343
* returns the appropriate window.
346
* @param {HTMLElement} element optional Target element.
347
* @return {Object} The window for the given element or the default window.
349
_getWin: function(element) {
350
var doc = Y_DOM._getDoc(element);
351
return doc[DEFAULT_VIEW] || doc[PARENT_WINDOW] || Y.config.win;
354
_batch: function(nodes, fn, arg1, arg2, arg3, etc) {
355
fn = (typeof fn === 'string') ? Y_DOM[fn] : fn;
362
while ((node = nodes[i++])) {
363
result = result = fn.call(Y_DOM, node, arg1, arg2, arg3, etc);
364
if (typeof result !== 'undefined') {
371
return (typeof ret !== 'undefined') ? ret : nodes;
374
generateID: function(el) {
390
}, '3.9.1', {"requires": ["oop", "features"]});