1
|
/**
|
2
|
* ====================================================================
|
3
|
* About
|
4
|
* ====================================================================
|
5
|
* Sarissa cross browser XML library - IE .load eulation (deprecated)
|
6
|
* @version 0.9.7.6
|
7
|
* @author: Manos Batsis, mailto: mbatsis at users full stop sourceforge full stop net
|
8
|
*
|
9
|
* This script emulates Internet Explorer's .load method of DOM Document objects.
|
10
|
*
|
11
|
* All functionality in this file is DEPRECATED, XMLHttpRequest objects
|
12
|
* should be used to load XML documents instead
|
13
|
*
|
14
|
* @version 0.9.7.6
|
15
|
* @author: Manos Batsis, mailto: mbatsis at users full stop sourceforge full stop net
|
16
|
* ====================================================================
|
17
|
* Licence
|
18
|
* ====================================================================
|
19
|
* Sarissa is free software distributed under the GNU GPL version 2 (see <a href="gpl.txt">gpl.txt</a>) or higher,
|
20
|
* GNU LGPL version 2.1 (see <a href="lgpl.txt">lgpl.txt</a>) or higher and Apache Software License 2.0 or higher
|
21
|
* (see <a href="asl.txt">asl.txt</a>). This means you can choose one of the three and use that if you like. If
|
22
|
* you make modifications under the ASL, i would appreciate it if you submitted those.
|
23
|
* In case your copy of Sarissa does not include the license texts, you may find
|
24
|
* them online in various formats at <a href="http://www.gnu.org">http://www.gnu.org</a> and
|
25
|
* <a href="http://www.apache.org">http://www.apache.org</a>.
|
26
|
*
|
27
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
28
|
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
29
|
* WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE
|
30
|
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
31
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
32
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
33
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
34
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
35
|
*/
|
36
|
if(!_SARISSA_IS_IE){
|
37
|
if(_SARISSA_HAS_DOM_CREATE_DOCUMENT){
|
38
|
/**
|
39
|
* <p>Ensures the document was loaded correctly, otherwise sets the
|
40
|
* parseError to -1 to indicate something went wrong. Internal use</p>
|
41
|
* @private
|
42
|
*/
|
43
|
Sarissa.__handleLoad__ = function(oDoc){
|
44
|
Sarissa.__setReadyState__(oDoc, 4);
|
45
|
};
|
46
|
function SarissaParseError() {
|
47
|
this.errorCode = 0;
|
48
|
};
|
49
|
/**
|
50
|
* <p>Attached by an event handler to the load event. Internal use.</p>
|
51
|
* @private
|
52
|
*/
|
53
|
_sarissa_XMLDocument_onload = function(){
|
54
|
Sarissa.__handleLoad__(this);
|
55
|
};
|
56
|
/**
|
57
|
* <p>Sets the readyState property of the given DOM Document object.
|
58
|
* Internal use.</p>
|
59
|
* @private
|
60
|
* @argument oDoc the DOM Document object to fire the
|
61
|
* readystatechange event
|
62
|
* @argument iReadyState the number to change the readystate property to
|
63
|
*/
|
64
|
Sarissa.__setReadyState__ = function(oDoc, iReadyState){
|
65
|
oDoc.readyState = iReadyState;
|
66
|
oDoc.readystate = iReadyState;
|
67
|
if (oDoc.onreadystatechange != null && typeof oDoc.onreadystatechange == "function")
|
68
|
oDoc.onreadystatechange();
|
69
|
};
|
70
|
Sarissa.getDomDocument = function(sUri, sName){
|
71
|
|
72
|
var oDoc = document.implementation.createDocument(sUri?sUri:null, sName?sName:null, null);
|
73
|
if(!oDoc.onreadystatechange){
|
74
|
|
75
|
/**
|
76
|
* <p>Emulate IE's onreadystatechange attribute</p>
|
77
|
*/
|
78
|
oDoc.onreadystatechange = null;
|
79
|
};
|
80
|
if(!oDoc.readyState){
|
81
|
/**
|
82
|
* <p>Emulates IE's readyState property, which always gives an integer from 0 to 4:</p>
|
83
|
* <ul><li>1 == LOADING,</li>
|
84
|
* <li>2 == LOADED,</li>
|
85
|
* <li>3 == INTERACTIVE,</li>
|
86
|
* <li>4 == COMPLETED</li></ul>
|
87
|
*/
|
88
|
oDoc.readyState = 0;
|
89
|
};
|
90
|
if(!oDoc.parseError){
|
91
|
oDoc.parseError = new SarissaParseError();
|
92
|
};
|
93
|
oDoc.addEventListener("load", _sarissa_XMLDocument_onload, false);
|
94
|
return oDoc;
|
95
|
};
|
96
|
if(window.XMLDocument){
|
97
|
/**
|
98
|
* <p>Keeps a handle to the original load() method. Internal use and only
|
99
|
* if Mozilla version is lower than 1.4</p>
|
100
|
* @private
|
101
|
*/
|
102
|
XMLDocument.prototype._sarissa_load = XMLDocument.prototype.load;
|
103
|
// NOTE: setting async to false will only work with documents
|
104
|
// called over HTTP (meaning a server), not the local file system,
|
105
|
// unless you are using Moz 1.4+.
|
106
|
|
107
|
/**
|
108
|
* <p>This is deprecated, use XMLHttpRequest to load remote documents instead.
|
109
|
* Overrides the original load method to provide synchronous loading for
|
110
|
* Mozilla versions prior to 1.4 and fix ready state stuff.</p>
|
111
|
* @deprecated
|
112
|
* @returns the DOM Object as it was before the load() call (may be empty)
|
113
|
*/
|
114
|
XMLDocument.prototype.load = function(sURI) {
|
115
|
var oDoc = Sarissa.getDomDocument();
|
116
|
Sarissa.copyChildNodes(this, oDoc);
|
117
|
this.parseError.errorCode = 0;
|
118
|
Sarissa.__setReadyState__(this, 1);
|
119
|
try {
|
120
|
if(this.async == false && _SARISSA_SYNC_NON_IMPLEMENTED) {
|
121
|
var tmp = new XMLHttpRequest();
|
122
|
tmp.open("GET", sURI, false);
|
123
|
tmp.send(null);
|
124
|
Sarissa.__setReadyState__(this, 2);
|
125
|
Sarissa.copyChildNodes(tmp.responseXML, this);
|
126
|
Sarissa.__setReadyState__(this, 3);
|
127
|
}else{
|
128
|
this._sarissa_load(sURI);
|
129
|
};
|
130
|
}
|
131
|
catch (objException) {
|
132
|
oDoc.parseError.errorCode = -1;
|
133
|
}
|
134
|
finally {
|
135
|
if (!oDoc.documentElement || oDoc.documentElement.tagName == "parsererror"){
|
136
|
oDoc.parseError.errorCode = -1;
|
137
|
};
|
138
|
if(this.async == false){
|
139
|
Sarissa.__handleLoad__(this);
|
140
|
};
|
141
|
};
|
142
|
return oDoc;
|
143
|
};
|
144
|
//if(window.XMLDocument) , now mainly for opera
|
145
|
}// TODO: check if the new document has content before trying to copynodes, check for error handling in DOM 3 LS
|
146
|
else if(document.implementation && document.implementation.hasFeature && document.implementation.hasFeature('LS', '3.0')){
|
147
|
Document.prototype.async = true;
|
148
|
Document.prototype.onreadystatechange = null;
|
149
|
Document.prototype.load = function(sURI) {
|
150
|
var oldDoc = Sarissa.getDomDocument();
|
151
|
Sarissa.copyChildNodes(this, oldDoc, false);
|
152
|
var parser = document.implementation.createLSParser(this.async ? document.implementation.MODE_ASYNCHRONOUS : document.implementation.MODE_SYNCHRONOUS, null);
|
153
|
if(this.async){
|
154
|
var self = this;
|
155
|
parser.addEventListener("load",
|
156
|
function(e) {
|
157
|
self.readyState = 4;
|
158
|
Sarissa.copyChildNodes(e.newDocument, self, false);
|
159
|
self.onreadystatechange.call();
|
160
|
},
|
161
|
false);
|
162
|
};
|
163
|
try {
|
164
|
var oDoc = parser.parseURI(sURI);
|
165
|
if(!this.async) {
|
166
|
Sarissa.copyChildNodes(oDoc, this, false);
|
167
|
};
|
168
|
}
|
169
|
catch(e){
|
170
|
this.parseError.errorCode = -1;
|
171
|
};
|
172
|
return oldDoc;
|
173
|
};
|
174
|
/**
|
175
|
* <p>Factory method to obtain a new DOM Document object</p>
|
176
|
* @argument sUri the namespace of the root node (if any)
|
177
|
* @argument sUri the local name of the root node (if any)
|
178
|
* @returns a new DOM Document
|
179
|
*/
|
180
|
Sarissa.getDomDocument = function(sUri, sName){
|
181
|
var oDoc = document.implementation.createDocument(sUri?sUri:null, sName?sName:null, null);
|
182
|
if(!oDoc.parseError){
|
183
|
oDoc.parseError = {errorCode:0};
|
184
|
};
|
185
|
return oDoc;
|
186
|
};
|
187
|
}
|
188
|
else {
|
189
|
Sarissa.getDomDocument = function(sUri, sName){
|
190
|
var oDoc = document.implementation.createDocument(sUri?sUri:null, sName?sName:null, null);
|
191
|
// looks like safari does not create the root element for some unknown reason
|
192
|
if(oDoc && (sUri || sName) && !oDoc.documentElement){
|
193
|
oDoc.appendChild(oDoc.createElementNS(sUri, sName));
|
194
|
};
|
195
|
// attachb to the new object as we have no prototype to use, this is for safari
|
196
|
if(!oDoc.load) {
|
197
|
oDoc.load = function(sUrl) {
|
198
|
var oldDoc = document.implementation.createDocument();
|
199
|
Sarissa.copyChildNodes(this, oldDoc);
|
200
|
this.parseError = {errorCode : 0};
|
201
|
Sarissa.__setReadyState__(this, 1);
|
202
|
if(this.async == false) {
|
203
|
var tmp = new XMLHttpRequest();
|
204
|
tmp.open("GET", sUrl, false);
|
205
|
tmp.send(null);
|
206
|
Sarissa.__setReadyState__(this, 2);
|
207
|
Sarissa.copyChildNodes(tmp.responseXML, oDoc);
|
208
|
if(!oDoc.documentElement || oDoc.getElementsByTagName("parsererror").length >0){
|
209
|
oDoc.parseError.errorCode = -1;
|
210
|
};
|
211
|
Sarissa.__setReadyState__(this, 3);
|
212
|
Sarissa.__setReadyState__(this, 4);
|
213
|
}
|
214
|
else {
|
215
|
var xmlhttp = new XMLHttpRequest();
|
216
|
xmlhttp.open('GET', sUrl, true);
|
217
|
xmlhttp.onreadystatechange = function(){
|
218
|
if (xmlhttp.readyState == 4) {
|
219
|
Sarissa.copyChildNodes(xmlhttp.responseXML, oDoc);
|
220
|
if(!oDoc.documentElement || oDoc.getElementsByTagName("parsererror").length > 0){
|
221
|
oDoc.parseError.errorCode = -1;
|
222
|
};
|
223
|
};
|
224
|
Sarissa.__setReadyState__(oDoc, xmlhttp.readyState);
|
225
|
};
|
226
|
xmlhttp.send(null);
|
227
|
};
|
228
|
return oldDoc;
|
229
|
};
|
230
|
};
|
231
|
return oDoc;
|
232
|
};
|
233
|
};
|
234
|
};//if(_SARISSA_HAS_DOM_CREATE_DOCUMENT)
|
235
|
};
|
236
|
// EOF
|