~jonas-drange/ubuntu-webcatalog/tos-redirect

« back to all changes in this revision

Viewing changes to src/webcatalog/static/yui/3.10.3/build/dom-core/dom-core-debug.js

  • Committer: Tarmac
  • Author(s): Stephen Stewart
  • Date: 2013-06-26 09:19:32 UTC
  • mfrom: (184.1.4 ubuntu-global-nav)
  • Revision ID: tarmac-20130626091932-8urtuli368k8p7ds
[r=beuno,jonas-drange] add ubuntu global nav to apps.ubuntu.com

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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/
 
6
*/
 
7
 
 
8
YUI.add('dom-core', function (Y, NAME) {
 
9
 
 
10
var NODE_TYPE = 'nodeType',
 
11
    OWNER_DOCUMENT = 'ownerDocument',
 
12
    DOCUMENT_ELEMENT = 'documentElement',
 
13
    DEFAULT_VIEW = 'defaultView',
 
14
    PARENT_WINDOW = 'parentWindow',
 
15
    TAG_NAME = 'tagName',
 
16
    PARENT_NODE = 'parentNode',
 
17
    PREVIOUS_SIBLING = 'previousSibling',
 
18
    NEXT_SIBLING = 'nextSibling',
 
19
    CONTAINS = 'contains',
 
20
    COMPARE_DOCUMENT_POSITION = 'compareDocumentPosition',
 
21
    EMPTY_ARRAY = [],
 
22
    
 
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('')),
 
27
            result = false;
 
28
        
 
29
        try {
 
30
            result = node.contains(textNode);
 
31
        } catch(e) {}
 
32
 
 
33
        return result;
 
34
    })(),
 
35
 
 
36
/** 
 
37
 * The DOM utility provides a cross-browser abtraction layer
 
38
 * normalizing DOM tasks, and adds extra helper functionality
 
39
 * for other common tasks. 
 
40
 * @module dom
 
41
 * @main dom
 
42
 * @submodule dom-base
 
43
 * @for DOM
 
44
 *
 
45
 */
 
46
 
 
47
/**
 
48
 * Provides DOM helper methods.
 
49
 * @class DOM
 
50
 *
 
51
 */
 
52
    
 
53
Y_DOM = {
 
54
    /**
 
55
     * Returns the HTMLElement with the given ID (Wrapper for document.getElementById).
 
56
     * @method byId         
 
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. 
 
60
     */
 
61
    byId: function(id, doc) {
 
62
        // handle dupe IDs and IE name collision
 
63
        return Y_DOM.allById(id, doc)[0] || null;
 
64
    },
 
65
 
 
66
    getId: function(node) {
 
67
        var id;
 
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) {
 
72
            id = node.id;
 
73
        } else if (node.attributes && node.attributes.id) {
 
74
            id = node.attributes.id.value;
 
75
        }
 
76
 
 
77
        return id;
 
78
    },
 
79
 
 
80
    setId: function(node, id) {
 
81
        if (node.setAttribute) {
 
82
            node.setAttribute('id', id);
 
83
        } else {
 
84
            node.id = id;
 
85
        }
 
86
    },
 
87
 
 
88
    /*
 
89
     * Finds the ancestor of the element.
 
90
     * @method ancestor
 
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. 
 
97
     */
 
98
    ancestor: function(element, fn, testSelf, stopFn) {
 
99
        var ret = null;
 
100
        if (testSelf) {
 
101
            ret = (!fn || fn(element)) ? element : null;
 
102
 
 
103
        }
 
104
        return ret || Y_DOM.elementByAxis(element, PARENT_NODE, fn, null, stopFn);
 
105
    },
 
106
 
 
107
    /*
 
108
     * Finds the ancestors of the element.
 
109
     * @method ancestors
 
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.
 
116
     */
 
117
    ancestors: function(element, fn, testSelf, stopFn) {
 
118
        var ancestor = element,
 
119
            ret = [];
 
120
 
 
121
        while ((ancestor = Y_DOM.ancestor(ancestor, fn, testSelf, stopFn))) {
 
122
            testSelf = false;
 
123
            if (ancestor) {
 
124
                ret.unshift(ancestor);
 
125
 
 
126
                if (stopFn && stopFn(ancestor)) {
 
127
                    return ret;
 
128
                }
 
129
            }
 
130
        }
 
131
 
 
132
        return ret;
 
133
    },
 
134
 
 
135
    /**
 
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.
 
145
     */
 
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)) ) {
 
149
                    return element;
 
150
                }
 
151
 
 
152
                if (stopAt && stopAt(element)) {
 
153
                    return null;
 
154
                }
 
155
        }
 
156
        return null;
 
157
    },
 
158
 
 
159
    /**
 
160
     * Determines whether or not one HTMLElement is or contains another HTMLElement.
 
161
     * @method contains
 
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.
 
165
     */
 
166
    contains: function(element, needle) {
 
167
        var ret = false;
 
168
 
 
169
        if ( !needle || !element || !needle[NODE_TYPE] || !element[NODE_TYPE]) {
 
170
            ret = false;
 
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)) { 
 
180
                ret = true;
 
181
            }
 
182
        } else {
 
183
            ret = Y_DOM._bruteContains(element, needle);
 
184
        }
 
185
 
 
186
        return ret;
 
187
    },
 
188
 
 
189
    /**
 
190
     * Determines whether or not the HTMLElement is part of the document.
 
191
     * @method inDoc
 
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. 
 
195
     */
 
196
    inDoc: function(element, doc) {
 
197
        var ret = false,
 
198
            rootNode;
 
199
 
 
200
        if (element && element.nodeType) {
 
201
            (doc) || (doc = element[OWNER_DOCUMENT]);
 
202
 
 
203
            rootNode = doc[DOCUMENT_ELEMENT];
 
204
 
 
205
            // contains only works with HTML_ELEMENT
 
206
            if (rootNode && rootNode.contains && element.tagName) {
 
207
                ret = rootNode.contains(element);
 
208
            } else {
 
209
                ret = Y_DOM.contains(rootNode, element);
 
210
            }
 
211
        }
 
212
 
 
213
        return ret;
 
214
 
 
215
    },
 
216
 
 
217
   allById: function(id, root) {
 
218
        root = root || Y.config.doc;
 
219
        var nodes = [],
 
220
            ret = [],
 
221
            i,
 
222
            node;
 
223
 
 
224
        if (root.querySelectorAll) {
 
225
            ret = root.querySelectorAll('[id="' + id + '"]');
 
226
        } else if (root.all) {
 
227
            nodes = root.all(id);
 
228
 
 
229
            if (nodes) {
 
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
 
234
                        ret.push(nodes);
 
235
                        nodes = EMPTY_ARRAY; // done, no need to filter
 
236
                    } else { //  prep for filtering
 
237
                        nodes = [nodes];
 
238
                    }
 
239
                }
 
240
 
 
241
                if (nodes.length) {
 
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)) { 
 
248
                            ret.push(node);
 
249
                        }
 
250
                    }
 
251
                }
 
252
            }
 
253
        } else {
 
254
            ret = [Y_DOM._getDoc(root).getElementById(id)];
 
255
        }
 
256
    
 
257
        return ret;
 
258
   },
 
259
 
 
260
 
 
261
    isWindow: function(obj) {
 
262
        return !!(obj && obj.scrollTo && obj.document);
 
263
    },
 
264
 
 
265
    _removeChildNodes: function(node) {
 
266
        while (node.firstChild) {
 
267
            node.removeChild(node.firstChild);
 
268
        }
 
269
    },
 
270
 
 
271
    siblings: function(node, fn) {
 
272
        var nodes = [],
 
273
            sibling = node;
 
274
 
 
275
        while ((sibling = sibling[PREVIOUS_SIBLING])) {
 
276
            if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
 
277
                nodes.unshift(sibling);
 
278
            }
 
279
        }
 
280
 
 
281
        sibling = node;
 
282
        while ((sibling = sibling[NEXT_SIBLING])) {
 
283
            if (sibling[TAG_NAME] && (!fn || fn(sibling))) {
 
284
                nodes.push(sibling);
 
285
            }
 
286
        }
 
287
 
 
288
        return nodes;
 
289
    },
 
290
 
 
291
    /**
 
292
     * Brute force version of contains.
 
293
     * Used for browsers without contains support for non-HTMLElement Nodes (textNodes, etc).
 
294
     * @method _bruteContains
 
295
     * @private
 
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.
 
299
     */
 
300
    _bruteContains: function(element, needle) {
 
301
        while (needle) {
 
302
            if (element === needle) {
 
303
                return true;
 
304
            }
 
305
            needle = needle.parentNode;
 
306
        }
 
307
        return false;
 
308
    },
 
309
 
 
310
// TODO: move to Lang?
 
311
    /**
 
312
     * Memoizes dynamic regular expressions to boost runtime performance. 
 
313
     * @method _getRegExp
 
314
     * @private
 
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
 
318
     */
 
319
    _getRegExp: function(str, flags) {
 
320
        flags = 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);
 
324
        }
 
325
        return Y_DOM._regexCache[str + flags];
 
326
    },
 
327
 
 
328
// TODO: make getDoc/Win true privates?
 
329
    /**
 
330
     * returns the appropriate document.
 
331
     * @method _getDoc
 
332
     * @private
 
333
     * @param {HTMLElement} element optional Target element.
 
334
     * @return {Object} The document for the given element or the default document. 
 
335
     */
 
336
    _getDoc: function(element) {
 
337
        var doc = Y.config.doc;
 
338
        if (element) {
 
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
 
343
        }
 
344
 
 
345
        return doc;
 
346
    },
 
347
 
 
348
    /**
 
349
     * returns the appropriate window.
 
350
     * @method _getWin
 
351
     * @private
 
352
     * @param {HTMLElement} element optional Target element.
 
353
     * @return {Object} The window for the given element or the default window. 
 
354
     */
 
355
    _getWin: function(element) {
 
356
        var doc = Y_DOM._getDoc(element);
 
357
        return doc[DEFAULT_VIEW] || doc[PARENT_WINDOW] || Y.config.win;
 
358
    },
 
359
 
 
360
    _batch: function(nodes, fn, arg1, arg2, arg3, etc) {
 
361
        fn = (typeof fn === 'string') ? Y_DOM[fn] : fn;
 
362
        var result,
 
363
            i = 0,
 
364
            node,
 
365
            ret;
 
366
 
 
367
        if (fn && nodes) {
 
368
            while ((node = nodes[i++])) {
 
369
                result = result = fn.call(Y_DOM, node, arg1, arg2, arg3, etc);
 
370
                if (typeof result !== 'undefined') {
 
371
                    (ret) || (ret = []);
 
372
                    ret.push(result);
 
373
                }
 
374
            }
 
375
        }
 
376
 
 
377
        return (typeof ret !== 'undefined') ? ret : nodes;
 
378
    },
 
379
 
 
380
    generateID: function(el) {
 
381
        var id = el.id;
 
382
 
 
383
        if (!id) {
 
384
            id = Y.stamp(el);
 
385
            el.id = id; 
 
386
        }   
 
387
 
 
388
        return id; 
 
389
    }
 
390
};
 
391
 
 
392
 
 
393
Y.DOM = Y_DOM;
 
394
 
 
395
 
 
396
}, '3.10.3', {"requires": ["oop", "features"]});