1 define("dojo/dom-attr", ["exports", "./sniff", "./_base/lang", "./dom", "./dom-style", "./dom-prop"],
2 function(exports, has, lang, dom, style, prop){
6 // This module defines the core dojo DOM attributes API.
8 // TODOC: summary not showing up in output see https://github.com/csnover/js-doc-parse/issues/42
10 // =============================
11 // Element attribute Functions
12 // =============================
14 // This module will be obsolete soon. Use dojo/prop instead.
16 // dojo.attr() should conform to http://www.w3.org/TR/DOM-Level-2-Core/
18 // attribute-related functions (to be obsolete soon)
20 var forcePropNames = {
27 // original attribute names
35 function _hasAttr(node, name){
36 var attr = node.getAttributeNode && node.getAttributeNode(name);
37 return attr && attr.specified; // Boolean
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
45 exports.has = function hasAttr(/*DOMNode|String*/ node, /*String*/ name){
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
52 // the name of the attribute
54 // true if the requested attribute is specified on the
55 // given element, and false otherwise
57 var lc = name.toLowerCase();
58 return forcePropNames[prop.names[lc] || name] || _hasAttr(dom.byId(node), attrNames[lc] || name); // Boolean
61 exports.get = function getAttr(/*DOMNode|String*/ node, /*String*/ name){
63 // Gets an attribute on an HTML element.
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
69 // the name of the attribute to get.
71 // the value of the requested attribute or null if that attribute does not have a specified or
75 // | // get the current value of the "foo" attribute on a node
76 // | dojo.getAttr(dojo.byId("nodeId"), "foo");
77 // | // or we can just pass the id:
78 // | dojo.getAttr("nodeId", "foo");
80 node = dom.byId(node);
81 var lc = name.toLowerCase(),
82 propName = prop.names[lc] || name,
83 forceProp = forcePropNames[propName],
84 value = node[propName]; // should we access this attribute via a property or via getAttribute()?
86 if(forceProp && typeof value != "undefined"){
88 return value; // Anything
90 if(propName != "href" && (typeof value == "boolean" || lang.isFunction(value))){
92 return value; // Anything
95 // we need _hasAttr() here to guard against IE returning a default value
96 var attrName = attrNames[lc] || name;
97 return _hasAttr(node, attrName) ? node.getAttribute(attrName) : null; // Anything
100 exports.set = function setAttr(/*DOMNode|String*/ node, /*String|Object*/ name, /*String?*/ value){
102 // Sets an attribute on an HTML element.
104 // Handles normalized setting of attributes on DOM Nodes.
106 // When passing functions as values, note that they will not be
107 // directly assigned to slots on the node, but rather the default
108 // behavior will be removed and the new behavior will be added
109 // using `dojo.connect()`, meaning that event handler properties
110 // will be normalized and that some caveats with regards to
111 // non-standard behaviors for onsubmit apply. Namely that you
112 // should cancel form submission using `dojo.stopEvent()` on the
113 // passed event object instead of returning a boolean value from
114 // the handler itself.
115 // node: DOMNode|String
116 // id or reference to the element to set the attribute on
117 // name: String|Object
118 // the name of the attribute to set, or a hash of key-value pairs to set.
120 // the value to set for the attribute, if the name is a string.
125 // | // use attr() to set the tab index
126 // | dojo.setAttr("nodeId", "tabIndex", 3);
129 // Set multiple values at once, including event handlers:
130 // | dojo.setAttr("formId", {
133 // | "method": "POST",
134 // | "onsubmit": function(e){
135 // | // stop submitting the form. Note that the IE behavior
136 // | // of returning true or false will have no effect here
137 // | // since our handler is connect()ed to the built-in
138 // | // onsubmit behavior and so we need to use
139 // | // dojo.stopEvent() to ensure that the submission
140 // | // doesn't proceed.
141 // | dojo.stopEvent(e);
143 // | // submit the form with Ajax
144 // | dojo.xhrPost({ form: "formId" });
149 // Style is s special case: Only set with an object hash of styles
150 // | dojo.setAttr("someNode",{
153 // | width:"200px", height:"100px", color:"#000"
158 // Again, only set style as an object hash of styles:
159 // | var obj = { color:"#fff", backgroundColor:"#000" };
160 // | dojo.setAttr("someNode", "style", obj);
162 // | // though shorter to use `dojo.style()` in this case:
163 // | dojo.setStyle("someNode", obj);
165 node = dom.byId(node);
166 if(arguments.length == 2){ // inline'd type check
167 // the object form of setter: the 2nd argument is a dictionary
169 exports.set(node, x, name[x]);
171 return node; // DomNode
173 var lc = name.toLowerCase(),
174 propName = prop.names[lc] || name,
175 forceProp = forcePropNames[propName];
176 if(propName == "style" && typeof value != "string"){ // inline'd type check
177 // special case: setting a style
178 style.set(node, value);
179 return node; // DomNode
181 if(forceProp || typeof value == "boolean" || lang.isFunction(value)){
182 return prop.set(node, name, value);
185 node.setAttribute(attrNames[lc] || name, value);
186 return node; // DomNode
189 exports.remove = function removeAttr(/*DOMNode|String*/ node, /*String*/ name){
191 // Removes an attribute from an HTML element.
192 // node: DOMNode|String
193 // id or reference to the element to remove the attribute from
195 // the name of the attribute to remove
197 dom.byId(node).removeAttribute(attrNames[name.toLowerCase()] || name);
200 exports.getNodeProp = function getNodeProp(/*DomNode|String*/ node, /*String*/ name){
202 // Returns an effective value of a property or an attribute.
203 // node: DOMNode|String
204 // id or reference to the element to remove the attribute from
206 // the name of the attribute
208 // the value of the attribute
210 node = dom.byId(node);
211 var lc = name.toLowerCase(), propName = prop.names[lc] || name;
212 if((propName in node) && propName != "href"){
214 return node[propName]; // Anything
217 var attrName = attrNames[lc] || name;
218 return _hasAttr(node, attrName) ? node.getAttribute(attrName) : null; // Anything