From e641a180110f0d30a1a34071addc6eb6d87291ee Mon Sep 17 00:00:00 2001
From: =?utf8?q?Benoi=CC=82t=20Pin?=
Factory method to obtain a new DOM Document object
* @memberOf Sarissa - * @argument sUri the namespace of the root node (if any) - * @argument sUri the local name of the root node (if any) - * @returns a new DOM Document + * @param {String} sUri the namespace of the root node (if any) + * @param {String} sUri the local name of the root node (if any) + * @returns {DOMDOcument} a new DOM Document */ Sarissa.getDomDocument = function(sUri, sName){ var oDoc = document.implementation.createDocument(sUri?sUri:null, sName?sName:null, null); @@ -450,18 +473,18 @@ if(Sarissa._SARISSA_IS_IE){ //========================================== // Common stuff //========================================== -if(!window.DOMParser){ +if(!window.DOMParser || Sarissa._SARISSA_IS_IE9){ if(Sarissa._SARISSA_IS_SAFARI){ - /* + /** * DOMParser is a utility class, used to construct DOMDocuments from XML strings * @constructor */ DOMParser = function() { }; /** * Construct a new DOM Document from the given XMLstring - * @param sXml the given XML string - * @param contentType the content type of the document the given string represents (one of text/xml, application/xml, application/xhtml+xml). - * @return a new DOM Document from the given XML string + * @param {String} sXml the given XML string + * @param {String} contentType the content type of the document the given string represents (one of text/xml, application/xml, application/xhtml+xml). + * @return {DOMDocument} a new DOM Document from the given XML string */ DOMParser.prototype.parseFromString = function(sXml, contentType){ var xmlhttp = new XMLHttpRequest(); @@ -473,6 +496,10 @@ if(!window.DOMParser){ DOMParser = function() { }; DOMParser.prototype.parseFromString = function(sXml, contentType){ var doc = Sarissa.getDomDocument(); + try{ + doc.validateOnParse = false; + doc.setProperty("ProhibitDTD", false); + }catch(e){} doc.loadXML(sXml); return doc; }; @@ -484,8 +511,8 @@ if((typeof(document.importNode) == "undefined") && Sarissa._SARISSA_IS_IE){ /** * Implementation of importNode for the context window document in IE. * IfoNode
is a TextNode, bChildren
is ignored.
- * @param oNode the Node to import
- * @param bChildren whether to include the children of oNode
+ * @param {DOMNode} oNode the Node to import
+ * @param {boolean} bChildren whether to include the children of oNode
* @returns the imported node for further use
*/
document.importNode = function(oNode, bChildren){
@@ -523,13 +550,13 @@ if(!Sarissa.getParseErrorText){
* element if you want to render it.
* Many thanks to Christian Stocker for the initial patch.
* @memberOf Sarissa - * @argument oDoc The target DOM document - * @returns The parsing error description of the target Document in + * @param {DOMDocument} oDoc The target DOM document + * @returns {String} The parsing error description of the target Document in * human readable form (preformated text) */ Sarissa.getParseErrorText = function (oDoc){ var parseErrorText = Sarissa.PARSED_OK; - if(!oDoc.documentElement){ + if((!oDoc) || (!oDoc.documentElement)){ parseErrorText = Sarissa.PARSED_EMPTY; } else if(oDoc.documentElement.tagName == "parsererror"){ parseErrorText = oDoc.documentElement.firstChild.data; @@ -545,13 +572,18 @@ if(!Sarissa.getParseErrorText){ } /** * Get a string with the concatenated values of all string nodes under the given node - * @memberOf Sarissa - * @argument oNode the given DOM node - * @argument deep whether to recursively scan the children nodes of the given node for text as well. Default isfalse
+ * @param {DOMNode} oNode the given DOM node
+ * @param {boolean} deep whether to recursively scan the children nodes of the given node for text as well. Default is false
+ * @memberOf Sarissa
*/
Sarissa.getText = function(oNode, deep){
var s = "";
var nodes = oNode.childNodes;
+ // opera fix, finds no child text node for attributes so we use .value
+ if (oNode.nodeType == Node.ATTRIBUTE_NODE && nodes.length == 0) {
+ return oNode.value;
+ }
+ // END opera fix
for(var i=0; i < nodes.length; i++){
var node = nodes[i];
var nodeType = node.nodeType;
@@ -571,17 +603,29 @@ if(!window.XMLSerializer && Sarissa.getDomDocument && Sarissa.getDomDocument("",
XMLSerializer = function(){};
/**
* Serialize the given DOM Node to an XML string
- * @param oNode the DOM Node to serialize
+ * @param {DOMNode} oNode the DOM Node to serialize
*/
XMLSerializer.prototype.serializeToString = function(oNode) {
return oNode.xml;
};
+} else if (Sarissa._SARISSA_IS_IE9 && window.XMLSerializer) {
+ // We save old object for any futur use with IE Document (not xml)
+ IE9XMLSerializer= XMLSerializer;
+ XMLSerializer = function(){ this._oldSerializer=new IE9XMLSerializer() };
+ XMLSerializer.prototype.serializeToString = function(oNode) {
+ if (typeof(oNode)=='object' && 'xml' in oNode) {
+ return oNode.xml;
+ } else {
+ return this._oldSerializer.serializeToString(oNode);
+ }
+ };
}
/**
* Strips tags from the given markup string. If the given string is
* undefined
, null
or empty, it is returned as is.
* @memberOf Sarissa
+ * @param {String} s the string to strip the tags from
*/
Sarissa.stripTags = function (s) {
return s?s.replace(/<[^>]+>/g,""):s;
@@ -589,7 +633,7 @@ Sarissa.stripTags = function (s) {
/**
* Deletes all child nodes of the given node
* @memberOf Sarissa - * @argument oNode the Node to empty + * @param {DOMNode} oNode the Node to empty */ Sarissa.clearChildNodes = function(oNode) { // need to check for firstChild due to opera 8 bug with hasChildNodes @@ -602,9 +646,9 @@ Sarissa.clearChildNodes = function(oNode) { *Note: The second object's original content is deleted before * the copy operation, unless you supply a true third parameter
* @memberOf Sarissa - * @argument nodeFrom the Node to copy the childNodes from - * @argument nodeTo the Node to copy the childNodes to - * @argument bPreserveExisting whether to preserve the original content of nodeTo, default is false + * @param {DOMNode} nodeFrom the Node to copy the childNodes from + * @param {DOMNode} nodeTo the Node to copy the childNodes to + * @param {boolean} bPreserveExisting whether to preserve the original content of nodeTo, default is false */ Sarissa.copyChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) { if(Sarissa._SARISSA_IS_SAFARI && nodeTo.nodeType == Node.DOCUMENT_NODE){ // SAFARI_OLD ?? @@ -636,9 +680,9 @@ Sarissa.copyChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) { *Note: The second object's original content is deleted before * the move operation, unless you supply a true third parameter
* @memberOf Sarissa - * @argument nodeFrom the Node to copy the childNodes from - * @argument nodeTo the Node to copy the childNodes to - * @argument bPreserveExisting whether to preserve the original content of nodeTo, default is + * @param {DOMNode} nodeFrom the Node to copy the childNodes from + * @param {DOMNode} nodeTo the Node to copy the childNodes to + * @param {boolean} bPreserveExisting whether to preserve the original content of nodeTo, default is */ Sarissa.moveChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) { if((!nodeFrom) || (!nodeTo)){ @@ -674,22 +718,30 @@ Sarissa.moveChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) { * as the XML element name. Array elements are rendered asarray-item
elements,
* using their index/key as the value of the key
attribute.
* @memberOf Sarissa
- * @argument anyObject the object to serialize
- * @argument objectName a name for that object
- * @return the XML serialization of the given object as a string
+ * @param {Object} anyObject the object to serialize
+ * @param {String} objectName a name for that object, to be used as the root element name
+ * @param {String} indentSpace Optional, the indentation space to use, default is an empty
+ * string. A single space character is added in any recursive call.
+ * @param {noolean} skipEscape Optional, whether to skip escaping characters that map to the
+ * five predefined XML entities. Default is false
.
+ * @return {String} the XML serialization of the given object as a string
*/
-Sarissa.xmlize = function(anyObject, objectName, indentSpace){
+Sarissa.xmlize = function(anyObject, objectName, indentSpace, skipEscape){
indentSpace = indentSpace?indentSpace:'';
var s = indentSpace + '<' + objectName + '>';
var isLeaf = false;
if(!(anyObject instanceof Object) || anyObject instanceof Number || anyObject instanceof String || anyObject instanceof Boolean || anyObject instanceof Date){
- s += Sarissa.escape(""+anyObject);
+ s += (skipEscape ? Sarissa.escape(anyObject) : anyObject);
isLeaf = true;
}else{
s += "\n";
var isArrayItem = anyObject instanceof Array;
for(var name in anyObject){
- s += Sarissa.xmlize(anyObject[name], (isArrayItem?"array-item key=\""+name+"\"":name), indentSpace + " ");
+ // do not xmlize functions
+ if (anyObject[name] instanceof Function){
+ continue;
+ }
+ s += Sarissa.xmlize(anyObject[name], (isArrayItem?"array-item key=\""+name+"\"":name), indentSpace + " ");
}
s += indentSpace;
}
@@ -699,7 +751,7 @@ Sarissa.xmlize = function(anyObject, objectName, indentSpace){
/**
* Escape the given string chacters that correspond to the five predefined XML entities
* @memberOf Sarissa
- * @param sXml the string to escape
+ * @param {String} sXml the string to escape
*/
Sarissa.escape = function(sXml){
return sXml.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'");
@@ -709,7 +761,7 @@ Sarissa.escape = function(sXml){
* Unescape the given string. This turns the occurences of the predefined XML
* entities to become the characters they represent correspond to the five predefined XML entities
* @memberOf Sarissa
- * @param sXml the string to unescape
+ * @param {String}sXml the string to unescape
*/
Sarissa.unescape = function(sXml){
return sXml.replace(/'/g,"'").replace(/"/g,"\"").replace(/>/g,">").replace(/</g,"<").replace(/&/g,"&");
@@ -728,13 +780,13 @@ Sarissa.updateCursor = function(oTargetElement, sValue) {
* You can also pass a callback function to be executed when the update is finished. The function will be called as
* functionName(oNode, oTargetElement);
* @memberOf Sarissa
- * @param sFromUrl the URL to make the request to
- * @param oTargetElement the element to update
- * @param xsltproc (optional) the transformer to use on the returned
+ * @param {String} sFromUrl the URL to make the request to
+ * @param {DOMElement} oTargetElement the element to update
+ * @param {XSLTProcessor} xsltproc (optional) the transformer to use on the returned
* content before updating the target element with it
- * @param callback (optional) a Function object to execute once the update is finished successfuly, called as callback(sFromUrl, oTargetElement)
.
+ * @param {Function} callback (optional) a Function object to execute once the update is finished successfuly, called as callback(sFromUrl, oTargetElement)
.
* In case an exception is thrown during execution, the callback is called as called as callback(sFromUrl, oTargetElement, oException)
- * @param skipCache (optional) whether to skip any cache
+ * @param {boolean} skipCache (optional) whether to skip any cache
*/
Sarissa.updateContentFromURI = function(sFromUrl, oTargetElement, xsltproc, callback, skipCache) {
try{
@@ -747,7 +799,9 @@ Sarissa.updateContentFromURI = function(sFromUrl, oTargetElement, xsltproc, call
var oDomDoc = xmlhttp.responseXML;
if(oDomDoc && Sarissa.getParseErrorText(oDomDoc) == Sarissa.PARSED_OK){
Sarissa.updateContentFromNode(xmlhttp.responseXML, oTargetElement, xsltproc);
- callback(sFromUrl, oTargetElement);
+ if(callback){
+ callback(sFromUrl, oTargetElement);
+ }
}
else{
throw Sarissa.getParseErrorText(oDomDoc);
@@ -786,9 +840,9 @@ Sarissa.updateContentFromURI = function(sFromUrl, oTargetElement, xsltproc, call
* You can also pass a callback function to be executed when the update is finished. The function will be called as
* functionName(oNode, oTargetElement);
* @memberOf Sarissa
- * @param oNode the URL to make the request to
- * @param oTargetElement the element to update
- * @param xsltproc (optional) the transformer to use on the given
+ * @param {DOMNode} oNode the URL to make the request to
+ * @param {DOMElement} oTargetElement the element to update
+ * @param {XSLTProcessor} xsltproc (optional) the transformer to use on the given
* DOM node before updating the target element with it
*/
Sarissa.updateContentFromNode = function(oNode, oTargetElement, xsltproc) {
@@ -813,12 +867,12 @@ Sarissa.updateContentFromNode = function(oNode, oTargetElement, xsltproc) {
}
else {
// ok that was not smart; it was paranoid. Keep up the good work by trying to use DOM instead of innerHTML
- if(oNode.nodeType == Node.DOCUMENT_NODE || oNode.ownerDocument.documentElement == oNode) {
- oTargetElement.innerHTML = new XMLSerializer().serializeToString(oNode);
- }
- else{
+ try{
oTargetElement.appendChild(oTargetElement.ownerDocument.importNode(oNode, true));
}
+ catch(e){
+ oTargetElement.innerHTML = new XMLSerializer().serializeToString(oNode);
+ }
}
}
}
@@ -834,6 +888,7 @@ Sarissa.updateContentFromNode = function(oNode, oTargetElement, xsltproc) {
/**
* Creates an HTTP URL query string from the given HTML form data
* @memberOf Sarissa
+ * @param {HTMLFormElement} oForm the form to construct the query string from
*/
Sarissa.formToQueryString = function(oForm){
var qs = "";
@@ -893,11 +948,11 @@ Sarissa.formToQueryString = function(oForm){
* simply by assigning another value to Sarissa.REMOTE_CALL_FLAG. If JavaScript is not supported
* the form will be submitted normally.
* @memberOf Sarissa
- * @param oForm the form submition to emulate
- * @param oTargetElement the element to update
- * @param xsltproc (optional) the transformer to use on the returned
+ * @param {HTMLFormElement} oForm the form submition to emulate
+ * @param {DOMElement} oTargetElement the element to update
+ * @param {XSLTProcessor} xsltproc (optional) the transformer to use on the returned
* content before updating the target element with it
- * @param callback (optional) a Function object to execute once the update is finished successfuly, called as callback(oNode, oTargetElement)
.
+ * @param {Function} callback (optional) a Function object to execute once the update is finished successfuly, called as callback(oNode, oTargetElement)
.
* In case an exception occurs during excecution and a callback function was provided, the exception is cought and the callback is called as
* callback(oForm, oTargetElement, exception)
*/
@@ -923,7 +978,9 @@ Sarissa.updateContentFromForm = function(oForm, oTargetElement, xsltproc, callba
var oDomDoc = xmlhttp.responseXML;
if(oDomDoc && Sarissa.getParseErrorText(oDomDoc) == Sarissa.PARSED_OK){
Sarissa.updateContentFromNode(xmlhttp.responseXML, oTargetElement, xsltproc);
- callback(oForm, oTargetElement);
+ if(callback){
+ callback(oForm, oTargetElement);
+ }
}
else{
throw Sarissa.getParseErrorText(oDomDoc);
@@ -953,4 +1010,59 @@ Sarissa.updateContentFromForm = function(oForm, oTargetElement, xsltproc, callba
return false;
};
+/**
+ * Get the name of a function created like:
+ * function functionName(){}+ * If a name is not found, attach the function to + * the window object with a new name and return that + * @param {Function} oFunc the function object + */ +Sarissa.getFunctionName = function(oFunc){ + if(!oFunc || (typeof oFunc != 'function' )){ + throw "The value of parameter 'oFunc' must be a function"; + } + if(oFunc.name) { + return oFunc.name; + } + // try to parse the function name from the defintion + var sFunc = oFunc.toString(); + alert("sFunc: "+sFunc); + var name = sFunc.substring(sFunc.indexOf('function') + 8 , sFunc.indexOf('(')); + if(!name || name.length == 0 || name == " "){ + // attach to window object under a new name + name = "SarissaAnonymous" + Sarissa._getUniqueSuffix(); + window[name] = oFunc; + } + return name; +}; + +/** + * + */ +Sarissa.setRemoteJsonCallback = function(url, callback, callbackParam) { + if(!callbackParam){ + callbackParam = "callback"; + } + var callbackFunctionName = Sarissa.getFunctionName(callback); + //alert("callbackFunctionName: '" + callbackFunctionName+"', length: "+callbackFunctionName.length); + var id = "sarissa_json_script_id_" + Sarissa._getUniqueSuffix(); + var oHead = document.getElementsByTagName("head")[0]; + var scriptTag = document.createElement('script'); + scriptTag.type = 'text/javascript'; + scriptTag.id = id; + scriptTag.onload = function(){ + // cleanUp + // document.removeChild(scriptTag); + }; + if(url.indexOf("?") != -1){ + url += ("&" + callbackParam + "=" + callbackFunctionName); + } + else{ + url += ("?" + callbackParam + "=" + callbackFunctionName); + } + scriptTag.src = url; + oHead.appendChild(scriptTag); + return id; +}; + // EOF -- 2.20.1