3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('dom-base', function(Y) {
13
var documentElement = Y.config.doc.documentElement,
16
OWNER_DOCUMENT = 'ownerDocument',
18
addFeature = Y.Features.add,
19
testFeature = Y.Features.test;
23
* Returns the text content of the HTMLElement.
25
* @param {HTMLElement} element The html element.
26
* @return {String} The text content of the element (includes text of any descending elements).
28
getText: (documentElement.textContent !== undefined) ?
32
ret = element.textContent;
35
} : function(element) {
38
ret = element.innerText || element.nodeValue; // might be a textNode
44
* Sets the text content of the HTMLElement.
46
* @param {HTMLElement} element The html element.
47
* @param {String} content The content to add.
49
setText: (documentElement.textContent !== undefined) ?
50
function(element, content) {
52
element.textContent = content;
54
} : function(element, content) {
55
if ('innerText' in element) {
56
element.innerText = content;
57
} else if ('nodeValue' in element) {
58
element.nodeValue = content;
62
CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
71
* Provides a normalized attribute interface.
72
* @method setAttribute
73
* @param {HTMLElement} el The target element for the attribute.
74
* @param {String} attr The attribute to set.
75
* @param {String} val The value of the attribute.
77
setAttribute: function(el, attr, val, ieAttr) {
78
if (el && attr && el.setAttribute) {
79
attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
80
el.setAttribute(attr, val, ieAttr);
86
* Provides a normalized attribute interface.
87
* @method getAttribute
88
* @param {HTMLElement} el The target element for the attribute.
89
* @param {String} attr The attribute to get.
90
* @return {String} The current value of the attribute.
92
getAttribute: function(el, attr, ieAttr) {
93
ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
95
if (el && attr && el.getAttribute) {
96
attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
97
ret = el.getAttribute(attr, ieAttr);
100
ret = ''; // per DOM spec
110
getValue: function(node) {
111
var ret = '', // TODO: return null?
114
if (node && node[TAG_NAME]) {
115
getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
124
// workaround for IE8 JSON stringify bug
125
// which converts empty string values to null
126
if (ret === EMPTY_STRING) {
127
ret = EMPTY_STRING; // for real
130
return (typeof ret === 'string') ? ret : '';
133
setValue: function(node, val) {
136
if (node && node[TAG_NAME]) {
137
setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
150
addFeature('value-set', 'select', {
152
var node = Y.config.doc.createElement('select');
153
node.innerHTML = '<option>1</option><option>2</option>';
155
return (node.value && node.value === '2');
159
if (!testFeature('value-set', 'select')) {
160
Y_DOM.VALUE_SETTERS.select = function(node, val) {
161
for (var i = 0, options = node.getElementsByTagName('option'), option;
162
option = options[i++];) {
163
if (Y_DOM.getValue(option) === val) {
164
option.selected = true;
165
//Y_DOM.setAttribute(option, 'selected', 'selected');
172
Y.mix(Y_DOM.VALUE_GETTERS, {
173
button: function(node) {
174
return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
178
Y.mix(Y_DOM.VALUE_SETTERS, {
179
// IE: node.value changes the button text, which should be handled via innerHTML
180
button: function(node, val) {
181
var attr = node.attributes.value;
183
attr = node[OWNER_DOCUMENT].createAttribute('value');
184
node.setAttributeNode(attr);
192
Y.mix(Y_DOM.VALUE_GETTERS, {
193
option: function(node) {
194
var attrs = node.attributes;
195
return (attrs.value && attrs.value.specified) ? node.value : node.text;
198
select: function(node) {
199
var val = node.value,
200
options = node.options;
202
if (options && options.length) {
203
// TODO: implement multipe select
205
} else if (node.selectedIndex > -1) {
206
val = Y_DOM.getValue(options[node.selectedIndex]);
213
var addClass, hasClass, removeClass;
217
* Determines whether a DOM element has the given className.
220
* @param {HTMLElement} element The DOM element.
221
* @param {String} className the class name to search for
222
* @return {Boolean} Whether or not the element has the given class.
224
hasClass: function(node, className) {
225
var re = Y.DOM._getRegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
226
return re.test(node.className);
230
* Adds a class name to a given DOM element.
233
* @param {HTMLElement} element The DOM element.
234
* @param {String} className the class name to add to the class attribute
236
addClass: function(node, className) {
237
if (!Y.DOM.hasClass(node, className)) { // skip if already present
238
node.className = Y.Lang.trim([node.className, className].join(' '));
243
* Removes a class name from a given element.
244
* @method removeClass
246
* @param {HTMLElement} element The DOM element.
247
* @param {String} className the class name to remove from the class attribute
249
removeClass: function(node, className) {
250
if (className && hasClass(node, className)) {
251
node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
252
className + '(?:\\s+|$)'), ' '));
254
if ( hasClass(node, className) ) { // in case of multiple adjacent
255
removeClass(node, className);
261
* Replace a class with another class for a given element.
262
* If no oldClassName is present, the newClassName is simply added.
263
* @method replaceClass
265
* @param {HTMLElement} element The DOM element
266
* @param {String} oldClassName the class name to be replaced
267
* @param {String} newClassName the class name that will be replacing the old class name
269
replaceClass: function(node, oldC, newC) {
270
removeClass(node, oldC); // remove first in case oldC === newC
271
addClass(node, newC);
275
* If the className exists on the node it is removed, if it doesn't exist it is added.
276
* @method toggleClass
278
* @param {HTMLElement} element The DOM element
279
* @param {String} className the class name to be toggled
280
* @param {Boolean} addClass optional boolean to indicate whether class
281
* should be added or removed regardless of current state
283
toggleClass: function(node, className, force) {
284
var add = (force !== undefined) ? force :
285
!(hasClass(node, className));
288
addClass(node, className);
290
removeClass(node, className);
295
hasClass = Y.DOM.hasClass;
296
removeClass = Y.DOM.removeClass;
297
addClass = Y.DOM.addClass;
299
var re_tag = /<([a-z]+)/i,
303
addFeature = Y.Features.add,
304
testFeature = Y.Features.test,
308
createFromDIV = function(html, tag) {
309
var div = Y.config.doc.createElement('div'),
312
div.innerHTML = html;
313
if (!div.firstChild || div.firstChild.tagName !== tag.toUpperCase()) {
320
re_tbody = /(?:\/(?:thead|tfoot|tbody|caption|col|colgroup)>)+\s*<tbody/,
322
TABLE_OPEN = '<table>',
323
TABLE_CLOSE = '</table>';
328
_create: function(html, doc, tag) {
331
var frag = Y_DOM._fragClones[tag];
333
frag = frag.cloneNode(false);
335
frag = Y_DOM._fragClones[tag] = doc.createElement(tag);
337
frag.innerHTML = html;
341
_children: function(node, tag) {
343
children = node.children,
348
if (children && children.tags) { // use tags filter when possible
350
children = node.children.tags(tag);
351
} else { // IE leaks comments into children
352
hasComments = children.tags('!').length;
356
if (!children || (!children.tags && tag) || hasComments) {
357
childNodes = children || node.childNodes;
359
while ((child = childNodes[i++])) {
360
if (child.nodeType === 1) {
361
if (!tag || tag === child.tagName) {
362
children.push(child);
368
return children || [];
372
* Creates a new dom node using the provided markup string.
374
* @param {String} html The markup used to create the element
375
* @param {HTMLDocument} doc An optional document context
376
* @return {HTMLElement|DocumentFragment} returns a single HTMLElement
377
* when creating one node, and a documentFragment when creating
380
create: function(html, doc) {
381
if (typeof html === 'string') {
382
html = Y.Lang.trim(html); // match IE which trims whitespace from innerHTML
386
doc = doc || Y.config.doc;
387
var m = re_tag.exec(html),
388
create = Y_DOM._create,
394
if (html != undefined) { // not undefined or null
396
creator = custom[m[1].toLowerCase()];
397
if (typeof creator === 'function') {
404
nodes = create(html, doc, tag).childNodes;
406
if (nodes.length === 1) { // return single node, breaking parentNode ref from "fragment"
407
ret = nodes[0].parentNode.removeChild(nodes[0]);
408
} else if (nodes[0] && nodes[0].className === 'yui3-big-dummy') { // using dummy node to preserve some attributes (e.g. OPTION not selected)
409
if (nodes.length === 2) {
410
ret = nodes[0].nextSibling;
412
nodes[0].parentNode.removeChild(nodes[0]);
413
ret = Y_DOM._nl2frag(nodes, doc);
415
} else { // return multiple nodes as a fragment
416
ret = Y_DOM._nl2frag(nodes, doc);
424
_nl2frag: function(nodes, doc) {
428
if (nodes && (nodes.push || nodes.item) && nodes[0]) {
429
doc = doc || nodes[0].ownerDocument;
430
ret = doc.createDocumentFragment();
432
if (nodes.item) { // convert live list to static array
433
nodes = Y.Array(nodes, 0, true);
436
for (i = 0, len = nodes.length; i < len; i++) {
437
ret.appendChild(nodes[i]);
439
} // else inline with log for minification
444
* Inserts content in a node at the given location
446
* @param {HTMLElement} node The node to insert into
447
* @param {HTMLElement | Array | HTMLCollection} content The content to be inserted
448
* @param {HTMLElement} where Where to insert the content
449
* If no "where" is given, content is appended to the node
450
* Possible values for "where"
452
* <dt>HTMLElement</dt>
453
* <dd>The element to insert before</dd>
455
* <dd>Replaces the existing HTML</dd>
457
* <dd>Inserts before the existing HTML</dd>
459
* <dd>Inserts content before the node</dd>
461
* <dd>Inserts content after the node</dd>
464
addHTML: function(node, content, where) {
465
var nodeParent = node.parentNode,
472
if (content != undefined) { // not null or undefined (maybe 0)
473
if (content.nodeType) { // DOM node, just add it
475
} else if (typeof content == 'string' || typeof content == 'number') {
476
ret = newNode = Y_DOM.create(content);
477
} else if (content[0] && content[0].nodeType) { // array or collection
478
newNode = Y.config.doc.createDocumentFragment();
479
while ((item = content[i++])) {
480
newNode.appendChild(item); // append to fragment for insertion
486
if (newNode && where.parentNode) { // insert regardless of relationship to node
487
where.parentNode.insertBefore(newNode, where);
491
while (node.firstChild) {
492
node.removeChild(node.firstChild);
494
if (newNode) { // allow empty content to clear node
495
node.appendChild(newNode);
500
nodeParent.insertBefore(newNode, node);
505
if (node.nextSibling) { // IE errors if refNode is null
506
nodeParent.insertBefore(newNode, node.nextSibling);
508
nodeParent.appendChild(newNode);
514
node.appendChild(newNode);
518
} else if (newNode) {
519
node.appendChild(newNode);
525
wrap: function(node, html) {
526
var parent = (html && html.nodeType) ? html : Y.DOM.create(html),
527
nodes = parent.getElementsByTagName('*');
530
parent = nodes[nodes.length - 1];
533
if (node.parentNode) {
534
node.parentNode.replaceChild(parent, node);
536
parent.appendChild(node);
539
unwrap: function(node) {
540
var parent = node.parentNode,
541
lastChild = parent.lastChild,
546
grandparent = parent.parentNode;
548
node = parent.firstChild;
549
while (node !== lastChild) {
550
next = node.nextSibling;
551
grandparent.insertBefore(node, parent);
554
grandparent.replaceChild(lastChild, parent);
556
parent.removeChild(node);
562
addFeature('innerhtml', 'table', {
564
var node = Y.config.doc.createElement('table');
566
node.innerHTML = '<tbody></tbody>';
570
return (node.firstChild && node.firstChild.nodeName === 'TBODY');
574
addFeature('innerhtml-div', 'tr', {
576
return createFromDIV('<tr></tr>', 'tr');
580
addFeature('innerhtml-div', 'script', {
582
return createFromDIV('<script></script>', 'script');
586
if (!testFeature('innerhtml', 'table')) {
587
// TODO: thead/tfoot with nested tbody
588
// IE adds TBODY when creating TABLE elements (which may share this impl)
589
creators.tbody = function(html, doc) {
590
var frag = Y_DOM.create(TABLE_OPEN + html + TABLE_CLOSE, doc),
591
tb = Y.DOM._children(frag, 'tbody')[0];
593
if (frag.children.length > 1 && tb && !re_tbody.test(html)) {
594
tb.parentNode.removeChild(tb); // strip extraneous tbody
600
if (!testFeature('innerhtml-div', 'script')) {
601
creators.script = function(html, doc) {
602
var frag = doc.createElement('div');
604
frag.innerHTML = '-' + html;
605
frag.removeChild(frag.firstChild);
609
creators.link = creators.style = creators.script;
612
if (!testFeature('innerhtml-div', 'tr')) {
614
option: function(html, doc) {
615
return Y_DOM.create('<select><option class="yui3-big-dummy" selected></option>' + html + '</select>', doc);
618
tr: function(html, doc) {
619
return Y_DOM.create('<tbody>' + html + '</tbody>', doc);
622
td: function(html, doc) {
623
return Y_DOM.create('<tr>' + html + '</tr>', doc);
626
col: function(html, doc) {
627
return Y_DOM.create('<colgroup>' + html + '</colgroup>', doc);
636
thead: creators.tbody,
637
tfoot: creators.tbody,
638
caption: creators.tbody,
639
colgroup: creators.tbody,
640
optgroup: creators.option
644
Y_DOM.creators = creators;
647
* Sets the width of the element to the given size, regardless
648
* of box model, border, padding, etc.
650
* @param {HTMLElement} element The DOM element.
651
* @param {String|Number} size The pixel height to size to
654
setWidth: function(node, size) {
655
Y.DOM._setSize(node, 'width', size);
659
* Sets the height of the element to the given size, regardless
660
* of box model, border, padding, etc.
662
* @param {HTMLElement} element The DOM element.
663
* @param {String|Number} size The pixel height to size to
666
setHeight: function(node, size) {
667
Y.DOM._setSize(node, 'height', size);
670
_setSize: function(node, prop, val) {
671
val = (val > 0) ? val : 0;
674
node.style[prop] = val + 'px';
675
size = (prop === 'height') ? node.offsetHeight : node.offsetWidth;
678
val = val - (size - val);
684
node.style[prop] = val + 'px';
690
}, '3.5.1' ,{requires:['dom-core']});