1 if(!dojo._hasResource["dojox.data.dom"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.data.dom"] = true;
3 dojo.provide("dojox.data.dom");
5 //DOM type to int value for reference.
6 //Ints make for more compact code than full constant names.
10 //CDATA_SECTION_NODE = 4;
11 //ENTITY_REFERENCE_NODE = 5;
13 //PROCESSING_INSTRUCTION_NODE = 7;
16 //DOCUMENT_TYPE_NODE = 10;
17 //DOCUMENT_FRAGMENT_NODE = 11;
20 //FIXME: Remove this file when possible.
21 //This file contains internal/helper APIs as holders until the true DOM apis of Dojo 0.9 are finalized.
22 //Therefore, these should not be generally used, they are present only for the use by XmlStore and the
23 //wires project until proper dojo replacements are available. When such exist, XmlStore and the like
24 //will be ported off these and this file will be deleted.
25 dojo.experimental("dojox.data.dom");
27 dojox.data.dom.createDocument = function(/*string?*/ str, /*string?*/ mimetype){
29 // cross-browser implementation of creating an XML document object.
32 // Optional text to create the document from. If not provided, an empty XML document will be created.
34 // Optional mimetype of the text. Typically, this is text/xml. Will be defaulted to text/xml if not provided.
35 var _document = dojo.doc;
37 if(!mimetype){ mimetype = "text/xml"; }
38 if(str && (typeof dojo.global["DOMParser"]) !== "undefined"){
39 var parser = new DOMParser();
40 return parser.parseFromString(str, mimetype); // DOMDocument
41 }else if((typeof dojo.global["ActiveXObject"]) !== "undefined"){
42 var prefixes = [ "MSXML2", "Microsoft", "MSXML", "MSXML3" ];
43 for(var i = 0; i<prefixes.length; i++){
45 var doc = new ActiveXObject(prefixes[i]+".XMLDOM");
50 return doc; // DOMDocument
52 console.log("loadXML didn't work?");
56 return doc; //DOMDocument
59 }catch(e){ /* squelch */ };
61 }else if((_document.implementation)&&
62 (_document.implementation.createDocument)){
64 if(_document.createElement){
65 // FIXME: this may change all tags to uppercase!
66 var tmp = _document.createElement("xml");
68 var xmlDoc = _document.implementation.createDocument("foo", "", null);
69 for(var i = 0; i < tmp.childNodes.length; i++) {
70 xmlDoc.importNode(tmp.childNodes.item(i), true);
72 return xmlDoc; // DOMDocument
75 return _document.implementation.createDocument("", "", null); // DOMDocument
78 return null; // DOMDocument
81 dojox.data.dom.textContent = function(/*Node*/node, /*string?*/text){
83 // Implementation of the DOM Level 3 attribute; scan node for text
85 // Implementation of the DOM Level 3 attribute; scan node for text
86 // This function can also update the text of a node by replacing all child
87 // content of the node.
89 // The node to get the text off of or set the text on.
91 // Optional argument of the text to apply to the node.
92 if(arguments.length>1){
93 var _document = node.ownerDocument || dojo.doc; //Preference is to get the node owning doc first or it may fail
94 dojox.data.dom.replaceChildren(node, _document.createTextNode(text));
95 return text; // string
97 if(node.textContent !== undefined){ //FF 1.5
98 return node.textContent; // string
102 return _result; //empty string.
104 for(var i = 0; i < node.childNodes.length; i++){
105 switch(node.childNodes[i].nodeType){
106 case 1: // ELEMENT_NODE
107 case 5: // ENTITY_REFERENCE_NODE
108 _result += dojox.data.dom.textContent(node.childNodes[i]);
111 case 2: // ATTRIBUTE_NODE
112 case 4: // CDATA_SECTION_NODE
113 _result += node.childNodes[i].nodeValue;
119 return _result; // string
123 dojox.data.dom.replaceChildren = function(/*Element*/node, /*Node || array*/ newChildren){
125 // Removes all children of node and appends newChild. All the existing
126 // children will be destroyed.
128 // Removes all children of node and appends newChild. All the existing
129 // children will be destroyed.
131 // The node to modify the children on
133 // The children to add to the node. It can either be a single Node or an
138 for(var i=0;i<node.childNodes.length;i++){
139 nodes.push(node.childNodes[i]);
143 dojox.data.dom.removeChildren(node);
144 for(var i=0;i<nodes.length;i++){
145 dojo._destroyElement(nodes[i]);
148 if(!dojo.isArray(newChildren)){
149 node.appendChild(newChildren);
151 for(var i=0;i<newChildren.length;i++){
152 node.appendChild(newChildren[i]);
157 dojox.data.dom.removeChildren = function(/*Element*/node){
159 // removes all children from node and returns the count of children removed.
160 // The children nodes are not destroyed. Be sure to call dojo._destroyElement on them
161 // after they are not used anymore.
163 // The node to remove all the children from.
164 var count = node.childNodes.length;
165 while(node.hasChildNodes()){
166 node.removeChild(node.firstChild);
172 dojox.data.dom.innerXML = function(/*Node*/node){
174 // Implementation of MS's innerXML function.
176 // The node from which to generate the XML text representation.
178 return node.innerXML; // string
180 return node.xml; // string
181 }else if(typeof XMLSerializer != "undefined"){
182 return (new XMLSerializer()).serializeToString(node); // string