1 /*
  2  * ====================================================================
  3  * About Sarissa: http://dev.abiss.gr/sarissa
  4  * ====================================================================
  5  * Sarissa is an ECMAScript library acting as a cross-browser wrapper for native XML APIs.
  6  * The library supports Gecko based browsers like Mozilla and Firefox,
  7  * Internet Explorer (5.5+ with MSXML3.0+), Konqueror, Safari and Opera
  8  * @version 0.9.9.4
  9  * @author: Copyright 2004-2008 Emmanouil Batsis, mailto: mbatsis at users full stop sourceforge full stop net
 10  * ====================================================================
 11  * Licence
 12  * ====================================================================
 13  * Sarissa is free software distributed under the GNU GPL version 2 (see <a href="gpl.txt">gpl.txt</a>) or higher, 
 14  * GNU LGPL version 2.1 (see <a href="lgpl.txt">lgpl.txt</a>) or higher and Apache Software License 2.0 or higher 
 15  * (see <a href="asl.txt">asl.txt</a>). This means you can choose one of the three and use that if you like. If 
 16  * you make modifications under the ASL, i would appreciate it if you submitted those.
 17  * In case your copy of Sarissa does not include the license texts, you may find
 18  * them online in various formats at <a href="http://www.gnu.org">http://www.gnu.org</a> and 
 19  * <a href="http://www.apache.org">http://www.apache.org</a>.
 20  *
 21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
 22  * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 23  * WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE 
 24  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
 25  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 27  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 28  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29  */
 30 /**
 31  * <p>Sarissa is a utility class. Provides "static" methods for DOMDocument, 
 32  * DOM Node serialization to XML strings and other utility goodies.</p>
 33  * @constructor
 34  * @static
 35  */
 36 function Sarissa(){}
 37 Sarissa.VERSION = "0.9.9.4";
 38 Sarissa.PARSED_OK = "Document contains no parsing errors";
 39 Sarissa.PARSED_EMPTY = "Document is empty";
 40 Sarissa.PARSED_UNKNOWN_ERROR = "Not well-formed or other error";
 41 Sarissa.IS_ENABLED_TRANSFORM_NODE = false;
 42 Sarissa.REMOTE_CALL_FLAG = "gr.abiss.sarissa.REMOTE_CALL_FLAG";
 43 /** @private */
 44 Sarissa._lastUniqueSuffix = 0;
 45 /** @private */
 46 Sarissa._getUniqueSuffix = function(){
 47 	return Sarissa._lastUniqueSuffix++;
 48 };
 49 /** @private */
 50 Sarissa._SARISSA_IEPREFIX4XSLPARAM = "";
 51 /** @private */
 52 Sarissa._SARISSA_HAS_DOM_IMPLEMENTATION = document.implementation && true;
 53 /** @private */
 54 Sarissa._SARISSA_HAS_DOM_CREATE_DOCUMENT = Sarissa._SARISSA_HAS_DOM_IMPLEMENTATION && document.implementation.createDocument;
 55 /** @private */
 56 Sarissa._SARISSA_HAS_DOM_FEATURE = Sarissa._SARISSA_HAS_DOM_IMPLEMENTATION && document.implementation.hasFeature;
 57 /** @private */
 58 Sarissa._SARISSA_IS_MOZ = Sarissa._SARISSA_HAS_DOM_CREATE_DOCUMENT && Sarissa._SARISSA_HAS_DOM_FEATURE;
 59 /** @private */
 60 Sarissa._SARISSA_IS_SAFARI = navigator.userAgent.toLowerCase().indexOf("safari") != -1 || navigator.userAgent.toLowerCase().indexOf("konqueror") != -1;
 61 /** @private */
 62 Sarissa._SARISSA_IS_SAFARI_OLD = Sarissa._SARISSA_IS_SAFARI && (parseInt((navigator.userAgent.match(/AppleWebKit\/(\d+)/)||{})[1], 10) < 420);
 63 /** @private */
 64 Sarissa._SARISSA_IS_IE = document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1;
 65 /** @private */
 66 Sarissa._SARISSA_IS_OPERA = navigator.userAgent.toLowerCase().indexOf("opera") != -1;
 67 if(!window.Node || !Node.ELEMENT_NODE){
 68     Node = {ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, ENTITY_REFERENCE_NODE: 5,  ENTITY_NODE: 6, PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10, DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12};
 69 }
 70 
 71 //This breaks for(x in o) loops in the old Safari
 72 if(Sarissa._SARISSA_IS_SAFARI_OLD){
 73 	HTMLHtmlElement = document.createElement("html").constructor;
 74 	Node = HTMLElement = {};
 75 	HTMLElement.prototype = HTMLHtmlElement.__proto__.__proto__;
 76 	HTMLDocument = Document = document.constructor;
 77 	var x = new DOMParser();
 78 	XMLDocument = x.constructor;
 79 	Element = x.parseFromString("<Single />", "text/xml").documentElement.constructor;
 80 	x = null;
 81 }
 82 if(typeof XMLDocument == "undefined" && typeof Document !="undefined"){ XMLDocument = Document; } 
 83 
 84 // IE initialization
 85 if(Sarissa._SARISSA_IS_IE){
 86     // for XSLT parameter names, prefix needed by IE
 87     Sarissa._SARISSA_IEPREFIX4XSLPARAM = "xsl:";
 88     // used to store the most recent ProgID available out of the above
 89     var _SARISSA_DOM_PROGID = "";
 90     var _SARISSA_XMLHTTP_PROGID = "";
 91     var _SARISSA_DOM_XMLWRITER = "";
 92     /**
 93      * Called when the sarissa.js file is parsed, to pick most recent
 94      * ProgIDs for IE, then gets destroyed.
 95      * @memberOf Sarissa
 96      * @private
 97      * @param idList an array of MSXML PROGIDs from which the most recent will be picked for a given object
 98      * @param enabledList an array of arrays where each array has two items; the index of the PROGID for which a certain feature is enabled
 99      */
100     Sarissa.pickRecentProgID = function (idList){
101         // found progID flag
102         var bFound = false, e;
103         var o2Store;
104         for(var i=0; i < idList.length && !bFound; i++){
105             try{
106                 var oDoc = new ActiveXObject(idList[i]);
107                 o2Store = idList[i];
108                 bFound = true;
109             }catch (objException){
110                 // trap; try next progID
111                 e = objException;
112             }
113         }
114         if (!bFound) {
115             throw "Could not retrieve a valid progID of Class: " + idList[idList.length-1]+". (original exception: "+e+")";
116         }
117         idList = null;
118         return o2Store;
119     };
120     // pick best available MSXML progIDs
121     _SARISSA_DOM_PROGID = null;
122     _SARISSA_THREADEDDOM_PROGID = null;
123     _SARISSA_XSLTEMPLATE_PROGID = null;
124     _SARISSA_XMLHTTP_PROGID = null;
125     // commenting the condition out; we need to redefine XMLHttpRequest 
126     // anyway as IE7 hardcodes it to MSXML3.0 causing version problems 
127     // between different activex controls 
128     //if(!window.XMLHttpRequest){
129     /**
130      * Emulate XMLHttpRequest
131      * @constructor
132      */
133     XMLHttpRequest = function() {
134         if(!_SARISSA_XMLHTTP_PROGID){
135             _SARISSA_XMLHTTP_PROGID = Sarissa.pickRecentProgID(["Msxml2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]);
136         }
137         return new ActiveXObject(_SARISSA_XMLHTTP_PROGID);
138     };
139     //}
140     // we dont need this anymore
141     //============================================
142     // Factory methods (IE)
143     //============================================
144     // see non-IE version
145     Sarissa.getDomDocument = function(sUri, sName){
146         if(!_SARISSA_DOM_PROGID){
147             _SARISSA_DOM_PROGID = Sarissa.pickRecentProgID(["Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"]);
148         }
149         var oDoc = new ActiveXObject(_SARISSA_DOM_PROGID);
150         // if a root tag name was provided, we need to load it in the DOM object
151         if (sName){
152             // create an artifical namespace prefix 
153             // or reuse existing prefix if applicable
154             var prefix = "";
155             if(sUri){
156                 if(sName.indexOf(":") > 1){
157                     prefix = sName.substring(0, sName.indexOf(":"));
158                     sName = sName.substring(sName.indexOf(":")+1); 
159                 }else{
160                     prefix = "a" + Sarissa._getUniqueSuffix();
161                 }
162             }
163             // use namespaces if a namespace URI exists
164             if(sUri){
165                 oDoc.loadXML('<' + prefix+':'+sName + " xmlns:" + prefix + "=\"" + sUri + "\"" + " />");
166             } else {
167                 oDoc.loadXML('<' + sName + " />");
168             }
169         }
170         return oDoc;
171     };
172     // see non-IE version   
173     Sarissa.getParseErrorText = function (oDoc) {
174         var parseErrorText = Sarissa.PARSED_OK;
175         if(oDoc && oDoc.parseError && oDoc.parseError.errorCode && oDoc.parseError.errorCode != 0){
176             parseErrorText = "XML Parsing Error: " + oDoc.parseError.reason + 
177                 "\nLocation: " + oDoc.parseError.url + 
178                 "\nLine Number " + oDoc.parseError.line + ", Column " + 
179                 oDoc.parseError.linepos + 
180                 ":\n" + oDoc.parseError.srcText +
181                 "\n";
182             for(var i = 0;  i < oDoc.parseError.linepos;i++){
183                 parseErrorText += "-";
184             }
185             parseErrorText +=  "^\n";
186         }
187         else if(oDoc.documentElement === null){
188             parseErrorText = Sarissa.PARSED_EMPTY;
189         }
190         return parseErrorText;
191     };
192     // see non-IE version
193     Sarissa.setXpathNamespaces = function(oDoc, sNsSet) {
194         oDoc.setProperty("SelectionLanguage", "XPath");
195         oDoc.setProperty("SelectionNamespaces", sNsSet);
196     };
197     /**
198      * A class that reuses the same XSLT stylesheet for multiple transforms.
199      * @constructor
200      */
201     XSLTProcessor = function(){
202         if(!_SARISSA_XSLTEMPLATE_PROGID){
203             _SARISSA_XSLTEMPLATE_PROGID = Sarissa.pickRecentProgID(["Msxml2.XSLTemplate.6.0", "MSXML2.XSLTemplate.3.0"]);
204         }
205         this.template = new ActiveXObject(_SARISSA_XSLTEMPLATE_PROGID);
206         this.processor = null;
207     };
208     /**
209      * Imports the given XSLT DOM and compiles it to a reusable transform
210      * <b>Note:</b> If the stylesheet was loaded from a URL and contains xsl:import or xsl:include elements,it will be reloaded to resolve those
211      * @param {DOMDocument} xslDoc The XSLT DOMDocument to import
212      */
213     XSLTProcessor.prototype.importStylesheet = function(xslDoc){
214         if(!_SARISSA_THREADEDDOM_PROGID){
215             _SARISSA_THREADEDDOM_PROGID = Sarissa.pickRecentProgID(["MSXML2.FreeThreadedDOMDocument.6.0", "MSXML2.FreeThreadedDOMDocument.3.0"]);
216         }
217         xslDoc.setProperty("SelectionLanguage", "XPath");
218         xslDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
219         // convert stylesheet to free threaded
220         var converted = new ActiveXObject(_SARISSA_THREADEDDOM_PROGID);
221         // make included/imported stylesheets work if exist and xsl was originally loaded from url
222         try{
223             converted.resolveExternals = true; 
224             converted.setProperty("AllowDocumentFunction", true); 
225         }
226         catch(e){
227             // Ignore. "AllowDocumentFunction" is only supported in MSXML 3.0 SP4 and later.
228         } 
229         if(xslDoc.url && xslDoc.selectSingleNode("//xsl:*[local-name() = 'import' or local-name() = 'include']") != null){
230             converted.async = false;
231             converted.load(xslDoc.url);
232         } 
233         else {
234             converted.loadXML(xslDoc.xml);
235         }
236         converted.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
237         var output = converted.selectSingleNode("//xsl:output");
238         //this.outputMethod = output ? output.getAttribute("method") : "html";
239         if(output) {
240             this.outputMethod = output.getAttribute("method");
241         } 
242         else {
243             delete this.outputMethod;
244         } 
245         this.template.stylesheet = converted;
246         this.processor = this.template.createProcessor();
247         // for getParameter and clearParameters
248         this.paramsSet = [];
249     };
250 
251     /**
252      * Transform the given XML DOM and return the transformation result as a new DOM document
253      * @param {DOMDocument} sourceDoc The XML DOMDocument to transform
254      * @return {DOMDocument} The transformation result as a DOM Document
255      */
256     XSLTProcessor.prototype.transformToDocument = function(sourceDoc){
257         // fix for bug 1549749
258         var outDoc;
259         if(_SARISSA_THREADEDDOM_PROGID){
260             this.processor.input=sourceDoc;
261             outDoc=new ActiveXObject(_SARISSA_DOM_PROGID);
262             this.processor.output=outDoc;
263             this.processor.transform();
264             return outDoc;
265         }
266         else{
267             if(!_SARISSA_DOM_XMLWRITER){
268                 _SARISSA_DOM_XMLWRITER = Sarissa.pickRecentProgID(["Msxml2.MXXMLWriter.6.0", "Msxml2.MXXMLWriter.3.0", "MSXML2.MXXMLWriter", "MSXML.MXXMLWriter", "Microsoft.XMLDOM"]);
269             }
270             this.processor.input = sourceDoc;
271             outDoc = new ActiveXObject(_SARISSA_DOM_XMLWRITER);
272             this.processor.output = outDoc; 
273             this.processor.transform();
274             var oDoc = new ActiveXObject(_SARISSA_DOM_PROGID);
275             oDoc.loadXML(outDoc.output+"");
276             return oDoc;
277         }
278     };
279     
280     /**
281      * Transform the given XML DOM and return the transformation result as a new DOM fragment.
282      * <b>Note</b>: The xsl:output method must match the nature of the owner document (XML/HTML).
283      * @param {DOMDocument} sourceDoc The XML DOMDocument to transform
284      * @param {DOMDocument} ownerDoc The owner of the result fragment
285      * @return {DOMDocument} The transformation result as a DOM Document
286      */
287     XSLTProcessor.prototype.transformToFragment = function (sourceDoc, ownerDoc) {
288         this.processor.input = sourceDoc;
289         this.processor.transform();
290         var s = this.processor.output;
291         var f = ownerDoc.createDocumentFragment();
292         var container;
293         if (this.outputMethod == 'text') {
294             f.appendChild(ownerDoc.createTextNode(s));
295         } else if (ownerDoc.body && ownerDoc.body.innerHTML) {
296             container = ownerDoc.createElement('div');
297             container.innerHTML = s;
298             while (container.hasChildNodes()) {
299                 f.appendChild(container.firstChild);
300             }
301         }
302         else {
303             var oDoc = new ActiveXObject(_SARISSA_DOM_PROGID);
304             if (s.substring(0, 5) == '<?xml') {
305                 s = s.substring(s.indexOf('?>') + 2);
306             }
307             var xml = ''.concat('<my>', s, '</my>');
308             oDoc.loadXML(xml);
309             container = oDoc.documentElement;
310             while (container.hasChildNodes()) {
311                 f.appendChild(container.firstChild);
312             }
313         }
314         return f;
315     };
316     
317     /**
318      * Set global XSLT parameter of the imported stylesheet
319      * @param {String} nsURI The parameter namespace URI
320      * @param {String} name The parameter base name
321      * @param {String} value The new parameter value
322      */
323      XSLTProcessor.prototype.setParameter = function(nsURI, name, value){
324          // make value a zero length string if null to allow clearing
325          value = value ? value : "";
326          // nsURI is optional but cannot be null
327          if(nsURI){
328              this.processor.addParameter(name, value, nsURI);
329          }else{
330              this.processor.addParameter(name, value);
331          }
332          // update updated params for getParameter
333          nsURI = "" + (nsURI || "");
334          if(!this.paramsSet[nsURI]){
335              this.paramsSet[nsURI] = [];
336          }
337          this.paramsSet[nsURI][name] = value;
338      };
339     /**
340      * Gets a parameter if previously set by setParameter. Returns null
341      * otherwise
342      * @param {String} name The parameter base name
343      * @param {String} value The new parameter value
344      * @return {String} The parameter value if reviously set by setParameter, null otherwise
345      */
346     XSLTProcessor.prototype.getParameter = function(nsURI, name){
347         nsURI = "" + (nsURI || "");
348         if(this.paramsSet[nsURI] && this.paramsSet[nsURI][name]){
349             return this.paramsSet[nsURI][name];
350         }else{
351             return null;
352         }
353     };
354     
355     /**
356      * Clear parameters (set them to default values as defined in the stylesheet itself)
357      */
358     XSLTProcessor.prototype.clearParameters = function(){
359         for(var nsURI in this.paramsSet){
360             for(var name in this.paramsSet[nsURI]){
361                 if(nsURI!=""){
362                     this.processor.addParameter(name, "", nsURI);
363                 }else{
364                     this.processor.addParameter(name, "");
365                 }
366             }
367         }
368         this.paramsSet = [];
369     };
370 }else{ /* end IE initialization, try to deal with real browsers now ;-) */
371     if(Sarissa._SARISSA_HAS_DOM_CREATE_DOCUMENT){
372         /**
373          * <p>Ensures the document was loaded correctly, otherwise sets the
374          * parseError to -1 to indicate something went wrong. Internal use</p>
375          * @private
376          */
377         Sarissa.__handleLoad__ = function(oDoc){
378             Sarissa.__setReadyState__(oDoc, 4);
379         };
380         /**
381         * <p>Attached by an event handler to the load event. Internal use.</p>
382         * @private
383         */
384         _sarissa_XMLDocument_onload = function(){
385             Sarissa.__handleLoad__(this);
386         };
387         /**
388          * <p>Sets the readyState property of the given DOM Document object.
389          * Internal use.</p>
390          * @memberOf Sarissa
391          * @private
392          * @param oDoc the DOM Document object to fire the
393          *          readystatechange event
394          * @param iReadyState the number to change the readystate property to
395          */
396         Sarissa.__setReadyState__ = function(oDoc, iReadyState){
397             oDoc.readyState = iReadyState;
398             oDoc.readystate = iReadyState;
399             if (oDoc.onreadystatechange != null && typeof oDoc.onreadystatechange == "function") {
400                 oDoc.onreadystatechange();
401             }
402         };
403         
404         Sarissa.getDomDocument = function(sUri, sName){
405             var oDoc = document.implementation.createDocument(sUri?sUri:null, sName?sName:null, null);
406             if(!oDoc.onreadystatechange){
407             
408                 /**
409                 * <p>Emulate IE's onreadystatechange attribute</p>
410                 */
411                 oDoc.onreadystatechange = null;
412             }
413             if(!oDoc.readyState){
414                 /**
415                 * <p>Emulates IE's readyState property, which always gives an integer from 0 to 4:</p>
416                 * <ul><li>1 == LOADING,</li>
417                 * <li>2 == LOADED,</li>
418                 * <li>3 == INTERACTIVE,</li>
419                 * <li>4 == COMPLETED</li></ul>
420                 */
421                 oDoc.readyState = 0;
422             }
423             oDoc.addEventListener("load", _sarissa_XMLDocument_onload, false);
424             return oDoc;
425         };
426         if(window.XMLDocument){
427             // do nothing
428         }// TODO: check if the new document has content before trying to copynodes, check  for error handling in DOM 3 LS
429         else if(Sarissa._SARISSA_HAS_DOM_FEATURE && window.Document && !Document.prototype.load && document.implementation.hasFeature('LS', '3.0')){
430     		//Opera 9 may get the XPath branch which gives creates XMLDocument, therefore it doesn't reach here which is good
431             /**
432             * <p>Factory method to obtain a new DOM Document object</p>
433             * @memberOf Sarissa
434             * @param {String} sUri the namespace of the root node (if any)
435             * @param {String} sUri the local name of the root node (if any)
436             * @returns {DOMDOcument} a new DOM Document
437             */
438             Sarissa.getDomDocument = function(sUri, sName){
439                 var oDoc = document.implementation.createDocument(sUri?sUri:null, sName?sName:null, null);
440                 return oDoc;
441             };
442         }
443         else {
444             Sarissa.getDomDocument = function(sUri, sName){
445                 var oDoc = document.implementation.createDocument(sUri?sUri:null, sName?sName:null, null);
446                 // looks like safari does not create the root element for some unknown reason
447                 if(oDoc && (sUri || sName) && !oDoc.documentElement){
448                     oDoc.appendChild(oDoc.createElementNS(sUri, sName));
449                 }
450                 return oDoc;
451             };
452         }
453     }//if(Sarissa._SARISSA_HAS_DOM_CREATE_DOCUMENT)
454 }
455 //==========================================
456 // Common stuff
457 //==========================================
458 if(!window.DOMParser){
459     if(Sarissa._SARISSA_IS_SAFARI){
460         /**
461          * DOMParser is a utility class, used to construct DOMDocuments from XML strings
462          * @constructor
463          */
464         DOMParser = function() { };
465         /** 
466         * Construct a new DOM Document from the given XMLstring
467         * @param {String} sXml the given XML string
468         * @param {String} contentType the content type of the document the given string represents (one of text/xml, application/xml, application/xhtml+xml). 
469         * @return {DOMDocument} a new DOM Document from the given XML string
470         */
471         DOMParser.prototype.parseFromString = function(sXml, contentType){
472             var xmlhttp = new XMLHttpRequest();
473             xmlhttp.open("GET", "data:text/xml;charset=utf-8," + encodeURIComponent(sXml), false);
474             xmlhttp.send(null);
475             return xmlhttp.responseXML;
476         };
477     }else if(Sarissa.getDomDocument && Sarissa.getDomDocument() && Sarissa.getDomDocument(null, "bar").xml){
478         DOMParser = function() { };
479         DOMParser.prototype.parseFromString = function(sXml, contentType){
480             var doc = Sarissa.getDomDocument();
481             doc.loadXML(sXml);
482             return doc;
483         };
484     }
485 }
486 
487 if((typeof(document.importNode) == "undefined") && Sarissa._SARISSA_IS_IE){
488     try{
489         /**
490         * Implementation of importNode for the context window document in IE.
491         * If <code>oNode</code> is a TextNode, <code>bChildren</code> is ignored.
492         * @param {DOMNode} oNode the Node to import
493         * @param {boolean} bChildren whether to include the children of oNode
494         * @returns the imported node for further use
495         */
496         document.importNode = function(oNode, bChildren){
497             var tmp;
498             if (oNode.nodeName=='#text') {
499                 return document.createTextNode(oNode.data);
500             }
501             else {
502                 if(oNode.nodeName == "tbody" || oNode.nodeName == "tr"){
503                     tmp = document.createElement("table");
504                 }
505                 else if(oNode.nodeName == "td"){
506                     tmp = document.createElement("tr");
507                 }
508                 else if(oNode.nodeName == "option"){
509                     tmp = document.createElement("select");
510                 }
511                 else{
512                     tmp = document.createElement("div");
513                 }
514                 if(bChildren){
515                     tmp.innerHTML = oNode.xml ? oNode.xml : oNode.outerHTML;
516                 }else{
517                     tmp.innerHTML = oNode.xml ? oNode.cloneNode(false).xml : oNode.cloneNode(false).outerHTML;
518                 }
519                 return tmp.getElementsByTagName("*")[0];
520             }
521         };
522     }catch(e){ }
523 }
524 if(!Sarissa.getParseErrorText){
525     /**
526      * <p>Returns a human readable description of the parsing error. Usefull
527      * for debugging. Tip: append the returned error string in a <pre>
528      * element if you want to render it.</p>
529      * <p>Many thanks to Christian Stocker for the initial patch.</p>
530      * @memberOf Sarissa
531      * @param {DOMDocument} oDoc The target DOM document
532      * @returns {String} The parsing error description of the target Document in
533      *          human readable form (preformated text)
534      */
535     Sarissa.getParseErrorText = function (oDoc){
536         var parseErrorText = Sarissa.PARSED_OK;
537         if((!oDoc) || (!oDoc.documentElement)){
538             parseErrorText = Sarissa.PARSED_EMPTY;
539         } else if(oDoc.documentElement.tagName == "parsererror"){
540             parseErrorText = oDoc.documentElement.firstChild.data;
541             parseErrorText += "\n" +  oDoc.documentElement.firstChild.nextSibling.firstChild.data;
542         } else if(oDoc.getElementsByTagName("parsererror").length > 0){
543             var parsererror = oDoc.getElementsByTagName("parsererror")[0];
544             parseErrorText = Sarissa.getText(parsererror, true)+"\n";
545         } else if(oDoc.parseError && oDoc.parseError.errorCode != 0){
546             parseErrorText = Sarissa.PARSED_UNKNOWN_ERROR;
547         }
548         return parseErrorText;
549     };
550 }
551 /**
552  * Get a string with the concatenated values of all string nodes under the given node
553  * @param {DOMNode} oNode the given DOM node
554  * @param {boolean} deep whether to recursively scan the children nodes of the given node for text as well. Default is <code>false</code>
555  * @memberOf Sarissa 
556  */
557 Sarissa.getText = function(oNode, deep){
558     var s = "";
559     var nodes = oNode.childNodes;
560     for(var i=0; i < nodes.length; i++){
561         var node = nodes[i];
562         var nodeType = node.nodeType;
563         if(nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE){
564             s += node.data;
565         } else if(deep === true && (nodeType == Node.ELEMENT_NODE || nodeType == Node.DOCUMENT_NODE || nodeType == Node.DOCUMENT_FRAGMENT_NODE)){
566             s += Sarissa.getText(node, true);
567         }
568     }
569     return s;
570 };
571 if(!window.XMLSerializer && Sarissa.getDomDocument && Sarissa.getDomDocument("","foo", null).xml){
572     /**
573      * Utility class to serialize DOM Node objects to XML strings
574      * @constructor
575      */
576     XMLSerializer = function(){};
577     /**
578      * Serialize the given DOM Node to an XML string
579      * @param {DOMNode} oNode the DOM Node to serialize
580      */
581     XMLSerializer.prototype.serializeToString = function(oNode) {
582         return oNode.xml;
583     };
584 }
585 
586 /**
587  * Strips tags from the given markup string. If the given string is 
588  * <code>undefined</code>, <code>null</code> or empty, it is returned as is. 
589  * @memberOf Sarissa
590  * @param {String} s the string to strip the tags from
591  */
592 Sarissa.stripTags = function (s) {
593     return s?s.replace(/<[^>]+>/g,""):s;
594 };
595 /**
596  * <p>Deletes all child nodes of the given node</p>
597  * @memberOf Sarissa
598  * @param {DOMNode} oNode the Node to empty
599  */
600 Sarissa.clearChildNodes = function(oNode) {
601     // need to check for firstChild due to opera 8 bug with hasChildNodes
602     while(oNode.firstChild) {
603         oNode.removeChild(oNode.firstChild);
604     }
605 };
606 /**
607  * <p> Copies the childNodes of nodeFrom to nodeTo</p>
608  * <p> <b>Note:</b> The second object's original content is deleted before 
609  * the copy operation, unless you supply a true third parameter</p>
610  * @memberOf Sarissa
611  * @param {DOMNode} nodeFrom the Node to copy the childNodes from
612  * @param {DOMNode} nodeTo the Node to copy the childNodes to
613  * @param {boolean} bPreserveExisting whether to preserve the original content of nodeTo, default is false
614  */
615 Sarissa.copyChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) {
616     if(Sarissa._SARISSA_IS_SAFARI && nodeTo.nodeType == Node.DOCUMENT_NODE){ // SAFARI_OLD ??
617     	nodeTo = nodeTo.documentElement; //Apparently there's a bug in safari where you can't appendChild to a document node
618     }
619     
620     if((!nodeFrom) || (!nodeTo)){
621         throw "Both source and destination nodes must be provided";
622     }
623     if(!bPreserveExisting){
624         Sarissa.clearChildNodes(nodeTo);
625     }
626     var ownerDoc = nodeTo.nodeType == Node.DOCUMENT_NODE ? nodeTo : nodeTo.ownerDocument;
627     var nodes = nodeFrom.childNodes;
628     var i;
629     if(typeof(ownerDoc.importNode) != "undefined")  {
630         for(i=0;i < nodes.length;i++) {
631             nodeTo.appendChild(ownerDoc.importNode(nodes[i], true));
632         }
633     } else {
634         for(i=0;i < nodes.length;i++) {
635             nodeTo.appendChild(nodes[i].cloneNode(true));
636         }
637     }
638 };
639 
640 /**
641  * <p> Moves the childNodes of nodeFrom to nodeTo</p>
642  * <p> <b>Note:</b> The second object's original content is deleted before 
643  * the move operation, unless you supply a true third parameter</p>
644  * @memberOf Sarissa
645  * @param {DOMNode} nodeFrom the Node to copy the childNodes from
646  * @param {DOMNode} nodeTo the Node to copy the childNodes to
647  * @param {boolean} bPreserveExisting whether to preserve the original content of nodeTo, default is
648  */ 
649 Sarissa.moveChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) {
650     if((!nodeFrom) || (!nodeTo)){
651         throw "Both source and destination nodes must be provided";
652     }
653     if(!bPreserveExisting){
654         Sarissa.clearChildNodes(nodeTo);
655     }
656     var nodes = nodeFrom.childNodes;
657     // if within the same doc, just move, else copy and delete
658     if(nodeFrom.ownerDocument == nodeTo.ownerDocument){
659         while(nodeFrom.firstChild){
660             nodeTo.appendChild(nodeFrom.firstChild);
661         }
662     } else {
663         var ownerDoc = nodeTo.nodeType == Node.DOCUMENT_NODE ? nodeTo : nodeTo.ownerDocument;
664         var i;
665         if(typeof(ownerDoc.importNode) != "undefined") {
666            for(i=0;i < nodes.length;i++) {
667                nodeTo.appendChild(ownerDoc.importNode(nodes[i], true));
668            }
669         }else{
670            for(i=0;i < nodes.length;i++) {
671                nodeTo.appendChild(nodes[i].cloneNode(true));
672            }
673         }
674         Sarissa.clearChildNodes(nodeFrom);
675     }
676 };
677 
678 /** 
679  * <p>Serialize any <strong>non</strong> DOM object to an XML string. All properties are serialized using the property name
680  * as the XML element name. Array elements are rendered as <code>array-item</code> elements, 
681  * using their index/key as the value of the <code>key</code> attribute.</p>
682  * @memberOf Sarissa
683  * @param {Object} anyObject the object to serialize
684  * @param {String} objectName a name for that object, to be used as the root element name
685  * @return {String} the XML serialization of the given object as a string
686  */
687 Sarissa.xmlize = function(anyObject, objectName, indentSpace){
688     indentSpace = indentSpace?indentSpace:'';
689     var s = indentSpace  + '<' + objectName + '>';
690     var isLeaf = false;
691     if(!(anyObject instanceof Object) || anyObject instanceof Number || anyObject instanceof String || anyObject instanceof Boolean || anyObject instanceof Date){
692         s += Sarissa.escape(""+anyObject);
693         isLeaf = true;
694     }else{
695         s += "\n";
696         var isArrayItem = anyObject instanceof Array;
697         for(var name in anyObject){
698             s += Sarissa.xmlize(anyObject[name], (isArrayItem?"array-item key=\""+name+"\"":name), indentSpace + "   ");
699         }
700         s += indentSpace;
701     }
702     return (s += (objectName.indexOf(' ')!=-1?"</array-item>\n":"</" + objectName + ">\n"));
703 };
704 
705 /** 
706  * Escape the given string chacters that correspond to the five predefined XML entities
707  * @memberOf Sarissa
708  * @param {String} sXml the string to escape
709  */
710 Sarissa.escape = function(sXml){
711     return sXml.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
712 };
713 
714 /** 
715  * Unescape the given string. This turns the occurences of the predefined XML 
716  * entities to become the characters they represent correspond to the five predefined XML entities
717  * @memberOf Sarissa
718  * @param  {String}sXml the string to unescape
719  */
720 Sarissa.unescape = function(sXml){
721     return sXml.replace(/'/g,"'").replace(/"/g,"\"").replace(/>/g,">").replace(/</g,"<").replace(/&/g,"&");
722 };
723 
724 /** @private */
725 Sarissa.updateCursor = function(oTargetElement, sValue) {
726     if(oTargetElement && oTargetElement.style && oTargetElement.style.cursor != undefined ){
727         oTargetElement.style.cursor = sValue;
728     }
729 };
730 
731 /**
732  * Asynchronously update an element with response of a GET request on the given URL.  Passing a configured XSLT 
733  * processor will result in transforming and updating oNode before using it to update oTargetElement.
734  * You can also pass a callback function to be executed when the update is finished. The function will be called as 
735  * <code>functionName(oNode, oTargetElement);</code>
736  * @memberOf Sarissa
737  * @param {String} sFromUrl the URL to make the request to
738  * @param {DOMElement} oTargetElement the element to update
739  * @param {XSLTProcessor} xsltproc (optional) the transformer to use on the returned
740  *                  content before updating the target element with it
741  * @param {Function} callback (optional) a Function object to execute once the update is finished successfuly, called as <code>callback(sFromUrl, oTargetElement)</code>. 
742  *        In case an exception is thrown during execution, the callback is called as called as <code>callback(sFromUrl, oTargetElement, oException)</code>
743  * @param {boolean} skipCache (optional) whether to skip any cache
744  */
745 Sarissa.updateContentFromURI = function(sFromUrl, oTargetElement, xsltproc, callback, skipCache) {
746     try{
747         Sarissa.updateCursor(oTargetElement, "wait");
748         var xmlhttp = new XMLHttpRequest();
749         xmlhttp.open("GET", sFromUrl, true);
750         xmlhttp.onreadystatechange = function() {
751             if (xmlhttp.readyState == 4) {
752             	try{
753             		var oDomDoc = xmlhttp.responseXML;
754 	            	if(oDomDoc && Sarissa.getParseErrorText(oDomDoc) == Sarissa.PARSED_OK){
755 		                Sarissa.updateContentFromNode(xmlhttp.responseXML, oTargetElement, xsltproc);
756         				if(callback){
757 		                	callback(sFromUrl, oTargetElement);
758 		                }
759 	            	}
760 	            	else{
761 	            		throw Sarissa.getParseErrorText(oDomDoc);
762 	            	}
763             	}
764             	catch(e){
765             		if(callback){
766 			        	callback(sFromUrl, oTargetElement, e);
767 			        }
768 			        else{
769 			        	throw e;
770 			        }
771             	}
772             }
773         };
774         if (skipCache) {
775              var oldage = "Sat, 1 Jan 2000 00:00:00 GMT";
776              xmlhttp.setRequestHeader("If-Modified-Since", oldage);
777         }
778         xmlhttp.send("");
779     }
780     catch(e){
781         Sarissa.updateCursor(oTargetElement, "auto");
782         if(callback){
783         	callback(sFromUrl, oTargetElement, e);
784         }
785         else{
786         	throw e;
787         }
788     }
789 };
790 
791 /**
792  * Update an element's content with the given DOM node. Passing a configured XSLT 
793  * processor will result in transforming and updating oNode before using it to update oTargetElement.
794  * You can also pass a callback function to be executed when the update is finished. The function will be called as 
795  * <code>functionName(oNode, oTargetElement);</code>
796  * @memberOf Sarissa
797  * @param {DOMNode} oNode the URL to make the request to
798  * @param {DOMElement} oTargetElement the element to update
799  * @param {XSLTProcessor} xsltproc (optional) the transformer to use on the given 
800  *                  DOM node before updating the target element with it
801  */
802 Sarissa.updateContentFromNode = function(oNode, oTargetElement, xsltproc) {
803     try {
804         Sarissa.updateCursor(oTargetElement, "wait");
805         Sarissa.clearChildNodes(oTargetElement);
806         // check for parsing errors
807         var ownerDoc = oNode.nodeType == Node.DOCUMENT_NODE?oNode:oNode.ownerDocument;
808         if(ownerDoc.parseError && ownerDoc.parseError.errorCode != 0) {
809             var pre = document.createElement("pre");
810             pre.appendChild(document.createTextNode(Sarissa.getParseErrorText(ownerDoc)));
811             oTargetElement.appendChild(pre);
812         }
813         else {
814             // transform if appropriate
815             if(xsltproc) {
816                 oNode = xsltproc.transformToDocument(oNode);
817             }
818             // be smart, maybe the user wants to display the source instead
819             if(oTargetElement.tagName.toLowerCase() == "textarea" || oTargetElement.tagName.toLowerCase() == "input") {
820                 oTargetElement.value = new XMLSerializer().serializeToString(oNode);
821             }
822             else {
823                 // ok that was not smart; it was paranoid. Keep up the good work by trying to use DOM instead of innerHTML
824                 try{
825                     oTargetElement.appendChild(oTargetElement.ownerDocument.importNode(oNode, true));
826                 }
827                 catch(e){
828                     oTargetElement.innerHTML = new XMLSerializer().serializeToString(oNode);
829                 }
830             }
831         }
832     }
833     catch(e) {
834     	throw e;
835     }
836     finally{
837         Sarissa.updateCursor(oTargetElement, "auto");
838     }
839 };
840 
841 
842 /**
843  * Creates an HTTP URL query string from the given HTML form data
844  * @memberOf Sarissa
845  * @param {HTMLFormElement} oForm the form to construct the query string from
846  */
847 Sarissa.formToQueryString = function(oForm){
848     var qs = "";
849     for(var i = 0;i < oForm.elements.length;i++) {
850         var oField = oForm.elements[i];
851         var sFieldName = oField.getAttribute("name") ? oField.getAttribute("name") : oField.getAttribute("id"); 
852         // ensure we got a proper name/id and that the field is not disabled
853         if(sFieldName && 
854             ((!oField.disabled) || oField.type == "hidden")) {
855             switch(oField.type) {
856                 case "hidden":
857                 case "text":
858                 case "textarea":
859                 case "password":
860                     qs += sFieldName + "=" + encodeURIComponent(oField.value) + "&";
861                     break;
862                 case "select-one":
863                     qs += sFieldName + "=" + encodeURIComponent(oField.options[oField.selectedIndex].value) + "&";
864                     break;
865                 case "select-multiple":
866                     for (var j = 0; j < oField.length; j++) {
867                         var optElem = oField.options[j];
868                         if (optElem.selected === true) {
869                             qs += sFieldName + "[]" + "=" + encodeURIComponent(optElem.value) + "&";
870                         }
871                      }
872                      break;
873                 case "checkbox":
874                 case "radio":
875                     if(oField.checked) {
876                         qs += sFieldName + "=" + encodeURIComponent(oField.value) + "&";
877                     }
878                     break;
879             }
880         }
881     }
882     // return after removing last '&'
883     return qs.substr(0, qs.length - 1); 
884 };
885 
886 
887 /**
888  * Asynchronously update an element with response of an XMLHttpRequest-based emulation of a form submission. <p>The form <code>action</code> and 
889  * <code>method</code> attributess will be followed. Passing a configured XSLT processor will result in 
890  * transforming and updating the server response before using it to update the target element.
891  * You can also pass a callback function to be executed when the update is finished. The function will be called as 
892  * <code>functionName(oNode, oTargetElement);</code></p>
893  * <p>Here is an example of using this in a form element:</p>
894  * <pre name="code" class="xml">
895  * <div id="targetId"> this content will be updated</div>
896  * <form action="/my/form/handler" method="post" 
897  *     onbeforesubmit="return Sarissa.updateContentFromForm(this, document.getElementById('targetId'));"><pre>
898  * <p>If JavaScript is supported, the form will not be submitted. Instead, Sarissa will
899  * scan the form and make an appropriate AJAX request, also adding a parameter 
900  * to signal to the server that this is an AJAX call. The parameter is 
901  * constructed as <code>Sarissa.REMOTE_CALL_FLAG = "=true"</code> so you can change the name in your webpage
902  * simply by assigning another value to Sarissa.REMOTE_CALL_FLAG. If JavaScript is not supported
903  * the form will be submitted normally.
904  * @memberOf Sarissa
905  * @param {HTMLFormElement} oForm the form submition to emulate
906  * @param {DOMElement} oTargetElement the element to update
907  * @param {XSLTProcessor} xsltproc (optional) the transformer to use on the returned
908  *                  content before updating the target element with it
909  * @param {Function} callback (optional) a Function object to execute once the update is finished successfuly, called as <code>callback(oNode, oTargetElement)</code>. 
910  *        In case an exception occurs during excecution and a callback function was provided, the exception is cought and the callback is called as 
911  *        <code>callback(oForm, oTargetElement, exception)</code>
912  */
913 Sarissa.updateContentFromForm = function(oForm, oTargetElement, xsltproc, callback) {
914     try{
915     	Sarissa.updateCursor(oTargetElement, "wait");
916         // build parameters from form fields
917         var params = Sarissa.formToQueryString(oForm) + "&" + Sarissa.REMOTE_CALL_FLAG + "=true";
918         var xmlhttp = new XMLHttpRequest();
919         var bUseGet = oForm.getAttribute("method") && oForm.getAttribute("method").toLowerCase() == "get"; 
920         if(bUseGet) {
921             xmlhttp.open("GET", oForm.getAttribute("action")+"?"+params, true);
922         }
923         else{
924             xmlhttp.open('POST', oForm.getAttribute("action"), true);
925             xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
926             xmlhttp.setRequestHeader("Content-length", params.length);
927             xmlhttp.setRequestHeader("Connection", "close");
928         }
929         xmlhttp.onreadystatechange = function() {
930         	try{
931 	            if (xmlhttp.readyState == 4) {
932 	            	var oDomDoc = xmlhttp.responseXML;
933 	            	if(oDomDoc && Sarissa.getParseErrorText(oDomDoc) == Sarissa.PARSED_OK){
934 		                Sarissa.updateContentFromNode(xmlhttp.responseXML, oTargetElement, xsltproc);
935         				if(callback){
936 		                	callback(oForm, oTargetElement);
937 		                }
938 	            	}
939 	            	else{
940 	            		throw Sarissa.getParseErrorText(oDomDoc);
941 	            	}
942 	            }
943         	}
944         	catch(e){
945         		if(callback){
946         			callback(oForm, oTargetElement, e);
947         		}
948         		else{
949         			throw e;
950         		}
951         	}
952         };
953         xmlhttp.send(bUseGet?"":params);
954     }
955     catch(e){
956         Sarissa.updateCursor(oTargetElement, "auto");
957         if(callback){
958         	callback(oForm, oTargetElement, e);
959         }
960         else{
961         	throw e;
962         }
963     }
964     return false;
965 };
966 Sarissa.FUNCTION_NAME_REGEXP = new RegExp("");//new RegExp("function\\s+(\\S+)\\s*\\((.|\\n)*?\\)\\s*{");
967 /**
968  * Get the name of a function created like:
969  * <pre>function functionName(){}</pre>
970  * If a name is not found and the bForce parameter is true,
971  * attach the function to the window object with a new name and
972  * return that
973  * @param {Function} oFunc the function object
974  * @param {boolean} bForce whether to force a name under the window context if none is found
975  */
976 Sarissa.getFunctionName = function(oFunc, bForce){
977 	//alert("Sarissa.getFunctionName oFunc: "+oFunc);
978 	var name;
979 	if(!name){
980 		if(bForce){
981 			// attach to window object under a new name
982 			name = "SarissaAnonymous" + Sarissa._getUniqueSuffix();
983 			window[name] = oFunc;
984 		}
985 		else{
986 			name = null;
987 		}
988 	}
989 	
990 	//alert("Sarissa.getFunctionName returns: "+name);
991 	if(name){
992 		window[name] = oFunc;
993 	}
994 	return name;
995 };
996 
997 /**
998  *
999  */
1000 Sarissa.setRemoteJsonCallback = function(url, callback, callbackParam) {
1001 	if(!callbackParam){
1002 		callbackParam = "callback";
1003 	}
1004 	var callbackFunctionName = Sarissa.getFunctionName(callback, true);
1005 	var id = "sarissa_json_script_id_" + Sarissa._getUniqueSuffix(); 
1006 	var oHead = document.getElementsByTagName("head")[0];
1007 	var scriptTag = document.createElement('script');
1008 	scriptTag.type = 'text/javascript';
1009 	scriptTag.id = id;
1010 	scriptTag.onload = function(){
1011 		// cleanUp
1012 		// document.removeChild(scriptTag);
1013 	};
1014 	if(url.indexOf("?") != -1){
1015 		url += ("&" + callbackParam + "=" + callbackFunctionName);
1016 	}
1017 	else{
1018 		url += ("?" + callbackParam + "=" + callbackFunctionName);
1019 	}
1020 	scriptTag.src = url;
1021   	oHead.appendChild(scriptTag);
1022   	return id;
1023 };
1024 
1025 //   EOF
1026