2
2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
3
Code licensed under the BSD License:
4
4
http://developer.yahoo.net/yui/license.txt
7
7
YUI.add('dom-base', function(Y) {
79
79
* @return {HTMLElement | null} The first matching child html element.
81
81
firstChild: function(element, fn) {
82
return Y.DOM._childBy(element, null, fn);
82
return Y.DOM._childBy(element, null, fn);
85
85
firstChildByTag: function(element, tag, fn) {
86
return Y.DOM._childBy(element, tag, fn);
86
return Y.DOM._childBy(element, tag, fn);
90
90
* Finds the lastChild of the given HTMLElement.
91
91
* @method lastChild
92
92
* @param {HTMLElement} element The html element.
93
* @param {String} tag The tag to search for.
93
94
* @param {Function} fn optional An optional boolean test to apply.
94
95
* The optional function is passed the current HTMLElement being tested as its only argument.
95
96
* If no function is given, the first found is returned.
96
97
* @return {HTMLElement | null} The first matching child html element.
98
99
lastChild: function(element, fn) {
99
return Y.DOM._childBy(element, null, fn, true);
100
return Y.DOM._childBy(element, null, fn, true);
102
103
lastChildByTag: function(element, tag, fn) {
103
return Y.DOM._childBy(element, tag, fn, true);
104
return Y.DOM._childBy(element, tag, fn, true);
159
160
* @return {Array} The collection of child elements.
161
162
children: function(element, fn) {
162
return Y.DOM.childrenByTag(element, null, fn);
163
return Y.DOM.childrenByTag(element, null, fn);
172
173
* @param {Boolean} all optional Whether all node types should be scanned, or just element nodes.
173
174
* @return {HTMLElement | null} The matching DOM node or null if none found.
175
previous: function(element, fn, all) {
176
previous: function(element, fn) {
176
177
return Y.DOM.elementByAxis(element, PREVIOUS_SIBLING, fn);
211
212
* @param {String} axis The axis to search (parentNode, nextSibling, previousSibling).
212
213
* @param {Function} fn optional An optional boolean test to apply.
213
214
* @param {Boolean} all optional Whether all node types should be returned, or just element nodes.
214
* The optional function is passed the current DOM node being tested as its only argument.
215
* The optional function is passed the current HTMLElement being tested as its only argument.
215
216
* If no function is given, the first element is returned.
216
217
* @return {HTMLElement | null} The matching element or null if none found.
255
256
* @param {HTMLElement} root optional An optional root element to start from.
256
257
* @param {Function} fn optional An optional boolean test to apply.
257
258
* The optional function is passed the current HTMLElement being tested as its only argument.
258
* If no function is given, the first match is returned.
259
* If no function is given, the first match is returned.
259
260
* @return {HTMLElement} The matching element.
261
262
firstByTag: function(tag, root, fn) {
285
286
filterElementsBy: function(elements, fn, firstOnly) {
286
287
var ret = (firstOnly) ? null : [];
287
for (var i = 0, el; el = elements[i++];) {
288
if ( el[TAG_NAME] && (!fn || fn(el)) ) {
288
for (var i = 0, len = elements[LENGTH]; i < len; ++i) {
289
if (elements[i][TAG_NAME] && (!fn || fn(elements[i]))) {
293
ret[ret[LENGTH]] = el;
294
ret[ret[LENGTH]] = elements[i];
308
309
contains: function(element, needle) {
311
if (!needle || !needle[NODE_TYPE] || !element || !element[NODE_TYPE]) {
312
if ( !needle || !element || !needle[NODE_TYPE] || !element[NODE_TYPE]) {
313
314
} else if (element[CONTAINS]) {
314
315
if (Y.UA.opera || needle[NODE_TYPE] === 1) { // IE & SAF contains fail if needle not an ELEMENT_NODE
329
* Returns an HTMLElement for the given string of HTML.
331
* @param {String} html The string of HTML to convert to a DOM element.
332
* @param {Document} document An optional document to create the node with.
333
* @return {HTMLElement} The newly created element.
330
* Determines whether or not the HTMLElement is part of the document.
332
* @param {HTMLElement} element The containing html element.
333
* @param {HTMLElement} doc optional The document to check.
334
* @return {Boolean} Whether or not the element is attached to the document.
336
inDoc: function(element, doc) {
337
doc = doc || Y.config.doc;
338
return Y.DOM.contains(doc.documentElement, element);
335
341
create: function(html, doc) {
336
342
doc = doc || Y.config.doc;
337
343
var m = re_tag.exec(html);