]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/data/dom.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / data / dom.js
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");
4
5 //DOM type to int value for reference.
6 //Ints make for more compact code than full constant names.
7 //ELEMENT_NODE                  = 1;
8 //ATTRIBUTE_NODE                = 2;
9 //TEXT_NODE                     = 3;
10 //CDATA_SECTION_NODE            = 4;
11 //ENTITY_REFERENCE_NODE         = 5;
12 //ENTITY_NODE                   = 6;
13 //PROCESSING_INSTRUCTION_NODE   = 7;
14 //COMMENT_NODE                  = 8;
15 //DOCUMENT_NODE                 = 9;
16 //DOCUMENT_TYPE_NODE            = 10;
17 //DOCUMENT_FRAGMENT_NODE        = 11;
18 //NOTATION_NODE                 = 12;
19
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");
26
27 dojox.data.dom.createDocument = function(/*string?*/ str, /*string?*/ mimetype){
28         //      summary:
29         //              cross-browser implementation of creating an XML document object.
30         //
31         //      str:
32         //              Optional text to create the document from.  If not provided, an empty XML document will be created.
33         //      mimetype:
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;
36
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++){
44                         try{
45                                 var doc = new ActiveXObject(prefixes[i]+".XMLDOM");
46                                 if(str){
47                                         if(doc){
48                                                 doc.async = false;
49                                                 doc.loadXML(str);
50                                                 return doc;     //      DOMDocument
51                                         }else{
52                                                 console.log("loadXML didn't work?");
53                                         }
54                                 }else{
55                                         if(doc){ 
56                                                 return doc; //DOMDocument
57                                         }
58                                 }
59                         }catch(e){ /* squelch */ };
60                 }
61         }else if((_document.implementation)&&
62                 (_document.implementation.createDocument)){
63                 if(str){
64                         if(_document.createElement){
65                                 // FIXME: this may change all tags to uppercase!
66                                 var tmp = _document.createElement("xml");
67                                 tmp.innerHTML = str;
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);
71                                 }
72                                 return xmlDoc;  //      DOMDocument
73                         }
74                 }else{
75                         return _document.implementation.createDocument("", "", null); // DOMDocument
76                 }
77         }
78         return null;    //      DOMDocument
79 }
80
81 dojox.data.dom.textContent = function(/*Node*/node, /*string?*/text){
82         //      summary:
83         //              Implementation of the DOM Level 3 attribute; scan node for text
84         //      description:
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.
88         //      node:
89         //              The node to get the text off of or set the text on.
90         //      text:
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
96         } else {
97                 if(node.textContent !== undefined){ //FF 1.5
98                         return node.textContent;        //      string
99                 }
100                 var _result = "";
101                 if(node == null){
102                         return _result; //empty string.
103                 }
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]);
109                                         break;
110                                 case 3: // TEXT_NODE
111                                 case 2: // ATTRIBUTE_NODE
112                                 case 4: // CDATA_SECTION_NODE
113                                         _result += node.childNodes[i].nodeValue;
114                                         break;
115                                 default:
116                                         break;
117                         }
118                 }
119                 return _result; //      string
120         }
121 }
122
123 dojox.data.dom.replaceChildren = function(/*Element*/node, /*Node || array*/ newChildren){
124         //      summary:
125         //              Removes all children of node and appends newChild. All the existing
126         //              children will be destroyed.
127         //      description:
128         //              Removes all children of node and appends newChild. All the existing
129         //              children will be destroyed.
130         //      node:
131         //              The node to modify the children on
132         //      newChildren:
133         //              The children to add to the node.  It can either be a single Node or an
134         //              array of Nodes.
135         var nodes = [];
136         
137         if(dojo.isIE){
138                 for(var i=0;i<node.childNodes.length;i++){
139                         nodes.push(node.childNodes[i]);
140                 }
141         }
142
143         dojox.data.dom.removeChildren(node);
144         for(var i=0;i<nodes.length;i++){
145                 dojo._destroyElement(nodes[i]);
146         }
147
148         if(!dojo.isArray(newChildren)){
149                 node.appendChild(newChildren);
150         }else{
151                 for(var i=0;i<newChildren.length;i++){
152                         node.appendChild(newChildren[i]);
153                 }
154         }
155 }
156
157 dojox.data.dom.removeChildren = function(/*Element*/node){
158         //      summary:
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.
162         //      node:
163         //              The node to remove all the children from.
164         var count = node.childNodes.length;
165         while(node.hasChildNodes()){
166                 node.removeChild(node.firstChild);
167         }
168         return count; // int
169 }
170
171
172 dojox.data.dom.innerXML = function(/*Node*/node){
173         //      summary:
174         //              Implementation of MS's innerXML function.
175         //      node:
176         //              The node from which to generate the XML text representation.
177         if(node.innerXML){
178                 return node.innerXML;   //      string
179         }else if (node.xml){
180                 return node.xml;                //      string
181         }else if(typeof XMLSerializer != "undefined"){
182                 return (new XMLSerializer()).serializeToString(node);   //      string
183         }
184 }
185
186
187 }