1/** 2 * A Javascript object to encode and/or decode html characters using HTML or Numeric entities that handles double or partial encoding 3 * Author: R Reid 4 * source: http://www.strictly-software.com/htmlencode 5 * Licences: GPL, The MIT License (MIT) 6 * Copyright: (c) 2011 Robert Reid - Strictly-Software.com 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 * 12 * Revision: 13 * 2011-07-14, Jacques-Yves Bleau: 14 * - fixed conversion error with capitalized accentuated characters 15 * + converted arr1 and arr2 to object property to remove redundancy 16 * 17 * Revision: 18 * 2011-11-10, Ce-Yi Hio: 19 * - fixed conversion error with a number of capitalized entity characters 20 * 21 * Revision: 22 * 2011-11-10, Rob Reid: 23 * - changed array format 24 * 25 * Revision: 26 * 2012-09-23, Alex Oss: 27 * - replaced string concatonation in numEncode with string builder, push and join for peformance with ammendments by Rob Reid 28 */ 29 30Encoder = { 31 32 // When encoding do we convert characters into html or numerical entities 33 EncodeType : "entity", // entity OR numerical 34 35 isEmpty : function(val){ 36 if(val){ 37 return ((val===null) || val.length==0 || /^\s+$/.test(val)); 38 }else{ 39 return true; 40 } 41 }, 42 43 // arrays for conversion from HTML Entities to Numerical values 44 arr1: [' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ','"','&','<','>','Œ','œ','Š','š','Ÿ','ˆ','˜',' ',' ',' ','‌','‍','‎','‏','–','—','‘','’','‚','“','”','„','†','‡','‰','‹','›','€','ƒ','Α','Β','Γ','Δ','Ε','Ζ','Η','Θ','Ι','Κ','Λ','Μ','Ν','Ξ','Ο','Π','Ρ','Σ','Τ','Υ','Φ','Χ','Ψ','Ω','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','ς','σ','τ','υ','φ','χ','ψ','ω','ϑ','ϒ','ϖ','•','…','′','″','‾','⁄','℘','ℑ','ℜ','™','ℵ','←','↑','→','↓','↔','↵','⇐','⇑','⇒','⇓','⇔','∀','∂','∃','∅','∇','∈','∉','∋','∏','∑','−','∗','√','∝','∞','∠','∧','∨','∩','∪','∫','∴','∼','≅','≈','≠','≡','≤','≥','⊂','⊃','⊄','⊆','⊇','⊕','⊗','⊥','⋅','⌈','⌉','⌊','⌋','⟨','⟩','◊','♠
44','♣','♥','♦'], 45 arr2: [' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ','"','&','<','>','Œ','œ','Š','š','Ÿ','ˆ','˜',' ',' ',' ','‌','‍','‎','‏','–','—','‘','’','‚','“','”','„','†','‡','‰','‹','›','€','ƒ','Α','Β','Γ','Δ','Ε','Ζ','Η','Θ','Ι','Κ','Λ','Μ','Ν','Ξ','Ο','Π','Ρ','Σ','Τ','Υ','Φ','Χ','Ψ','Ω','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','ς','σ','τ','υ','φ','χ','ψ','ω','ϑ','ϒ','ϖ','•','…','′','″','‾','⁄','℘','ℑ','ℜ','™','ℵ','←','↑','→','↓','↔','↵','⇐','⇑','⇒','⇓','⇔','∀','∂','∃','∅','∇','∈','∉','∋','∏','∑','−','∗','√','∝','∞','∠','∧','∨','∩','∪','∫','∴','∼','≅','≈','≠','≡','≤','≥','⊂','⊃','⊄','⊆','⊇','⊕','⊗','⊥','⋅','⌈','⌉','⌊','⌋','〈','〉','◊','♠','♣','♥','♦'], 46 47 // Convert HTML entities into numerical entities 48 HTML2Numerical : function(s){ 49 return this.swapArrayVals(s,this.arr1,this.arr2); 50 }, 51 52 // Convert Numerical entities into HTML entities 53 NumericalToHTML : function(s){ 54 return this.swapArrayVals(s,this.arr2,this.arr1); 55 }, 56 57 58 // Numerically encodes all unicode characters 59 numEncode : function(s){ 60 if(this.isEmpty(s)) return ""; 61 62 var a = [], 63 l = s.length; 64 65 for (var i=0;i<l;i++){ 66 var c = s.charAt(i); 67 if (c < " " || c > "~"){ 68 a.push("&#"); 69 a.push(c.charCodeAt()); //numeric value of code point 70 a.push(";"); 71 }else{ 72 a.push(c); 73 } 74 } 75 76 return a.join(""); 77 }, 78 79 // HTML Decode numerical and HTML entities back to original values 80 htmlDecode : function(s){ 81 82 var c,m,d = s; 83 84 if(this.isEmpty(d)) return ""; 85 86 // convert HTML entites back to numerical entites first 87 d = this.HTML2Numerical(d); 88 89 // look for numerical entities " 90 arr=d.match(/&#[0-9]{1,5};/g); 91 92 // if no matches found in string then skip 93 if(arr!=null){ 94 for(var x=0;x<arr.length;x++){ 95 m = arr[x]; 96 c = m.substring(2,m.length-1); //get numeric part which is refernce to unicode character 97 // if its a valid number we can decode 98 if(c >= -32768 && c <= 65535){ 99 // decode every single match within string 100 d = d.replace(m, String.fromCharCode(c)); 101 }else{ 102 d = d.replace(m, ""); //invalid so replace with nada 103 } 104 } 105 } 106 107 return d; 108 }, 109 110 // encode an input string into either numerical or HTML entities 111 htmlEncode : function(s,dbl){ 112 113 if(this.isEmpty(s)) return ""; 114 115 // do we allow double encoding? E.g will & be turned into &amp; 116 dbl = dbl || false; //default to prevent double encoding 117 118 // if allowing double encoding we do ampersands first 119 if(dbl){ 120 if(this.EncodeType=="numerical"){ 121 s = s.replace(/&/g, "&"); 122 }else{ 123 s = s.replace(/&/g, "&"); 124 } 125 } 126 127 // convert the xss chars to numerical entities ' " < > 128 s = this.XSSEncode(s,false); 129 130 if(this.EncodeType=="numerical" || !dbl){ 131 // Now call function that will convert any HTML entities to numerical codes 132 s = this.HTML2Numerical(s); 133 } 134 135 // Now encode all chars above 127 e.g unicode 136 s = this.numEncode(s); 137 138 // now we know anything that needs to be encoded has been converted to numerical entities we 139 // can encode any ampersands & that are not part of encoded entities 140 // to handle the fact that I need to do a negative check and handle multiple ampersands &&& 141 // I am going to use a placeholder 142 143 // if we don't want double encoded entities we ignore the & in existing entities 144 if(!dbl){ 145 s = s.replace(/&#/g,"##AMPHASH##"); 146 147 if(this.EncodeType=="numerical"){ 148 s = s.replace(/&/g, "&"); 149 }else{ 150 s = s.replace(/&/g, "&"); 151 } 152 153 s = s.replace(/##AMPHASH##/g,"&#"); 154 } 155 156 // replace any malformed entities 157 s = s.replace(/&#\d*([^\d;]|$)/g, "$1"); 158 159 if(!dbl){ 160 // safety check to correct any double encoded & 161 s = this.correctEncoding(s); 162 } 163 164 // now do we need to convert our numerical encoded string into entities 165 if(this.EncodeType=="entity"){ 166 s = this.NumericalToHTML(s); 167 } 168 169 return s; 170 }, 171 172 // Encodes the basic 4 characters used to malform HTML in XSS hacks 173 XSSEncode : function(s,en){ 174 if(!this.isEmpty(s)){ 175 en = en || true; 176 // do we convert to numerical or html entity? 177 if(en){ 178 s = s.replace(/\'/g,"'"); //no HTML equivalent as &apos is not cross browser supported 179 s = s.replace(/\"/g,"""); 180 s = s.replace(/</g,"<"); 181 s = s.replace(/>/g,">"); 182 }else{ 183 s = s.replace(/\'/g,"'"); //no HTML equivalent as &apos is not cross browser supported 184 s = s.replace(/\"/g,"""); 185 s = s.replace(/</g,"<"); 186 s = s.replace(/>/g,">"); 187 } 188 return s; 189 }else{ 190 return ""; 191 } 192 }, 193 194 // returns true if a string contains html or numerical encoded entities 195 hasEncoded : function(s){ 196 if(/&#[0-9]{1,5};/g.test(s)){ 197 return true; 198 }else if(/&[A-Z]{2,6};/gi.test(s)){ 199 return true; 200 }else{ 201 return false; 202 } 203 }, 204 205 // will remove any unicode characters 206 stripUnicode : function(s){ 207 return s.replace(/[^\x20-\x7E]/g,""); 208 209 }, 210 211 // corrects any double encoded & entities e.g &amp; 212 correctEncoding : function(s){ 213 return s.replace(/(&)(amp;)+/,"$1"); 214 }, 215 216 217 // Function to loop through an array swaping each item with the value from another array e.g swap HTML entities with Numericals 218 swapArrayVals : function(s,arr1,arr2){ 219 if(this.isEmpty(s)) return ""; 220 var re; 221 if(arr1 && arr2){ 222 //ShowDebug("in swapArrayVals arr1.length = " + arr1.length + " arr2.length = " + arr2.length) 223 // array lengths must match 224 if(arr1.length == arr2.length){ 225 for(var x=0,i=arr1.length;x<i;x++){ 226 re = new RegExp(arr1[x], 'g'); 227 s = s.replace(re,arr2[x]); //swap arr1 item with matching item from arr2 228 } 229 } 230 } 231 return s; 232 }, 233 234 inArray : function( item, arr ) { 235 for ( var i = 0, x = arr.length; i < x; i++ ){ 236 if ( arr[i] === item ){ 237 return i; 238 } 239 } 240 return -1; 241 } 242 243}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.