utils.js (4108B)
1 /** 2 * 3 * @filename utils.js 4 * @$LastChangedDate: 2004-06-26 13:51:10 +0200 (Sat, 26 Jun 2004) $ 5 * @author Fabio Serra <faser@faser.net> 6 * @copyright Fabio Serra (The Initial Developer of the Original Code) 7 * @license Mozilla Public License Version 1.1 8 */ 9 10 11 function trim(s){ 12 return s.replace(/\s+$|^\s+/g,""); 13 } 14 15 function toNumber(s) { 16 if(!s || s == "") return 0; 17 return Number(s.replace(/[^0-9\.]/g, "")); 18 } 19 20 function CaseInsensitiveString(s) { 21 return String(s).toUpperCase(); 22 } 23 24 function toYear(s) { 25 var date = new Date(s); 26 var year = 0; 27 if(date && date != "Invalid Date") { 28 year = date.getFullYear(); 29 } else { 30 year = s.substr((s.length-4),s.length); 31 } 32 33 if(isNaN(year)) { 34 year = 0; 35 } 36 37 return year; 38 } 39 40 function wordCount(str) { 41 var wc = str.split(" ").lenght; 42 } 43 44 //Busy Waiting 45 function pause(numberMillis) { 46 var now = new Date(); 47 var exitTime = now.getTime() + numberMillis; 48 while (true) { 49 now = new Date(); 50 if (now.getTime() > exitTime) return; 51 } 52 } 53 54 55 function stripHTML(string) { 56 return string.replace(/(<([^>]+)>)/ig,""); 57 } 58 59 function escapeXML(str) { 60 var newString = ""; 61 newString = str.replace(/&/g,"&"); 62 newString = newString.replace(/</g,"<"); 63 newString = newString.replace(/>/g,">"); 64 //To prevent xul errors 65 newString = newString.replace(/"/g,"''"); 66 67 return newString; 68 } 69 70 function serializeXML(xmlDoc) { 71 var s = new XMLSerializer(); 72 try { 73 var str = s.serializeToString(xmlDoc); 74 }catch(e){ 75 alert(e); 76 } 77 return str; 78 } 79 80 /** 81 * Insert an XML string in a XML/XUL document 82 * 83 * @param {XML Element} node 84 * @param {string} sxml The XML string 85 * @param {bool} isXul Indicate if the string is a XUL XML 86 * @param {bool} insertBefore Indicate if the XML should be appended or inserted before the XML element 87 * @return bool 88 * Thanks to Zero (zer0.shock@libero.it) 89 */ 90 function innerXML(node,sxml,isXUL,insertBefore) { 91 var appendChild = true; 92 if(insertBefore) {appendChild = false;} 93 var doc = ""; 94 if(isXUL) { 95 var boxContainerOpen = '<box id="xul-data" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">' 96 var boxContainerClose = '</box>'; 97 doc = (new DOMParser()).parseFromString(boxContainerOpen + sxml + boxContainerClose, "text/xml"); 98 }else{ 99 doc = (new DOMParser()).parseFromString("<root>"+sxml+"</root>", "text/xml"); 100 } 101 102 if (doc.firstChild.tagName=="parsererror"){ 103 alert(doc.firstChild.childNodes[0].nodeValue +'\n'); 104 105 return false; 106 }else{ 107 doc=doc.firstChild; 108 for (var i=0;i < doc.childNodes.length;i++) { 109 if(appendChild) { 110 node.appendChild(node.ownerDocument.importNode(doc.childNodes[i],true)); 111 }else{ 112 node.insertBefore(node.ownerDocument.importNode(doc.childNodes[i],true),node.firstChild); 113 } 114 } 115 return true; 116 } 117 } 118 119 //Elapsed time 120 function ElapsedTime() { 121 this.timeStart = new Date(); 122 } 123 124 ElapsedTime.prototype.getElapsedTime = function() { 125 var timeStop = new Date() 126 var elapsedTime = (timeStop - this.timeStart)/1000; 127 return elapsedTime; 128 } 129 130 //Dump to console the properties of an object 131 function dumpProp(obj) { 132 var str = ""; 133 for (var i in obj) { 134 str += i + '\n'; 135 } 136 dump(str); 137 } 138 139 function eDump(e) { 140 dump("\n" + e.type + ":"+e.target.tagName); 141 } 142 143 /** 144 * @deprecated too slow, use convertToUnicode 145 * http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm#a4 146 * @author tobias@justdreams.de 147 */ 148 function decode_utf8(utftext) { 149 var plaintext = ""; var i=0; var c=c1=c2=c3=0; 150 // while-Schleife, weil einige Zeichen uebersprungen werden 151 while(i<utftext.length) 152 { 153 c = utftext.charCodeAt(i); 154 if (c<128) { 155 plaintext += String.fromCharCode(c); 156 i++;} 157 else if((c>191) && (c<224)) { 158 c2 = utftext.charCodeAt(i+1); 159 plaintext += String.fromCharCode(((c&31)<<6) | (c2&63)); 160 i+=2;} 161 else { 162 c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2); 163 plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63)); 164 i+=3;} 165 } 166 return plaintext; 167 }