dom-attr.js 6.7 KB

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