dom-attr.js 7.6 KB

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