forked from dojo/dojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-attr.js
204 lines (184 loc) · 6.72 KB
/
dom-attr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
define(["exports", "./sniff", "./_base/lang", "./dom", "./dom-style", "./dom-prop"],
function(exports, has, lang, dom, style, prop){
// module:
// dojo/dom-attr
// summary:
// This module defines the core dojo DOM attributes API.
// TODOC: summary not showing up in output see https://github.com/csnover/js-doc-parse/issues/42
// =============================
// Element attribute Functions
// =============================
// This module will be obsolete soon. Use dojo/prop instead.
// dojo/dom-attr.get() should conform to http://www.w3.org/TR/DOM-Level-2-Core/
// attribute-related functions (to be obsolete soon)
var forcePropNames = {
innerHTML: 1,
textContent:1,
className: 1,
htmlFor: has("ie"),
value: 1
},
attrNames = {
// original attribute names
classname: "class",
htmlfor: "for",
// for IE
tabindex: "tabIndex",
readonly: "readOnly"
};
function _hasAttr(node, name){
var attr = node.getAttributeNode && node.getAttributeNode(name);
return !!attr && attr.specified; // Boolean
}
// There is a difference in the presence of certain properties and their default values
// between browsers. For example, on IE "disabled" is present on all elements,
// but it is value is "false"; "tabIndex" of <div> returns 0 by default on IE, yet other browsers
// can return -1.
exports.has = function hasAttr(/*DOMNode|String*/ node, /*String*/ name){
// summary:
// Returns true if the requested attribute is specified on the
// given element, and false otherwise.
// node: DOMNode|String
// id or reference to the element to check
// name: String
// the name of the attribute
// returns: Boolean
// true if the requested attribute is specified on the
// given element, and false otherwise
var lc = name.toLowerCase();
return forcePropNames[prop.names[lc] || name] || _hasAttr(dom.byId(node), attrNames[lc] || name); // Boolean
};
exports.get = function getAttr(/*DOMNode|String*/ node, /*String*/ name){
// summary:
// Gets an attribute on an HTML element.
// description:
// Handles normalized getting of attributes on DOM Nodes.
// node: DOMNode|String
// id or reference to the element to get the attribute on
// name: String
// the name of the attribute to get.
// returns:
// the value of the requested attribute or null if that attribute does not have a specified or
// default value;
//
// example:
// | // get the current value of the "foo" attribute on a node
// | require(["dojo/dom-attr", "dojo/dom"], function(domAttr, dom){
// | domAttr.get(dom.byId("nodeId"), "foo");
// | // or we can just pass the id:
// | domAttr.get("nodeId", "foo");
// | });
// |
node = dom.byId(node);
var lc = name.toLowerCase(),
propName = prop.names[lc] || name,
forceProp = forcePropNames[propName],
value = node[propName]; // should we access this attribute via a property or via getAttribute()?
if(forceProp && typeof value != "undefined"){
// node's property
return value; // Anything
}
if(propName == "textContent"){
return prop.get(node, propName);
}
if(propName != "href" && (typeof value == "boolean" || lang.isFunction(value))){
// node's property
return value; // Anything
}
// node's attribute
// we need _hasAttr() here to guard against IE returning a default value
var attrName = attrNames[lc] || name;
return _hasAttr(node, attrName) ? node.getAttribute(attrName) : null; // Anything
};
exports.set = function setAttr(/*DOMNode|String*/ node, /*String|Object*/ name, /*String?*/ value){
// summary:
// Sets an attribute on an HTML element.
// description:
// Handles normalized setting of attributes on DOM Nodes.
//
// When passing functions as values, note that they will not be
// directly assigned to slots on the node, but rather the default
// behavior will be removed and the new behavior will be added
// using `dojo.connect()`, meaning that event handler properties
// will be normalized and that some caveats with regards to
// non-standard behaviors for onsubmit apply. Namely that you
// should cancel form submission using `dojo.stopEvent()` on the
// passed event object instead of returning a boolean value from
// the handler itself.
// node: DOMNode|String
// id or reference to the element to set the attribute on
// name: String|Object
// the name of the attribute to set, or a hash of key-value pairs to set.
// value: String?
// the value to set for the attribute, if the name is a string.
// returns:
// the DOM node
//
// example:
// | // use attr() to set the tab index
// | require(["dojo/dom-attr"], function(domAttr){
// | domAttr.set("nodeId", "tabIndex", 3);
// | });
//
// example:
// Set multiple values at once, including event handlers:
// | require(["dojo/dom-attr"],
// | function(domAttr){
// | domAttr.set("formId", {
// | "foo": "bar",
// | "tabIndex": -1,
// | "method": "POST"
// | }
// | });
node = dom.byId(node);
if(arguments.length == 2){ // inline'd type check
// the object form of setter: the 2nd argument is a dictionary
for(var x in name){
exports.set(node, x, name[x]);
}
return node; // DomNode
}
var lc = name.toLowerCase(),
propName = prop.names[lc] || name,
forceProp = forcePropNames[propName];
if(propName == "style" && typeof value != "string"){ // inline'd type check
// special case: setting a style
style.set(node, value);
return node; // DomNode
}
if(forceProp || typeof value == "boolean" || lang.isFunction(value)){
return prop.set(node, name, value);
}
// node's attribute
node.setAttribute(attrNames[lc] || name, value);
return node; // DomNode
};
exports.remove = function removeAttr(/*DOMNode|String*/ node, /*String*/ name){
// summary:
// Removes an attribute from an HTML element.
// node: DOMNode|String
// id or reference to the element to remove the attribute from
// name: String
// the name of the attribute to remove
dom.byId(node).removeAttribute(attrNames[name.toLowerCase()] || name);
};
exports.getNodeProp = function getNodeProp(/*DomNode|String*/ node, /*String*/ name){
// summary:
// Returns an effective value of a property or an attribute.
// node: DOMNode|String
// id or reference to the element to remove the attribute from
// name: String
// the name of the attribute
// returns:
// the value of the attribute
node = dom.byId(node);
var lc = name.toLowerCase(), propName = prop.names[lc] || name;
if((propName in node) && propName != "href"){
// node's property
return node[propName]; // Anything
}
// node's attribute
var attrName = attrNames[lc] || name;
return _hasAttr(node, attrName) ? node.getAttribute(attrName) : null; // Anything
};
});