source: trunk/prototype/app/plugins/encoder/encoder.js @ 5341

Revision 5341, 11.8 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2434 - Commit inicial do novo módulo de agenda do Expresso - expressoCalendar

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