~andreserl/maas/packaging_precise_rebase

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/dom-attrs/dom-attrs.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-20 18:12:30 UTC
  • mfrom: (145.2.22 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130320181230-6l5guc0nhlv2z4p7
Re-base againts latest quantal released branch towards SRU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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) {
 
8
 
 
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;
 
16
 
 
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
        },
 
38
 
 
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
    },
 
57
 
 
58
    CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
 
59
        'for': 'htmlFor',
 
60
        'class': 'className'
 
61
    } : { // w3c
 
62
        'htmlFor': 'for',
 
63
        'className': 'class'
 
64
    },
 
65
 
 
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
    },
 
79
 
 
80
 
 
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);
 
94
 
 
95
            if (ret === null) {
 
96
                ret = ''; // per DOM spec
 
97
            }
 
98
        }
 
99
        return ret;
 
100
    },
 
101
 
 
102
    VALUE_SETTERS: {},
 
103
 
 
104
    VALUE_GETTERS: {},
 
105
 
 
106
    getValue: function(node) {
 
107
        var ret = '', // TODO: return null?
 
108
            getter;
 
109
 
 
110
        if (node && node[TAG_NAME]) {
 
111
            getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
 
112
 
 
113
            if (getter) {
 
114
                ret = getter(node);
 
115
            } else {
 
116
                ret = node.value;
 
117
            }
 
118
        }
 
119
 
 
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
        }
 
125
 
 
126
        return (typeof ret === 'string') ? ret : '';
 
127
    },
 
128
 
 
129
    setValue: function(node, val) {
 
130
        var setter;
 
131
 
 
132
        if (node && node[TAG_NAME]) {
 
133
            setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
 
134
 
 
135
            if (setter) {
 
136
                setter(node, val);
 
137
            } else {
 
138
                node.value = val;
 
139
            }
 
140
        }
 
141
    },
 
142
 
 
143
    creators: {}
 
144
});
 
145
 
 
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
    }
 
153
});
 
154
 
 
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
    }
 
166
}
 
167
 
 
168
Y.mix(Y_DOM.VALUE_GETTERS, {
 
169
    button: function(node) {
 
170
        return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
 
171
    }
 
172
});
 
173
 
 
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
        }
 
182
 
 
183
        attr.value = val;
 
184
    }
 
185
});
 
186
 
 
187
 
 
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
    },
 
193
 
 
194
    select: function(node) {
 
195
        var val = node.value,
 
196
            options = node.options;
 
197
 
 
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
        }
 
205
 
 
206
        return val;
 
207
    }
 
208
});
 
209
 
 
210
 
 
211
}, '3.5.1' ,{requires:['dom-core']});