PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/plugins/insertcode/js/encoder.js

#
JavaScript | 215 lines | 140 code | 40 blank | 35 comment | 34 complexity | 536f9cfd7531ae779600530d74b0c4a6 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. /*
  2. From http://www.strictly-software.com/htmlencode
  3. */
  4. Encoder = {
  5. // When encoding do we convert characters into html or numerical entities
  6. EncodeType : "entity", // entity OR numerical
  7. isEmpty : function(val){
  8. if(val){
  9. return ((val===null) || val.length==0 || /^\s+$/.test(val));
  10. }else{
  11. return true;
  12. }
  13. },
  14. // Convert HTML entities into numerical entities
  15. HTML2Numerical : function(s){
  16. var arr1 = new Array(' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','à','á','â','ã','Ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','Ö','×','ø','ù','ú','û','Ü','ý','þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','Ø','ù','ú','û','ü','ý','þ','ÿ','"','&','<','>','œ','œ','š','š','ÿ','ˆ','˜',' ',' ',' ','‌','‍','‎','‏','–','—','‘','’','‚','“','”','„','†','†','‰','‹','›','€','ƒ','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','σ','τ','υ','φ','χ','ψ','ω','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','ς','σ','τ','υ','φ','χ','ψ','ω','ϑ','ϒ','ϖ','•','…','′','′','‾','⁄','℘','ℑ','ℜ','™','ℵ','←','↑','→','↓','↔','↵','←','↑','→','↓','↔','∀','∂','∃','∅','∇','∈','∉','∋','∏','∑','−','∗','√','∝','∞','∠','∧','∨','∩','∪','∫','∴','∼','≅','≈','≠','≡','≤','≥','⊂','⊃','⊄','⊆','⊇','⊕','⊗','⊥','⋅','⌈','⌉','⌊','⌋','⟨','⟩','◊','♠','♣','♥','♦');
  17. var arr2 = new Array(' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ','"','&','<','>','Œ','œ','Š','š','Ÿ','ˆ','˜',' ',' ',' ','‌','‍','‎','‏','–','—','‘','’','‚','“','”','„','†','‡','‰','‹','›','€','ƒ','Α','Β','Γ','Δ','Ε','Ζ','Η','Θ','Ι','Κ','Λ','Μ','Ν','Ξ','Ο','Π','Ρ','Σ','Τ','Υ','Φ','Χ','Ψ','Ω','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','ς','σ','τ','υ','φ','χ','ψ','ω','ϑ','ϒ','ϖ','•','…','′','″','‾','⁄','℘','ℑ','ℜ','™','ℵ','←','↑','→','↓','↔','↵','⇐','⇑','⇒','⇓','⇔','∀','∂','∃','∅','∇','∈','∉','∋','∏','∑','−','∗','√','∝','∞','∠','∧','∨','∩','∪','∫','∴','∼','≅','≈','≠','≡','≤','≥','⊂','⊃','⊄','⊆','⊇','⊕','⊗','⊥','⋅','⌈','⌉','⌊','⌋','〈','〉','◊','♠','♣','♥','♦');
  18. return this.swapArrayVals(s,arr1,arr2);
  19. },
  20. // Convert Numerical entities into HTML entities
  21. NumericalToHTML : function(s){
  22. var arr1 = new Array(' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ','"','&','<','>','Œ','œ','Š','š','Ÿ','ˆ','˜',' ',' ',' ','‌','‍','‎','‏','–','—','‘','’','‚','“','”','„','†','‡','‰','‹','›','€','ƒ','Α','Β','Γ','Δ','Ε','Ζ','Η','Θ','Ι','Κ','Λ','Μ','Ν','Ξ','Ο','Π','Ρ','Σ','Τ','Υ','Φ','Χ','Ψ','Ω','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','ς','σ','τ','υ','φ','χ','ψ','ω','ϑ','ϒ','ϖ','•','…','′','″','‾','⁄','℘','ℑ','ℜ','™','ℵ','←','↑','→','↓','↔','↵','⇐','⇑','⇒','⇓','⇔','∀','∂','∃','∅','∇','∈','∉','∋','∏','∑','−','∗','√','∝','∞','∠','∧','∨','∩','∪','∫','∴','∼','≅','≈','≠','≡','≤','≥','⊂','⊃','⊄','⊆','⊇','⊕','⊗','⊥','⋅','⌈','⌉','⌊','⌋','〈','〉','◊','♠','♣','♥','♦');
  23. var arr2 = new Array(' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','à','á','â','ã','Ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','Ö','×','ø','ù','ú','û','Ü','ý','þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','Ø','ù','ú','û','ü','ý','þ','ÿ','"','&','<','>','œ','œ','š','š','ÿ','ˆ','˜',' ',' ',' ','‌','‍','‎','‏','–','—','‘','’','‚','“','”','„','†','†','‰','‹','›','€','ƒ','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','σ','τ','υ','φ','χ','ψ','ω','α','β','γ','δ','ε','ζ','η','θ','ι','κ','λ','μ','ν','ξ','ο','π','ρ','ς','σ','τ','υ','φ','χ','ψ','ω','ϑ','ϒ','ϖ','•','…','′','′','‾','⁄','℘','ℑ','ℜ','™','ℵ','←','↑','→','↓','↔','↵','←','↑','→','↓','↔','∀','∂','∃','∅','∇','∈','∉','∋','∏','∑','−','∗','√','∝','∞','∠','∧','∨','∩','∪','∫','∴','∼','≅','≈','≠','≡','≤','≥','⊂','⊃','⊄','⊆','⊇','⊕','⊗','⊥','⋅','⌈','⌉','⌊','⌋','⟨','⟩','◊','♠','♣','♥','♦');
  24. return this.swapArrayVals(s,arr1,arr2);
  25. },
  26. // Numerically encodes all unicode characters
  27. numEncode : function(s){
  28. if(this.isEmpty(s)) return "";
  29. var e = "";
  30. for (var i = 0; i < s.length; i++)
  31. {
  32. var c = s.charAt(i);
  33. if (c < " " || c > "~")
  34. {
  35. c = "&#" + c.charCodeAt() + ";";
  36. }
  37. e += c;
  38. }
  39. return e;
  40. },
  41. // HTML Decode numerical and HTML entities back to original values
  42. htmlDecode : function(s){
  43. var c,m,d = s;
  44. if(this.isEmpty(d)) return "";
  45. // convert HTML entites back to numerical entites first
  46. d = this.HTML2Numerical(d);
  47. // look for numerical entities &#34;
  48. arr=d.match(/&#[0-9]{1,5};/g);
  49. // if no matches found in string then skip
  50. if(arr!=null){
  51. for(var x=0;x<arr.length;x++){
  52. m = arr[x];
  53. c = m.substring(2,m.length-1); //get numeric part which is refernce to unicode character
  54. // if its a valid number we can decode
  55. if(c >= -32768 && c <= 65535){
  56. // decode every single match within string
  57. d = d.replace(m, String.fromCharCode(c));
  58. }else{
  59. d = d.replace(m, ""); //invalid so replace with nada
  60. }
  61. }
  62. }
  63. return d;
  64. },
  65. // encode an input string into either numerical or HTML entities
  66. htmlEncode : function(s,dbl){
  67. if(this.isEmpty(s)) return "";
  68. // do we allow double encoding? E.g will &amp; be turned into &amp;amp;
  69. dbl = dbl | false; //default to prevent double encoding
  70. // if allowing double encoding we do ampersands first
  71. if(dbl){
  72. if(this.EncodeType=="numerical"){
  73. s = s.replace(/&/g, "&#38;");
  74. }else{
  75. s = s.replace(/&/g, "&amp;");
  76. }
  77. }
  78. // convert the xss chars to numerical entities ' " < >
  79. s = this.XSSEncode(s,false);
  80. if(this.EncodeType=="numerical" || !dbl){
  81. // Now call function that will convert any HTML entities to numerical codes
  82. s = this.HTML2Numerical(s);
  83. }
  84. // Now encode all chars above 127 e.g unicode
  85. s = this.numEncode(s);
  86. // now we know anything that needs to be encoded has been converted to numerical entities we
  87. // can encode any ampersands & that are not part of encoded entities
  88. // to handle the fact that I need to do a negative check and handle multiple ampersands &&&
  89. // I am going to use a placeholder
  90. // if we don't want double encoded entities we ignore the & in existing entities
  91. if(!dbl){
  92. s = s.replace(/&#/g,"##AMPHASH##");
  93. if(this.EncodeType=="numerical"){
  94. s = s.replace(/&/g, "&#38;");
  95. }else{
  96. s = s.replace(/&/g, "&amp;");
  97. }
  98. s = s.replace(/##AMPHASH##/g,"&#");
  99. }
  100. // replace any malformed entities
  101. s = s.replace(/&#\d*([^\d;]|$)/g, "$1");
  102. if(!dbl){
  103. // safety check to correct any double encoded &amp;
  104. s = this.correctEncoding(s);
  105. }
  106. // now do we need to convert our numerical encoded string into entities
  107. if(this.EncodeType=="entity"){
  108. s = this.NumericalToHTML(s);
  109. }
  110. return s;
  111. },
  112. // Encodes the basic 4 characters used to malform HTML in XSS hacks
  113. XSSEncode : function(s,en){
  114. if(!this.isEmpty(s)){
  115. en = en || true;
  116. // do we convert to numerical or html entity?
  117. if(en){
  118. s = s.replace(/\'/g,"&#39;"); //no HTML equivalent as &apos is not cross browser supported
  119. s = s.replace(/\"/g,"&quot;");
  120. s = s.replace(/</g,"&lt;");
  121. s = s.replace(/>/g,"&gt;");
  122. }else{
  123. s = s.replace(/\'/g,"&#39;"); //no HTML equivalent as &apos is not cross browser supported
  124. s = s.replace(/\"/g,"&#34;");
  125. s = s.replace(/</g,"&#60;");
  126. s = s.replace(/>/g,"&#62;");
  127. }
  128. return s;
  129. }else{
  130. return "";
  131. }
  132. },
  133. // returns true if a string contains html or numerical encoded entities
  134. hasEncoded : function(s){
  135. if(/&#[0-9]{1,5};/g.test(s)){
  136. return true;
  137. }else if(/&[A-Z]{2,6};/gi.test(s)){
  138. return true;
  139. }else{
  140. return false;
  141. }
  142. },
  143. // will remove any unicode characters
  144. stripUnicode : function(s){
  145. return s.replace(/[^\x20-\x7E]/g,"");
  146. },
  147. // corrects any double encoded &amp; entities e.g &amp;amp;
  148. correctEncoding : function(s){
  149. return s.replace(/(&amp;)(amp;)+/,"$1");
  150. },
  151. // Function to loop through an array swaping each item with the value from another array e.g swap HTML entities with Numericals
  152. swapArrayVals : function(s,arr1,arr2){
  153. if(this.isEmpty(s)) return "";
  154. var re;
  155. if(arr1 && arr2){
  156. //ShowDebug("in swapArrayVals arr1.length = " + arr1.length + " arr2.length = " + arr2.length)
  157. // array lengths must match
  158. if(arr1.length == arr2.length){
  159. for(var x=0,i=arr1.length;x<i;x++){
  160. re = new RegExp(arr1[x], 'g');
  161. s = s.replace(re,arr2[x]); //swap arr1 item with matching item from arr2
  162. }
  163. }
  164. }
  165. return s;
  166. },
  167. inArray : function( item, arr ) {
  168. for ( var i = 0, x = arr.length; i < x; i++ ){
  169. if ( arr[i] === item ){
  170. return i;
  171. }
  172. }
  173. return -1;
  174. }
  175. }