3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('dom-attrs', 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);
82 * Provides a normalized attribute interface.
84 * @param {HTMLElement} el The target element for the attribute.
85 * @param {String} attr The attribute to get.
86 * @return {String} The current value of the attribute.
88 getAttribute: function(el, attr, ieAttr) {
89 ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
91 if (el && attr && el.getAttribute) {
92 attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
93 ret = el.getAttribute(attr, ieAttr);
96 ret = ''; // per DOM spec
106 getValue: function(node) {
107 var ret = '', // TODO: return null?
110 if (node && node[TAG_NAME]) {
111 getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
120 // workaround for IE8 JSON stringify bug
121 // which converts empty string values to null
122 if (ret === EMPTY_STRING) {
123 ret = EMPTY_STRING; // for real
126 return (typeof ret === 'string') ? ret : '';
129 setValue: function(node, val) {
132 if (node && node[TAG_NAME]) {
133 setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
146 addFeature('value-set', 'select', {
148 var node = Y.config.doc.createElement('select');
149 node.innerHTML = '<option>1</option><option>2</option>';
151 return (node.value && node.value === '2');
155 if (!testFeature('value-set', 'select')) {
156 Y_DOM.VALUE_SETTERS.select = function(node, val) {
157 for (var i = 0, options = node.getElementsByTagName('option'), option;
158 option = options[i++];) {
159 if (Y_DOM.getValue(option) === val) {
160 option.selected = true;
161 //Y_DOM.setAttribute(option, 'selected', 'selected');
168 Y.mix(Y_DOM.VALUE_GETTERS, {
169 button: function(node) {
170 return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
174 Y.mix(Y_DOM.VALUE_SETTERS, {
175 // IE: node.value changes the button text, which should be handled via innerHTML
176 button: function(node, val) {
177 var attr = node.attributes.value;
179 attr = node[OWNER_DOCUMENT].createAttribute('value');
180 node.setAttributeNode(attr);
188 Y.mix(Y_DOM.VALUE_GETTERS, {
189 option: function(node) {
190 var attrs = node.attributes;
191 return (attrs.value && attrs.value.specified) ? node.value : node.text;
194 select: function(node) {
195 var val = node.value,
196 options = node.options;
198 if (options && options.length) {
199 // TODO: implement multipe select
202 val = Y_DOM.getValue(options[node.selectedIndex]);
211 }, '3.5.1' ,{requires:['dom-core']});