2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('dom-core', function (Y, NAME) {
10
var NODE_TYPE = 'nodeType',
11
OWNER_DOCUMENT = 'ownerDocument',
12
DOCUMENT_ELEMENT = 'documentElement',
13
DEFAULT_VIEW = 'defaultView',
14
PARENT_WINDOW = 'parentWindow',
16
PARENT_NODE = 'parentNode',
17
PREVIOUS_SIBLING = 'previousSibling',
18
NEXT_SIBLING = 'nextSibling',
19
CONTAINS = 'contains',
20
COMPARE_DOCUMENT_POSITION = 'compareDocumentPosition',
23
// IE < 8 throws on node.contains(textNode)
24
supportsContainsTextNode = (function() {
25
var node = Y.config.doc.createElement('div'),
26
textNode = node.appendChild(Y.config.doc.createTextNode('')),
30
result = node.contains(textNode);
37
* The DOM utility provides a cross-browser abtraction layer
38
* normalizing DOM tasks, and adds extra helper functionality
39
* for other common tasks.
48
* Provides DOM helper methods.
55
* Returns the HTMLElement with the given ID (Wrapper for document.getElementById).
57
* @param {String} id the id attribute
58
* @param {Object} doc optional The document to search. Defaults to current document
59
* @return {HTMLElement | null} The HTMLElement with the id, or null if none found.
61
byId: function(id, doc) {
62
// handle dupe IDs and IE name collision
63
return Y_DOM.allById(id, doc)[0] || null;
66
getId: function(node) {
68
// HTMLElement returned from FORM when INPUT name === "id"
69
// IE < 8: HTMLCollection returned when INPUT id === "id"
70
// via both getAttribute and form.id
71
if (node.id && !node.id.tagName && !node.id.item) {
73
} else if (node.attributes && node.attributes.id) {
74
id = node.attributes.id.value;
80
setId: function(node, id) {
81
if (node.setAttribute) {
82
node.setAttribute('id', id);
89
* Finds the ancestor of the element.
91
* @param {HTMLElement} element The html element.
92
* @param {Function} fn optional An optional boolean test to apply.
93
* The optional function is passed the current DOM node being tested as its only argument.
94
* If no function is given, the parentNode is returned.
95
* @param {Boolean} testSelf optional Whether or not to include the element in the scan
96
* @return {HTMLElement | null} The matching DOM node or null if none found.
98
ancestor: function(element, fn, testSelf, stopFn) {
101
ret = (!fn || fn(element)) ? element : null;
104
return ret || Y_DOM.elementByAxis(element, PARENT_NODE, fn, null, stopFn);
108
* Finds the ancestors of the element.
110
* @param {HTMLElement} element The html element.
111
* @param {Function} fn optional An optional boolean test to apply.
112
* The optional function is passed the current DOM node being tested as its only argument.
113
* If no function is given, all ancestors are returned.
114
* @param {Boolean} testSelf optional Whether or not to include the element in the scan
115
* @return {Array} An array containing all matching DOM nodes.
117
ancestors: function(element, fn, testSelf, stopFn) {
118
var ancestor = element,
121
while ((ancestor = Y_DOM.ancestor(ancestor, fn, testSelf, stopFn))) {
124
ret.unshift(ancestor);
126
if (stopFn && stopFn(ancestor)) {
136
* Searches the element by the given axis for the first matching element.
137
* @method elementByAxis
138
* @param {HTMLElement} element The html element.
139
* @param {String} axis The axis to search (parentNode, nextSibling, previousSibling).
140
* @param {Function} fn optional An optional boolean test to apply.
141
* @param {Boolean} all optional Whether all node types should be returned, or just element nodes.
142
* The optional function is passed the current HTMLElement being tested as its only argument.
143
* If no function is given, the first element is returned.
144
* @return {HTMLElement | null} The matching element or null if none found.
146
elementByAxis: function(element, axis, fn, all, stopAt) {
147
while (element && (element = element[axis])) { // NOTE: assignment
148
if ( (all || element[TAG_NAME]) && (!fn || fn(element)) ) {
152
if (stopAt && stopAt(element)) {
160
* Determines whether or not one HTMLElement is or contains another HTMLElement.
162
* @param {HTMLElement} element The containing html element.
163
* @param {HTMLElement} needle The html element that may be contained.
164
* @return {Boolean} Whether or not the element is or contains the needle.
166
contains: function(element, needle) {
169
if ( !needle || !element || !needle[NODE_TYPE] || !element[NODE_TYPE]) {
171
} else if (element[CONTAINS] &&
172
// IE < 8 throws on node.contains(textNode) so fall back to brute.
173
// Falling back for other nodeTypes as well.
174
(needle[NODE_TYPE] === 1 || supportsContainsTextNode)) {
175
ret = element[CONTAINS](needle);
176
} else if (element[COMPARE_DOCUMENT_POSITION]) {
177
// Match contains behavior (node.contains(node) === true).
178
// Needed for Firefox < 4.
179
if (element === needle || !!(element[COMPARE_DOCUMENT_POSITION](needle) & 16)) {
183
ret = Y_DOM._bruteContains(element, needle);
190
* Determines whether or not the HTMLElement is part of the document.
192
* @param {HTMLElement} element The containing html element.
193
* @param {HTMLElement} doc optional The document to check.
194
* @return {Boolean} Whether or not the element is attached to the document.
196
inDoc: function(element, doc) {
200
if (element && element.nodeType) {
201
(doc) || (doc = element[OWNER_DOCUMENT]);
203
rootNode = doc[DOCUMENT_ELEMENT];
205
// contains only works with HTML_ELEMENT
206
if (rootNode && rootNode.contains && element.tagName) {
207
ret = rootNode.contains(element);
209
ret = Y_DOM.contains(rootNode, element);
217
allById: function(id, root) {
218
root = root || Y.config.doc;
224
if (root.querySelectorAll) {
225
ret = root.querySelectorAll('[id="' + id + '"]');
226
} else if (root.all) {
227
nodes = root.all(id);
230
// root.all may return HTMLElement or HTMLCollection.
231
// some elements are also HTMLCollection (FORM, SELECT).
232
if (nodes.nodeName) {
233
if (nodes.id === id) { // avoid false positive on name
235
nodes = EMPTY_ARRAY; // done, no need to filter
236
} else { // prep for filtering
242
// filter out matches on node.name
243
// and element.id as reference to element with id === 'id'
244
for (i = 0; node = nodes[i++];) {
245
if (node.id === id ||
246
(node.attributes && node.attributes.id &&
247
node.attributes.id.value === id)) {
254
ret = [Y_DOM._getDoc(root).getElementById(id)];
261
isWindow: function(obj) {
262
return !!(obj && obj.scrollTo && obj.document);
265
_removeChildNodes: function(node) {
266
while (node.firstChild) {
267
node.removeChild(node.firstChild);
271
siblings: function(node, fn) {
275
while ((sibling = sibling[PREVIOUS_SIBLING])) {
276
if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
277
nodes.unshift(sibling);
282
while ((sibling = sibling[NEXT_SIBLING])) {
283
if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
292
* Brute force version of contains.
293
* Used for browsers without contains support for non-HTMLElement Nodes (textNodes, etc).
294
* @method _bruteContains
296
* @param {HTMLElement} element The containing html element.
297
* @param {HTMLElement} needle The html element that may be contained.
298
* @return {Boolean} Whether or not the element is or contains the needle.
300
_bruteContains: function(element, needle) {
302
if (element === needle) {
305
needle = needle.parentNode;
310
// TODO: move to Lang?
312
* Memoizes dynamic regular expressions to boost runtime performance.
315
* @param {String} str The string to convert to a regular expression.
316
* @param {String} flags optional An optinal string of flags.
317
* @return {RegExp} An instance of RegExp
319
_getRegExp: function(str, flags) {
321
Y_DOM._regexCache = Y_DOM._regexCache || {};
322
if (!Y_DOM._regexCache[str + flags]) {
323
Y_DOM._regexCache[str + flags] = new RegExp(str, flags);
325
return Y_DOM._regexCache[str + flags];
328
// TODO: make getDoc/Win true privates?
330
* returns the appropriate document.
333
* @param {HTMLElement} element optional Target element.
334
* @return {Object} The document for the given element or the default document.
336
_getDoc: function(element) {
337
var doc = Y.config.doc;
339
doc = (element[NODE_TYPE] === 9) ? element : // element === document
340
element[OWNER_DOCUMENT] || // element === DOM node
341
element.document || // element === window
342
Y.config.doc; // default
349
* returns the appropriate window.
352
* @param {HTMLElement} element optional Target element.
353
* @return {Object} The window for the given element or the default window.
355
_getWin: function(element) {
356
var doc = Y_DOM._getDoc(element);
357
return doc[DEFAULT_VIEW] || doc[PARENT_WINDOW] || Y.config.win;
360
_batch: function(nodes, fn, arg1, arg2, arg3, etc) {
361
fn = (typeof fn === 'string') ? Y_DOM[fn] : fn;
368
while ((node = nodes[i++])) {
369
result = result = fn.call(Y_DOM, node, arg1, arg2, arg3, etc);
370
if (typeof result !== 'undefined') {
377
return (typeof ret !== 'undefined') ? ret : nodes;
380
generateID: function(el) {
396
}, '3.10.3', {"requires": ["oop", "features"]});