3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('dom-base', function(Y) {
9
var documentElement = Y.config.doc.documentElement,
12
OWNER_DOCUMENT = 'ownerDocument',
14
addFeature = Y.Features.add,
15
testFeature = Y.Features.test;
19
* Returns the text content of the HTMLElement.
21
* @param {HTMLElement} element The html element.
22
* @return {String} The text content of the element (includes text of any descending elements).
24
getText: (documentElement.textContent !== undefined) ?
28
ret = element.textContent;
31
} : function(element) {
34
ret = element.innerText || element.nodeValue; // might be a textNode
40
* Sets the text content of the HTMLElement.
42
* @param {HTMLElement} element The html element.
43
* @param {String} content The content to add.
45
setText: (documentElement.textContent !== undefined) ?
46
function(element, content) {
48
element.textContent = content;
50
} : function(element, content) {
51
if ('innerText' in element) {
52
element.innerText = content;
53
} else if ('nodeValue' in element) {
54
element.nodeValue = content;
58
CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
67
* Provides a normalized attribute interface.
68
* @method setAttribute
69
* @param {HTMLElement} el The target element for the attribute.
70
* @param {String} attr The attribute to set.
71
* @param {String} val The value of the attribute.
73
setAttribute: function(el, attr, val, ieAttr) {
74
if (el && attr && el.setAttribute) {
75
attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
76
el.setAttribute(attr, val, ieAttr);
78
else { Y.log('bad input to setAttribute', 'warn', 'dom'); }
83
* Provides a normalized attribute interface.
85
* @param {HTMLElement} el The target element for the attribute.
86
* @param {String} attr The attribute to get.
87
* @return {String} The current value of the attribute.
89
getAttribute: function(el, attr, ieAttr) {
90
ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
92
if (el && attr && el.getAttribute) {
93
attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
94
ret = el.getAttribute(attr, ieAttr);
97
ret = ''; // per DOM spec
100
else { Y.log('bad input to getAttribute', 'warn', 'dom'); }
108
getValue: function(node) {
109
var ret = '', // TODO: return null?
112
if (node && node[TAG_NAME]) {
113
getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
122
// workaround for IE8 JSON stringify bug
123
// which converts empty string values to null
124
if (ret === EMPTY_STRING) {
125
ret = EMPTY_STRING; // for real
128
return (typeof ret === 'string') ? ret : '';
131
setValue: function(node, val) {
134
if (node && node[TAG_NAME]) {
135
setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
148
addFeature('value-set', 'select', {
150
var node = Y.config.doc.createElement('select');
151
node.innerHTML = '<option>1</option><option>2</option>';
153
return (node.value && node.value === '2');
157
if (!testFeature('value-set', 'select')) {
158
Y_DOM.VALUE_SETTERS.select = function(node, val) {
159
for (var i = 0, options = node.getElementsByTagName('option'), option;
160
option = options[i++];) {
161
if (Y_DOM.getValue(option) === val) {
162
option.selected = true;
163
//Y_DOM.setAttribute(option, 'selected', 'selected');
170
Y.mix(Y_DOM.VALUE_GETTERS, {
171
button: function(node) {
172
return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
176
Y.mix(Y_DOM.VALUE_SETTERS, {
177
// IE: node.value changes the button text, which should be handled via innerHTML
178
button: function(node, val) {
179
var attr = node.attributes.value;
181
attr = node[OWNER_DOCUMENT].createAttribute('value');
182
node.setAttributeNode(attr);
190
Y.mix(Y_DOM.VALUE_GETTERS, {
191
option: function(node) {
192
var attrs = node.attributes;
193
return (attrs.value && attrs.value.specified) ? node.value : node.text;
196
select: function(node) {
197
var val = node.value,
198
options = node.options;
200
if (options && options.length) {
201
// TODO: implement multipe select
203
Y.log('multiple select normalization not implemented', 'warn', 'DOM');
205
val = Y_DOM.getValue(options[node.selectedIndex]);
212
var addClass, hasClass, removeClass;
216
* Determines whether a DOM element has the given className.
219
* @param {HTMLElement} element The DOM element.
220
* @param {String} className the class name to search for
221
* @return {Boolean} Whether or not the element has the given class.
223
hasClass: function(node, className) {
224
var re = Y.DOM._getRegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
225
return re.test(node.className);
229
* Adds a class name to a given DOM element.
232
* @param {HTMLElement} element The DOM element.
233
* @param {String} className the class name to add to the class attribute
235
addClass: function(node, className) {
236
if (!Y.DOM.hasClass(node, className)) { // skip if already present
237
node.className = Y.Lang.trim([node.className, className].join(' '));
242
* Removes a class name from a given element.
243
* @method removeClass
245
* @param {HTMLElement} element The DOM element.
246
* @param {String} className the class name to remove from the class attribute
248
removeClass: function(node, className) {
249
if (className && hasClass(node, className)) {
250
node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
251
className + '(?:\\s+|$)'), ' '));
253
if ( hasClass(node, className) ) { // in case of multiple adjacent
254
removeClass(node, className);
260
* Replace a class with another class for a given element.
261
* If no oldClassName is present, the newClassName is simply added.
262
* @method replaceClass
264
* @param {HTMLElement} element The DOM element
265
* @param {String} oldClassName the class name to be replaced
266
* @param {String} newClassName the class name that will be replacing the old class name
268
replaceClass: function(node, oldC, newC) {
269
//Y.log('replaceClass replacing ' + oldC + ' with ' + newC, 'info', 'Node');
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;
342
* Creates a new dom node using the provided markup string.
344
* @param {String} html The markup used to create the element
345
* @param {HTMLDocument} doc An optional document context
346
* @return {HTMLElement|DocumentFragment} returns a single HTMLElement
347
* when creating one node, and a documentFragment when creating
350
create: function(html, doc) {
351
if (typeof html === 'string') {
352
html = Y.Lang.trim(html); // match IE which trims whitespace from innerHTML
356
doc = doc || Y.config.doc;
357
var m = re_tag.exec(html),
358
create = Y_DOM._create,
364
if (html != undefined) { // not undefined or null
366
creator = custom[m[1].toLowerCase()];
367
if (typeof creator === 'function') {
374
nodes = create(html, doc, tag).childNodes;
376
if (nodes.length === 1) { // return single node, breaking parentNode ref from "fragment"
377
ret = nodes[0].parentNode.removeChild(nodes[0]);
378
} else if (nodes[0] && nodes[0].className === 'yui3-big-dummy') { // using dummy node to preserve some attributes (e.g. OPTION not selected)
379
if (nodes.length === 2) {
380
ret = nodes[0].nextSibling;
382
nodes[0].parentNode.removeChild(nodes[0]);
383
ret = Y_DOM._nl2frag(nodes, doc);
385
} else { // return multiple nodes as a fragment
386
ret = Y_DOM._nl2frag(nodes, doc);
393
_nl2frag: function(nodes, doc) {
397
if (nodes && (nodes.push || nodes.item) && nodes[0]) {
398
doc = doc || nodes[0].ownerDocument;
399
ret = doc.createDocumentFragment();
401
if (nodes.item) { // convert live list to static array
402
nodes = Y.Array(nodes, 0, true);
405
for (i = 0, len = nodes.length; i < len; i++) {
406
ret.appendChild(nodes[i]);
408
} // else inline with log for minification
409
else { Y.log('unable to convert ' + nodes + ' to fragment', 'warn', 'dom'); }
414
* Inserts content in a node at the given location
416
* @param {HTMLElement} node The node to insert into
417
* @param {HTMLElement | Array | HTMLCollection} content The content to be inserted
418
* @param {HTMLElement} where Where to insert the content
419
* If no "where" is given, content is appended to the node
420
* Possible values for "where"
422
* <dt>HTMLElement</dt>
423
* <dd>The element to insert before</dd>
425
* <dd>Replaces the existing HTML</dd>
427
* <dd>Inserts before the existing HTML</dd>
429
* <dd>Inserts content before the node</dd>
431
* <dd>Inserts content after the node</dd>
434
addHTML: function(node, content, where) {
435
var nodeParent = node.parentNode,
442
if (content != undefined) { // not null or undefined (maybe 0)
443
if (content.nodeType) { // DOM node, just add it
445
} else if (typeof content == 'string' || typeof content == 'number') {
446
ret = newNode = Y_DOM.create(content);
447
} else if (content[0] && content[0].nodeType) { // array or collection
448
newNode = Y.config.doc.createDocumentFragment();
449
while ((item = content[i++])) {
450
newNode.appendChild(item); // append to fragment for insertion
456
if (newNode && where.parentNode) { // insert regardless of relationship to node
457
where.parentNode.insertBefore(newNode, where);
461
while (node.firstChild) {
462
node.removeChild(node.firstChild);
464
if (newNode) { // allow empty content to clear node
465
node.appendChild(newNode);
470
nodeParent.insertBefore(newNode, node);
475
if (node.nextSibling) { // IE errors if refNode is null
476
nodeParent.insertBefore(newNode, node.nextSibling);
478
nodeParent.appendChild(newNode);
484
node.appendChild(newNode);
488
} else if (newNode) {
489
node.appendChild(newNode);
496
addFeature('innerhtml', 'table', {
498
var node = Y.config.doc.createElement('table');
500
node.innerHTML = '<tbody></tbody>';
504
return (node.firstChild && node.firstChild.nodeName === 'TBODY');
508
addFeature('innerhtml-div', 'tr', {
510
return createFromDIV('<tr></tr>', 'tr');
514
addFeature('innerhtml-div', 'script', {
516
return createFromDIV('<script></script>', 'script');
520
if (!testFeature('innerhtml', 'table')) {
521
// TODO: thead/tfoot with nested tbody
522
// IE adds TBODY when creating TABLE elements (which may share this impl)
523
creators.tbody = function(html, doc) {
524
var frag = Y_DOM.create(TABLE_OPEN + html + TABLE_CLOSE, doc),
525
tb = frag.children.tags('tbody')[0];
527
if (frag.children.length > 1 && tb && !re_tbody.test(html)) {
528
tb.parentNode.removeChild(tb); // strip extraneous tbody
534
if (!testFeature('innerhtml-div', 'script')) {
535
creators.script = function(html, doc) {
536
var frag = doc.createElement('div');
538
frag.innerHTML = '-' + html;
539
frag.removeChild(frag.firstChild);
543
creators.link = creators.style = creators.script;
546
if (!testFeature('innerhtml-div', 'tr')) {
548
option: function(html, doc) {
549
return Y_DOM.create('<select><option class="yui3-big-dummy" selected></option>' + html + '</select>', doc);
552
tr: function(html, doc) {
553
return Y_DOM.create('<tbody>' + html + '</tbody>', doc);
556
td: function(html, doc) {
557
return Y_DOM.create('<tr>' + html + '</tr>', doc);
560
col: function(html, doc) {
561
return Y_DOM.create('<colgroup>' + html + '</colgroup>', doc);
570
thead: creators.tbody,
571
tfoot: creators.tbody,
572
caption: creators.tbody,
573
colgroup: creators.tbody,
574
optgroup: creators.option
578
Y_DOM.creators = creators;
581
* Sets the width of the element to the given size, regardless
582
* of box model, border, padding, etc.
584
* @param {HTMLElement} element The DOM element.
585
* @param {String|Int} size The pixel height to size to
588
setWidth: function(node, size) {
589
Y.DOM._setSize(node, 'width', size);
593
* Sets the height of the element to the given size, regardless
594
* of box model, border, padding, etc.
596
* @param {HTMLElement} element The DOM element.
597
* @param {String|Int} size The pixel height to size to
600
setHeight: function(node, size) {
601
Y.DOM._setSize(node, 'height', size);
604
_setSize: function(node, prop, val) {
605
val = (val > 0) ? val : 0;
608
node.style[prop] = val + 'px';
609
size = (prop === 'height') ? node.offsetHeight : node.offsetWidth;
612
val = val - (size - val);
618
node.style[prop] = val + 'px';
624
}, '3.4.1' ,{requires:['dom-core']});