dom.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. if (!dojo._hasResource["dojox.data.dom"]) { // _hasResource checks added by
  2. // build. Do not use _hasResource
  3. // directly in your code.
  4. dojo._hasResource["dojox.data.dom"] = true;
  5. dojo.provide("dojox.data.dom");
  6. // DOM type to int value for reference.
  7. // Ints make for more compact code than full constant names.
  8. // ELEMENT_NODE = 1;
  9. // ATTRIBUTE_NODE = 2;
  10. // TEXT_NODE = 3;
  11. // CDATA_SECTION_NODE = 4;
  12. // ENTITY_REFERENCE_NODE = 5;
  13. // ENTITY_NODE = 6;
  14. // PROCESSING_INSTRUCTION_NODE = 7;
  15. // COMMENT_NODE = 8;
  16. // DOCUMENT_NODE = 9;
  17. // DOCUMENT_TYPE_NODE = 10;
  18. // DOCUMENT_FRAGMENT_NODE = 11;
  19. // NOTATION_NODE = 12;
  20. // FIXME: Remove this file when possible.
  21. // This file contains internal/helper APIs as holders until the true DOM
  22. // apis of Dojo 0.9 are finalized.
  23. // Therefore, these should not be generally used, they are present only for
  24. // the use by XmlStore and the
  25. // wires project until proper dojo replacements are available. When such
  26. // exist, XmlStore and the like
  27. // will be ported off these and this file will be deleted.
  28. dojo.experimental("dojox.data.dom");
  29. dojox.data.dom.createDocument = function(/* string? */str, /* string? */
  30. mimetype) {
  31. // summary:
  32. // cross-browser implementation of creating an XML document object.
  33. //
  34. // str:
  35. // Optional text to create the document from. If not provided, an empty
  36. // XML document will be created.
  37. // mimetype:
  38. // Optional mimetype of the text. Typically, this is text/xml. Will be
  39. // defaulted to text/xml if not provided.
  40. var _document = dojo.doc;
  41. if (!mimetype) {
  42. mimetype = "text/xml";
  43. }
  44. if (str && (typeof dojo.global["DOMParser"]) !== "undefined") {
  45. var parser = new DOMParser();
  46. return parser.parseFromString(str, mimetype); // DOMDocument
  47. } else if ((typeof dojo.global["ActiveXObject"]) !== "undefined") {
  48. var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
  49. for (var i = 0; i < prefixes.length; i++) {
  50. try {
  51. var doc = new ActiveXObject(prefixes[i] + ".XMLDOM");
  52. if (str) {
  53. if (doc) {
  54. doc.async = false;
  55. doc.loadXML(str);
  56. return doc; // DOMDocument
  57. } else {
  58. console.log("loadXML didn't work?");
  59. }
  60. } else {
  61. if (doc) {
  62. return doc; // DOMDocument
  63. }
  64. }
  65. } catch (e) { /* squelch */
  66. };
  67. }
  68. } else if ((_document.implementation)
  69. && (_document.implementation.createDocument)) {
  70. if (str) {
  71. if (_document.createElement) {
  72. // FIXME: this may change all tags to uppercase!
  73. var tmp = _document.createElement("xml");
  74. tmp.innerHTML = str;
  75. var xmlDoc = _document.implementation.createDocument("foo",
  76. "", null);
  77. for (var i = 0; i < tmp.childNodes.length; i++) {
  78. xmlDoc.importNode(tmp.childNodes.item(i), true);
  79. }
  80. return xmlDoc; // DOMDocument
  81. }
  82. } else {
  83. return _document.implementation.createDocument("", "", null); // DOMDocument
  84. }
  85. }
  86. return null; // DOMDocument
  87. }
  88. dojox.data.dom.textContent = function(/* Node */node, /* string? */text) {
  89. // summary:
  90. // Implementation of the DOM Level 3 attribute; scan node for text
  91. // description:
  92. // Implementation of the DOM Level 3 attribute; scan node for text
  93. // This function can also update the text of a node by replacing all
  94. // child
  95. // content of the node.
  96. // node:
  97. // The node to get the text off of or set the text on.
  98. // text:
  99. // Optional argument of the text to apply to the node.
  100. if (arguments.length > 1) {
  101. var _document = node.ownerDocument || dojo.doc; // Preference is to
  102. // get the node
  103. // owning doc first
  104. // or it may fail
  105. dojox.data.dom
  106. .replaceChildren(node, _document.createTextNode(text));
  107. return text; // string
  108. } else {
  109. if (node.textContent !== undefined) { // FF 1.5
  110. return node.textContent; // string
  111. }
  112. var _result = "";
  113. if (node == null) {
  114. return _result; // empty string.
  115. }
  116. for (var i = 0; i < node.childNodes.length; i++) {
  117. switch (node.childNodes[i].nodeType) {
  118. case 1 : // ELEMENT_NODE
  119. case 5 : // ENTITY_REFERENCE_NODE
  120. _result += dojox.data.dom
  121. .textContent(node.childNodes[i]);
  122. break;
  123. case 3 : // TEXT_NODE
  124. case 2 : // ATTRIBUTE_NODE
  125. case 4 : // CDATA_SECTION_NODE
  126. _result += node.childNodes[i].nodeValue;
  127. break;
  128. default :
  129. break;
  130. }
  131. }
  132. return _result; // string
  133. }
  134. }
  135. dojox.data.dom.replaceChildren = function(/* Element */node, /* Node || array */
  136. newChildren) {
  137. // summary:
  138. // Removes all children of node and appends newChild. All the existing
  139. // children will be destroyed.
  140. // description:
  141. // Removes all children of node and appends newChild. All the existing
  142. // children will be destroyed.
  143. // node:
  144. // The node to modify the children on
  145. // newChildren:
  146. // The children to add to the node. It can either be a single Node or an
  147. // array of Nodes.
  148. var nodes = [];
  149. if (dojo.isIE) {
  150. for (var i = 0; i < node.childNodes.length; i++) {
  151. nodes.push(node.childNodes[i]);
  152. }
  153. }
  154. dojox.data.dom.removeChildren(node);
  155. for (var i = 0; i < nodes.length; i++) {
  156. dojo._destroyElement(nodes[i]);
  157. }
  158. if (!dojo.isArray(newChildren)) {
  159. node.appendChild(newChildren);
  160. } else {
  161. for (var i = 0; i < newChildren.length; i++) {
  162. node.appendChild(newChildren[i]);
  163. }
  164. }
  165. }
  166. dojox.data.dom.removeChildren = function(/* Element */node) {
  167. // summary:
  168. // removes all children from node and returns the count of children
  169. // removed.
  170. // The children nodes are not destroyed. Be sure to call
  171. // dojo._destroyElement on them
  172. // after they are not used anymore.
  173. // node:
  174. // The node to remove all the children from.
  175. var count = node.childNodes.length;
  176. while (node.hasChildNodes()) {
  177. node.removeChild(node.firstChild);
  178. }
  179. return count; // int
  180. }
  181. dojox.data.dom.innerXML = function(/* Node */node) {
  182. // summary:
  183. // Implementation of MS's innerXML function.
  184. // node:
  185. // The node from which to generate the XML text representation.
  186. if (node.innerXML) {
  187. return node.innerXML; // string
  188. } else if (node.xml) {
  189. return node.xml; // string
  190. } else if (typeof XMLSerializer != "undefined") {
  191. return (new XMLSerializer()).serializeToString(node); // string
  192. }
  193. }
  194. }