dom.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. define("dojo/dom", ["./_base/sniff", "./_base/lang", "./_base/window"],
  2. function(has, lang, win){
  3. // module:
  4. // dojo/dom
  5. // summary:
  6. // This module defines the core dojo DOM API.
  7. // FIXME: need to add unit tests for all the semi-public methods
  8. try{
  9. document.execCommand("BackgroundImageCache", false, true);
  10. }catch(e){
  11. // sane browsers don't have cache "issues"
  12. }
  13. // =============================
  14. // DOM Functions
  15. // =============================
  16. /*=====
  17. dojo.byId = function(id, doc){
  18. // summary:
  19. // Returns DOM node with matching `id` attribute or `null`
  20. // if not found. If `id` is a DomNode, this function is a no-op.
  21. //
  22. // id: String|DOMNode
  23. // A string to match an HTML id attribute or a reference to a DOM Node
  24. //
  25. // doc: Document?
  26. // Document to work in. Defaults to the current value of
  27. // dojo.doc. Can be used to retrieve
  28. // node references from other documents.
  29. //
  30. // example:
  31. // Look up a node by ID:
  32. // | var n = dojo.byId("foo");
  33. //
  34. // example:
  35. // Check if a node exists, and use it.
  36. // | var n = dojo.byId("bar");
  37. // | if(n){ doStuff() ... }
  38. //
  39. // example:
  40. // Allow string or DomNode references to be passed to a custom function:
  41. // | var foo = function(nodeOrId){
  42. // | nodeOrId = dojo.byId(nodeOrId);
  43. // | // ... more stuff
  44. // | }
  45. =====*/
  46. /*=====
  47. dojo.isDescendant = function(node, ancestor){
  48. // summary:
  49. // Returns true if node is a descendant of ancestor
  50. // node: DOMNode|String
  51. // string id or node reference to test
  52. // ancestor: DOMNode|String
  53. // string id or node reference of potential parent to test against
  54. //
  55. // example:
  56. // Test is node id="bar" is a descendant of node id="foo"
  57. // | if(dojo.isDescendant("bar", "foo")){ ... }
  58. };
  59. =====*/
  60. // TODO: do we need this function in the base?
  61. /*=====
  62. dojo.setSelectable = function(node, selectable){
  63. // summary:
  64. // Enable or disable selection on a node
  65. // node: DOMNode|String
  66. // id or reference to node
  67. // selectable: Boolean
  68. // state to put the node in. false indicates unselectable, true
  69. // allows selection.
  70. // example:
  71. // Make the node id="bar" unselectable
  72. // | dojo.setSelectable("bar");
  73. // example:
  74. // Make the node id="bar" selectable
  75. // | dojo.setSelectable("bar", true);
  76. };
  77. =====*/
  78. var dom = {}; // the result object
  79. if(has("ie")){
  80. dom.byId = function(id, doc){
  81. if(typeof id != "string"){
  82. return id;
  83. }
  84. var _d = doc || win.doc, te = id && _d.getElementById(id);
  85. // attributes.id.value is better than just id in case the
  86. // user has a name=id inside a form
  87. if(te && (te.attributes.id.value == id || te.id == id)){
  88. return te;
  89. }else{
  90. var eles = _d.all[id];
  91. if(!eles || eles.nodeName){
  92. eles = [eles];
  93. }
  94. // if more than 1, choose first with the correct id
  95. var i = 0;
  96. while((te = eles[i++])){
  97. if((te.attributes && te.attributes.id && te.attributes.id.value == id) || te.id == id){
  98. return te;
  99. }
  100. }
  101. }
  102. };
  103. }else{
  104. dom.byId = function(id, doc){
  105. // inline'd type check.
  106. // be sure to return null per documentation, to match IE branch.
  107. return ((typeof id == "string") ? (doc || win.doc).getElementById(id) : id) || null; // DOMNode
  108. };
  109. }
  110. /*=====
  111. };
  112. =====*/
  113. dom.isDescendant = function(/*DOMNode|String*/node, /*DOMNode|String*/ancestor){
  114. try{
  115. node = dom.byId(node);
  116. ancestor = dom.byId(ancestor);
  117. while(node){
  118. if(node == ancestor){
  119. return true; // Boolean
  120. }
  121. node = node.parentNode;
  122. }
  123. }catch(e){ /* squelch, return false */ }
  124. return false; // Boolean
  125. };
  126. // TODO: do we need this function in the base?
  127. // Add feature test for user-select CSS property
  128. // (currently known to work in all but IE < 10 and Opera)
  129. // TODO: The user-select CSS property as of May 2014 is no longer part of
  130. // any CSS specification. In IE, -ms-user-select does not do the same thing
  131. // as the unselectable attribute on elements; namely, dijit Editor buttons
  132. // do not properly prevent the content of the editable content frame from
  133. // unblurring. As a result, the -ms- prefixed version is omitted here.
  134. has.add("css-user-select", function(global, doc, element){
  135. // Avoid exception when dom.js is loaded in non-browser environments
  136. if(!element){ return false; }
  137. var style = element.style;
  138. var prefixes = ["Khtml", "O", "Moz", "Webkit"],
  139. i = prefixes.length,
  140. name = "userSelect",
  141. prefix;
  142. // Iterate prefixes from most to least likely
  143. do{
  144. if(typeof style[name] !== "undefined"){
  145. // Supported; return property name
  146. return name;
  147. }
  148. }while(i-- && (name = prefixes[i] + "UserSelect"));
  149. // Not supported if we didn't return before now
  150. return false;
  151. });
  152. var cssUserSelect = has("css-user-select");
  153. dom.setSelectable = cssUserSelect ? function(node, selectable){
  154. // css-user-select returns a (possibly vendor-prefixed) CSS property name
  155. dom.byId(node).style[cssUserSelect] = selectable ? "" : "none";
  156. } : function(node, selectable){
  157. node = dom.byId(node);
  158. // (IE < 10 / Opera) Fall back to setting/removing the
  159. // unselectable attribute on the element and all its children
  160. var nodes = node.getElementsByTagName("*"),
  161. i = nodes.length;
  162. if(selectable){
  163. node.removeAttribute("unselectable");
  164. while(i--){
  165. nodes[i].removeAttribute("unselectable");
  166. }
  167. }else{
  168. node.setAttribute("unselectable", "on");
  169. while(i--){
  170. nodes[i].setAttribute("unselectable", "on");
  171. }
  172. }
  173. };
  174. /*
  175. */
  176. return dom;
  177. });