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