MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / dom-attrs / dom-attrs.js
blob858985fe3411b005176c4283e442004b6e13aa60
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('dom-attrs', function(Y) {
9 var documentElement = Y.config.doc.documentElement,
10     Y_DOM = Y.DOM,
11     TAG_NAME = 'tagName',
12     OWNER_DOCUMENT = 'ownerDocument',
13     EMPTY_STRING = '',
14     addFeature = Y.Features.add,
15     testFeature = Y.Features.test;
17 Y.mix(Y_DOM, {
18     /**
19      * Returns the text content of the HTMLElement. 
20      * @method getText         
21      * @param {HTMLElement} element The html element. 
22      * @return {String} The text content of the element (includes text of any descending elements).
23      */
24     getText: (documentElement.textContent !== undefined) ?
25         function(element) {
26             var ret = '';
27             if (element) {
28                 ret = element.textContent;
29             }
30             return ret || '';
31         } : function(element) {
32             var ret = '';
33             if (element) {
34                 ret = element.innerText || element.nodeValue; // might be a textNode
35             }
36             return ret || '';
37         },
39     /**
40      * Sets the text content of the HTMLElement. 
41      * @method setText         
42      * @param {HTMLElement} element The html element. 
43      * @param {String} content The content to add. 
44      */
45     setText: (documentElement.textContent !== undefined) ?
46         function(element, content) {
47             if (element) {
48                 element.textContent = content;
49             }
50         } : function(element, content) {
51             if ('innerText' in element) {
52                 element.innerText = content;
53             } else if ('nodeValue' in element) {
54                 element.nodeValue = content;
55             }
56     },
58     CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
59         'for': 'htmlFor',
60         'class': 'className'
61     } : { // w3c
62         'htmlFor': 'for',
63         'className': 'class'
64     },
66     /**
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.
72      */
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);
77         }
78     },
81     /**
82      * Provides a normalized attribute interface. 
83      * @method getAttibute
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. 
87      */
88     getAttribute: function(el, attr, ieAttr) {
89         ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
90         var ret = '';
91         if (el && attr && el.getAttribute) {
92             attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
93             ret = el.getAttribute(attr, ieAttr);
95             if (ret === null) {
96                 ret = ''; // per DOM spec
97             }
98         }
99         return ret;
100     },
102     VALUE_SETTERS: {},
104     VALUE_GETTERS: {},
106     getValue: function(node) {
107         var ret = '', // TODO: return null?
108             getter;
110         if (node && node[TAG_NAME]) {
111             getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
113             if (getter) {
114                 ret = getter(node);
115             } else {
116                 ret = node.value;
117             }
118         }
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
124         }
126         return (typeof ret === 'string') ? ret : '';
127     },
129     setValue: function(node, val) {
130         var setter;
132         if (node && node[TAG_NAME]) {
133             setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
135             if (setter) {
136                 setter(node, val);
137             } else {
138                 node.value = val;
139             }
140         }
141     },
143     creators: {}
146 addFeature('value-set', 'select', {
147     test: function() {
148         var node = Y.config.doc.createElement('select');
149         node.innerHTML = '<option>1</option><option>2</option>';
150         node.value = '2';
151         return (node.value && node.value === '2');
152     }
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');
162                 break;
163             }
164         }
165     }
168 Y.mix(Y_DOM.VALUE_GETTERS, {
169     button: function(node) {
170         return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
171     }
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;
178         if (!attr) {
179             attr = node[OWNER_DOCUMENT].createAttribute('value');
180             node.setAttributeNode(attr);
181         }
183         attr.value = val;
184     }
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;
192     },
194     select: function(node) {
195         var val = node.value,
196             options = node.options;
198         if (options && options.length) {
199             // TODO: implement multipe select
200             if (node.multiple) {
201             } else {
202                 val = Y_DOM.getValue(options[node.selectedIndex]);
203             }
204         }
206         return val;
207     }
211 }, '3.5.1' ,{requires:['dom-core']});