1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('dom-base', function (Y, NAME) {
8
var documentElement = Y.config.doc.documentElement,
11
OWNER_DOCUMENT = 'ownerDocument',
13
addFeature = Y.Features.add,
14
testFeature = Y.Features.test;
18
* Returns the text content of the HTMLElement.
20
* @param {HTMLElement} element The html element.
21
* @return {String} The text content of the element (includes text of any descending elements).
23
getText: (documentElement.textContent !== undefined) ?
27
ret = element.textContent;
30
} : function(element) {
33
ret = element.innerText || element.nodeValue; // might be a textNode
39
* Sets the text content of the HTMLElement.
41
* @param {HTMLElement} element The html element.
42
* @param {String} content The content to add.
44
setText: (documentElement.textContent !== undefined) ?
45
function(element, content) {
47
element.textContent = content;
49
} : function(element, content) {
50
if ('innerText' in element) {
51
element.innerText = content;
52
} else if ('nodeValue' in element) {
53
element.nodeValue = content;
57
CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
66
* Provides a normalized attribute interface.
67
* @method setAttribute
68
* @param {HTMLElement} el The target element for the attribute.
69
* @param {String} attr The attribute to set.
70
* @param {String} val The value of the attribute.
72
setAttribute: function(el, attr, val, ieAttr) {
73
if (el && attr && el.setAttribute) {
74
attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
75
el.setAttribute(attr, val, ieAttr);
81
* Provides a normalized attribute interface.
82
* @method getAttribute
83
* @param {HTMLElement} el The target element for the attribute.
84
* @param {String} attr The attribute to get.
85
* @return {String} The current value of the attribute.
87
getAttribute: function(el, attr, ieAttr) {
88
ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
90
if (el && attr && el.getAttribute) {
91
attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
92
ret = el.getAttribute(attr, ieAttr);
95
ret = ''; // per DOM spec
105
getValue: function(node) {
106
var ret = '', // TODO: return null?
109
if (node && node[TAG_NAME]) {
110
getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
119
// workaround for IE8 JSON stringify bug
120
// which converts empty string values to null
121
if (ret === EMPTY_STRING) {
122
ret = EMPTY_STRING; // for real
125
return (typeof ret === 'string') ? ret : '';
128
setValue: function(node, val) {
131
if (node && node[TAG_NAME]) {
132
setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
145
addFeature('value-set', 'select', {
147
var node = Y.config.doc.createElement('select');
148
node.innerHTML = '<option>1</option><option>2</option>';
150
return (node.value && node.value === '2');
154
if (!testFeature('value-set', 'select')) {
155
Y_DOM.VALUE_SETTERS.select = function(node, val) {
156
for (var i = 0, options = node.getElementsByTagName('option'), option;
157
option = options[i++];) {
158
if (Y_DOM.getValue(option) === val) {
159
option.selected = true;
160
//Y_DOM.setAttribute(option, 'selected', 'selected');
167
Y.mix(Y_DOM.VALUE_GETTERS, {
168
button: function(node) {
169
return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
173
Y.mix(Y_DOM.VALUE_SETTERS, {
174
// IE: node.value changes the button text, which should be handled via innerHTML
175
button: function(node, val) {
176
var attr = node.attributes.value;
178
attr = node[OWNER_DOCUMENT].createAttribute('value');
179
node.setAttributeNode(attr);
187
Y.mix(Y_DOM.VALUE_GETTERS, {
188
option: function(node) {
189
var attrs = node.attributes;
190
return (attrs.value && attrs.value.specified) ? node.value : node.text;
193
select: function(node) {
194
var val = node.value,
195
options = node.options;
197
if (options && options.length) {
198
// TODO: implement multipe select
200
} else if (node.selectedIndex > -1) {
201
val = Y_DOM.getValue(options[node.selectedIndex]);
208
var addClass, hasClass, removeClass;
212
* Determines whether a DOM element has the given className.
215
* @param {HTMLElement} element The DOM element.
216
* @param {String} className the class name to search for
217
* @return {Boolean} Whether or not the element has the given class.
219
hasClass: function(node, className) {
220
var re = Y.DOM._getRegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
221
return re.test(node.className);
225
* Adds a class name to a given DOM element.
228
* @param {HTMLElement} element The DOM element.
229
* @param {String} className the class name to add to the class attribute
231
addClass: function(node, className) {
232
if (!Y.DOM.hasClass(node, className)) { // skip if already present
233
node.className = Y.Lang.trim([node.className, className].join(' '));
238
* Removes a class name from a given element.
239
* @method removeClass
241
* @param {HTMLElement} element The DOM element.
242
* @param {String} className the class name to remove from the class attribute
244
removeClass: function(node, className) {
245
if (className && hasClass(node, className)) {
246
node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
247
className + '(?:\\s+|$)'), ' '));
249
if ( hasClass(node, className) ) { // in case of multiple adjacent
250
removeClass(node, className);
256
* Replace a class with another class for a given element.
257
* If no oldClassName is present, the newClassName is simply added.
258
* @method replaceClass
260
* @param {HTMLElement} element The DOM element
261
* @param {String} oldClassName the class name to be replaced
262
* @param {String} newClassName the class name that will be replacing the old class name
264
replaceClass: function(node, oldC, newC) {
265
removeClass(node, oldC); // remove first in case oldC === newC
266
addClass(node, newC);
270
* If the className exists on the node it is removed, if it doesn't exist it is added.
271
* @method toggleClass
273
* @param {HTMLElement} element The DOM element
274
* @param {String} className the class name to be toggled
275
* @param {Boolean} addClass optional boolean to indicate whether class
276
* should be added or removed regardless of current state
278
toggleClass: function(node, className, force) {
279
var add = (force !== undefined) ? force :
280
!(hasClass(node, className));
283
addClass(node, className);
285
removeClass(node, className);
290
hasClass = Y.DOM.hasClass;
291
removeClass = Y.DOM.removeClass;
292
addClass = Y.DOM.addClass;
294
var re_tag = /<([a-z]+)/i,
298
addFeature = Y.Features.add,
299
testFeature = Y.Features.test,
303
createFromDIV = function(html, tag) {
304
var div = Y.config.doc.createElement('div'),
307
div.innerHTML = html;
308
if (!div.firstChild || div.firstChild.tagName !== tag.toUpperCase()) {
315
re_tbody = /(?:\/(?:thead|tfoot|tbody|caption|col|colgroup)>)+\s*<tbody/,
317
TABLE_OPEN = '<table>',
318
TABLE_CLOSE = '</table>';
323
_create: function(html, doc, tag) {
326
var frag = Y_DOM._fragClones[tag];
328
frag = frag.cloneNode(false);
330
frag = Y_DOM._fragClones[tag] = doc.createElement(tag);
332
frag.innerHTML = html;
336
_children: function(node, tag) {
338
children = node.children,
343
if (children && children.tags) { // use tags filter when possible
345
children = node.children.tags(tag);
346
} else { // IE leaks comments into children
347
hasComments = children.tags('!').length;
351
if (!children || (!children.tags && tag) || hasComments) {
352
childNodes = children || node.childNodes;
354
while ((child = childNodes[i++])) {
355
if (child.nodeType === 1) {
356
if (!tag || tag === child.tagName) {
357
children.push(child);
363
return children || [];
367
* Creates a new dom node using the provided markup string.
369
* @param {String} html The markup used to create the element
370
* @param {HTMLDocument} doc An optional document context
371
* @return {HTMLElement|DocumentFragment} returns a single HTMLElement
372
* when creating one node, and a documentFragment when creating
375
create: function(html, doc) {
376
if (typeof html === 'string') {
377
html = Y.Lang.trim(html); // match IE which trims whitespace from innerHTML
381
doc = doc || Y.config.doc;
382
var m = re_tag.exec(html),
383
create = Y_DOM._create,
389
if (html != undefined) { // not undefined or null
391
creator = custom[m[1].toLowerCase()];
392
if (typeof creator === 'function') {
399
nodes = create(html, doc, tag).childNodes;
401
if (nodes.length === 1) { // return single node, breaking parentNode ref from "fragment"
402
ret = nodes[0].parentNode.removeChild(nodes[0]);
403
} else if (nodes[0] && nodes[0].className === 'yui3-big-dummy') { // using dummy node to preserve some attributes (e.g. OPTION not selected)
404
if (nodes.length === 2) {
405
ret = nodes[0].nextSibling;
407
nodes[0].parentNode.removeChild(nodes[0]);
408
ret = Y_DOM._nl2frag(nodes, doc);
410
} else { // return multiple nodes as a fragment
411
ret = Y_DOM._nl2frag(nodes, doc);
419
_nl2frag: function(nodes, doc) {
423
if (nodes && (nodes.push || nodes.item) && nodes[0]) {
424
doc = doc || nodes[0].ownerDocument;
425
ret = doc.createDocumentFragment();
427
if (nodes.item) { // convert live list to static array
428
nodes = Y.Array(nodes, 0, true);
431
for (i = 0, len = nodes.length; i < len; i++) {
432
ret.appendChild(nodes[i]);
434
} // else inline with log for minification
439
* Inserts content in a node at the given location
441
* @param {HTMLElement} node The node to insert into
442
* @param {HTMLElement | Array | HTMLCollection} content The content to be inserted
443
* @param {HTMLElement} where Where to insert the content
444
* If no "where" is given, content is appended to the node
445
* Possible values for "where"
447
* <dt>HTMLElement</dt>
448
* <dd>The element to insert before</dd>
450
* <dd>Replaces the existing HTML</dd>
452
* <dd>Inserts before the existing HTML</dd>
454
* <dd>Inserts content before the node</dd>
456
* <dd>Inserts content after the node</dd>
459
addHTML: function(node, content, where) {
460
var nodeParent = node.parentNode,
467
if (content != undefined) { // not null or undefined (maybe 0)
468
if (content.nodeType) { // DOM node, just add it
470
} else if (typeof content == 'string' || typeof content == 'number') {
471
ret = newNode = Y_DOM.create(content);
472
} else if (content[0] && content[0].nodeType) { // array or collection
473
newNode = Y.config.doc.createDocumentFragment();
474
while ((item = content[i++])) {
475
newNode.appendChild(item); // append to fragment for insertion
481
if (newNode && where.parentNode) { // insert regardless of relationship to node
482
where.parentNode.insertBefore(newNode, where);
486
while (node.firstChild) {
487
node.removeChild(node.firstChild);
489
if (newNode) { // allow empty content to clear node
490
node.appendChild(newNode);
495
nodeParent.insertBefore(newNode, node);
500
if (node.nextSibling) { // IE errors if refNode is null
501
nodeParent.insertBefore(newNode, node.nextSibling);
503
nodeParent.appendChild(newNode);
509
node.appendChild(newNode);
513
} else if (newNode) {
514
node.appendChild(newNode);
520
wrap: function(node, html) {
521
var parent = (html && html.nodeType) ? html : Y.DOM.create(html),
522
nodes = parent.getElementsByTagName('*');
525
parent = nodes[nodes.length - 1];
528
if (node.parentNode) {
529
node.parentNode.replaceChild(parent, node);
531
parent.appendChild(node);
534
unwrap: function(node) {
535
var parent = node.parentNode,
536
lastChild = parent.lastChild,
541
grandparent = parent.parentNode;
543
node = parent.firstChild;
544
while (node !== lastChild) {
545
next = node.nextSibling;
546
grandparent.insertBefore(node, parent);
549
grandparent.replaceChild(lastChild, parent);
551
parent.removeChild(node);
557
addFeature('innerhtml', 'table', {
559
var node = Y.config.doc.createElement('table');
561
node.innerHTML = '<tbody></tbody>';
565
return (node.firstChild && node.firstChild.nodeName === 'TBODY');
569
addFeature('innerhtml-div', 'tr', {
571
return createFromDIV('<tr></tr>', 'tr');
575
addFeature('innerhtml-div', 'script', {
577
return createFromDIV('<script></script>', 'script');
581
if (!testFeature('innerhtml', 'table')) {
582
// TODO: thead/tfoot with nested tbody
583
// IE adds TBODY when creating TABLE elements (which may share this impl)
584
creators.tbody = function(html, doc) {
585
var frag = Y_DOM.create(TABLE_OPEN + html + TABLE_CLOSE, doc),
586
tb = Y.DOM._children(frag, 'tbody')[0];
588
if (frag.children.length > 1 && tb && !re_tbody.test(html)) {
589
tb.parentNode.removeChild(tb); // strip extraneous tbody
595
if (!testFeature('innerhtml-div', 'script')) {
596
creators.script = function(html, doc) {
597
var frag = doc.createElement('div');
599
frag.innerHTML = '-' + html;
600
frag.removeChild(frag.firstChild);
604
creators.link = creators.style = creators.script;
607
if (!testFeature('innerhtml-div', 'tr')) {
609
option: function(html, doc) {
610
return Y_DOM.create('<select><option class="yui3-big-dummy" selected></option>' + html + '</select>', doc);
613
tr: function(html, doc) {
614
return Y_DOM.create('<tbody>' + html + '</tbody>', doc);
617
td: function(html, doc) {
618
return Y_DOM.create('<tr>' + html + '</tr>', doc);
621
col: function(html, doc) {
622
return Y_DOM.create('<colgroup>' + html + '</colgroup>', doc);
631
thead: creators.tbody,
632
tfoot: creators.tbody,
633
caption: creators.tbody,
634
colgroup: creators.tbody,
635
optgroup: creators.option
639
Y_DOM.creators = creators;
642
* Sets the width of the element to the given size, regardless
643
* of box model, border, padding, etc.
645
* @param {HTMLElement} element The DOM element.
646
* @param {String|Number} size The pixel height to size to
649
setWidth: function(node, size) {
650
Y.DOM._setSize(node, 'width', size);
654
* Sets the height of the element to the given size, regardless
655
* of box model, border, padding, etc.
657
* @param {HTMLElement} element The DOM element.
658
* @param {String|Number} size The pixel height to size to
661
setHeight: function(node, size) {
662
Y.DOM._setSize(node, 'height', size);
665
_setSize: function(node, prop, val) {
666
val = (val > 0) ? val : 0;
669
node.style[prop] = val + 'px';
670
size = (prop === 'height') ? node.offsetHeight : node.offsetWidth;
673
val = val - (size - val);
679
node.style[prop] = val + 'px';
685
}, '3.9.1', {"requires": ["dom-core"]});