sync
[roojs1] / Roo / htmleditor / TidyEntities.js
1 /***
2  * This is based loosely on tinymce 
3  * @class Roo.htmleditor.TidyEntities
4  * @static
5  * https://github.com/thorn0/tinymce.html/blob/master/tinymce.html.js
6  *
7  * Not 100% sure this is actually used or needed.
8  */
9
10 Roo.htmleditor.TidyEntities = {
11     
12     /**
13      * initialize data..
14      */
15     init : function (){
16      
17         this.namedEntities = this.buildEntitiesLookup(this.namedEntitiesData, 32);
18        
19     },
20
21
22     buildEntitiesLookup: function(items, radix) {
23         var i, chr, entity, lookup = {};
24         if (!items) {
25             return {};
26         }
27         items = typeof(items) == 'string' ? items.split(',') : items;
28         radix = radix || 10;
29         // Build entities lookup table
30         for (i = 0; i < items.length; i += 2) {
31             chr = String.fromCharCode(parseInt(items[i], radix));
32             // Only add non base entities
33             if (!this.baseEntities[chr]) {
34                 entity = '&' + items[i + 1] + ';';
35                 lookup[chr] = entity;
36                 lookup[entity] = chr;
37             }
38         }
39         return lookup;
40         
41     },
42     
43     asciiMap : {
44             128: '€',
45             130: '‚',
46             131: 'ƒ',
47             132: '„',
48             133: '…',
49             134: '†',
50             135: '‡',
51             136: 'ˆ',
52             137: '‰',
53             138: 'Š',
54             139: '‹',
55             140: 'Œ',
56             142: 'Ž',
57             145: '‘',
58             146: '’',
59             147: '“',
60             148: '”',
61             149: '•',
62             150: '–',
63             151: '—',
64             152: '˜',
65             153: '™',
66             154: 'š',
67             155: '›',
68             156: 'œ',
69             158: 'ž',
70             159: 'Ÿ'
71     },
72     // Raw entities
73     baseEntities : {
74         '"': '&quot;',
75         // Needs to be escaped since the YUI compressor would otherwise break the code
76         '\'': '&#39;',
77         '<': '&lt;',
78         '>': '&gt;',
79         '&': '&amp;',
80         '`': '&#96;'
81     },
82     // Reverse lookup table for raw entities
83     reverseEntities : {
84         '&lt;': '<',
85         '&gt;': '>',
86         '&amp;': '&',
87         '&quot;': '"',
88         '&apos;': '\''
89     },
90     
91     attrsCharsRegExp : /[&<>\"\u0060\u007E-\uD7FF\uE000-\uFFEF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
92     textCharsRegExp : /[<>&\u007E-\uD7FF\uE000-\uFFEF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
93     rawCharsRegExp : /[<>&\"\']/g,
94     entityRegExp : /&#([a-z0-9]+);?|&([a-z0-9]+);/gi,
95     namedEntities  : false,
96     namedEntitiesData : [ 
97         '50',
98         'nbsp',
99         '51',
100         'iexcl',
101         '52',
102         'cent',
103         '53',
104         'pound',
105         '54',
106         'curren',
107         '55',
108         'yen',
109         '56',
110         'brvbar',
111         '57',
112         'sect',
113         '58',
114         'uml',
115         '59',
116         'copy',
117         '5a',
118         'ordf',
119         '5b',
120         'laquo',
121         '5c',
122         'not',
123         '5d',
124         'shy',
125         '5e',
126         'reg',
127         '5f',
128         'macr',
129         '5g',
130         'deg',
131         '5h',
132         'plusmn',
133         '5i',
134         'sup2',
135         '5j',
136         'sup3',
137         '5k',
138         'acute',
139         '5l',
140         'micro',
141         '5m',
142         'para',
143         '5n',
144         'middot',
145         '5o',
146         'cedil',
147         '5p',
148         'sup1',
149         '5q',
150         'ordm',
151         '5r',
152         'raquo',
153         '5s',
154         'frac14',
155         '5t',
156         'frac12',
157         '5u',
158         'frac34',
159         '5v',
160         'iquest',
161         '60',
162         'Agrave',
163         '61',
164         'Aacute',
165         '62',
166         'Acirc',
167         '63',
168         'Atilde',
169         '64',
170         'Auml',
171         '65',
172         'Aring',
173         '66',
174         'AElig',
175         '67',
176         'Ccedil',
177         '68',
178         'Egrave',
179         '69',
180         'Eacute',
181         '6a',
182         'Ecirc',
183         '6b',
184         'Euml',
185         '6c',
186         'Igrave',
187         '6d',
188         'Iacute',
189         '6e',
190         'Icirc',
191         '6f',
192         'Iuml',
193         '6g',
194         'ETH',
195         '6h',
196         'Ntilde',
197         '6i',
198         'Ograve',
199         '6j',
200         'Oacute',
201         '6k',
202         'Ocirc',
203         '6l',
204         'Otilde',
205         '6m',
206         'Ouml',
207         '6n',
208         'times',
209         '6o',
210         'Oslash',
211         '6p',
212         'Ugrave',
213         '6q',
214         'Uacute',
215         '6r',
216         'Ucirc',
217         '6s',
218         'Uuml',
219         '6t',
220         'Yacute',
221         '6u',
222         'THORN',
223         '6v',
224         'szlig',
225         '70',
226         'agrave',
227         '71',
228         'aacute',
229         '72',
230         'acirc',
231         '73',
232         'atilde',
233         '74',
234         'auml',
235         '75',
236         'aring',
237         '76',
238         'aelig',
239         '77',
240         'ccedil',
241         '78',
242         'egrave',
243         '79',
244         'eacute',
245         '7a',
246         'ecirc',
247         '7b',
248         'euml',
249         '7c',
250         'igrave',
251         '7d',
252         'iacute',
253         '7e',
254         'icirc',
255         '7f',
256         'iuml',
257         '7g',
258         'eth',
259         '7h',
260         'ntilde',
261         '7i',
262         'ograve',
263         '7j',
264         'oacute',
265         '7k',
266         'ocirc',
267         '7l',
268         'otilde',
269         '7m',
270         'ouml',
271         '7n',
272         'divide',
273         '7o',
274         'oslash',
275         '7p',
276         'ugrave',
277         '7q',
278         'uacute',
279         '7r',
280         'ucirc',
281         '7s',
282         'uuml',
283         '7t',
284         'yacute',
285         '7u',
286         'thorn',
287         '7v',
288         'yuml',
289         'ci',
290         'fnof',
291         'sh',
292         'Alpha',
293         'si',
294         'Beta',
295         'sj',
296         'Gamma',
297         'sk',
298         'Delta',
299         'sl',
300         'Epsilon',
301         'sm',
302         'Zeta',
303         'sn',
304         'Eta',
305         'so',
306         'Theta',
307         'sp',
308         'Iota',
309         'sq',
310         'Kappa',
311         'sr',
312         'Lambda',
313         'ss',
314         'Mu',
315         'st',
316         'Nu',
317         'su',
318         'Xi',
319         'sv',
320         'Omicron',
321         't0',
322         'Pi',
323         't1',
324         'Rho',
325         't3',
326         'Sigma',
327         't4',
328         'Tau',
329         't5',
330         'Upsilon',
331         't6',
332         'Phi',
333         't7',
334         'Chi',
335         't8',
336         'Psi',
337         't9',
338         'Omega',
339         'th',
340         'alpha',
341         'ti',
342         'beta',
343         'tj',
344         'gamma',
345         'tk',
346         'delta',
347         'tl',
348         'epsilon',
349         'tm',
350         'zeta',
351         'tn',
352         'eta',
353         'to',
354         'theta',
355         'tp',
356         'iota',
357         'tq',
358         'kappa',
359         'tr',
360         'lambda',
361         'ts',
362         'mu',
363         'tt',
364         'nu',
365         'tu',
366         'xi',
367         'tv',
368         'omicron',
369         'u0',
370         'pi',
371         'u1',
372         'rho',
373         'u2',
374         'sigmaf',
375         'u3',
376         'sigma',
377         'u4',
378         'tau',
379         'u5',
380         'upsilon',
381         'u6',
382         'phi',
383         'u7',
384         'chi',
385         'u8',
386         'psi',
387         'u9',
388         'omega',
389         'uh',
390         'thetasym',
391         'ui',
392         'upsih',
393         'um',
394         'piv',
395         '812',
396         'bull',
397         '816',
398         'hellip',
399         '81i',
400         'prime',
401         '81j',
402         'Prime',
403         '81u',
404         'oline',
405         '824',
406         'frasl',
407         '88o',
408         'weierp',
409         '88h',
410         'image',
411         '88s',
412         'real',
413         '892',
414         'trade',
415         '89l',
416         'alefsym',
417         '8cg',
418         'larr',
419         '8ch',
420         'uarr',
421         '8ci',
422         'rarr',
423         '8cj',
424         'darr',
425         '8ck',
426         'harr',
427         '8dl',
428         'crarr',
429         '8eg',
430         'lArr',
431         '8eh',
432         'uArr',
433         '8ei',
434         'rArr',
435         '8ej',
436         'dArr',
437         '8ek',
438         'hArr',
439         '8g0',
440         'forall',
441         '8g2',
442         'part',
443         '8g3',
444         'exist',
445         '8g5',
446         'empty',
447         '8g7',
448         'nabla',
449         '8g8',
450         'isin',
451         '8g9',
452         'notin',
453         '8gb',
454         'ni',
455         '8gf',
456         'prod',
457         '8gh',
458         'sum',
459         '8gi',
460         'minus',
461         '8gn',
462         'lowast',
463         '8gq',
464         'radic',
465         '8gt',
466         'prop',
467         '8gu',
468         'infin',
469         '8h0',
470         'ang',
471         '8h7',
472         'and',
473         '8h8',
474         'or',
475         '8h9',
476         'cap',
477         '8ha',
478         'cup',
479         '8hb',
480         'int',
481         '8hk',
482         'there4',
483         '8hs',
484         'sim',
485         '8i5',
486         'cong',
487         '8i8',
488         'asymp',
489         '8j0',
490         'ne',
491         '8j1',
492         'equiv',
493         '8j4',
494         'le',
495         '8j5',
496         'ge',
497         '8k2',
498         'sub',
499         '8k3',
500         'sup',
501         '8k4',
502         'nsub',
503         '8k6',
504         'sube',
505         '8k7',
506         'supe',
507         '8kl',
508         'oplus',
509         '8kn',
510         'otimes',
511         '8l5',
512         'perp',
513         '8m5',
514         'sdot',
515         '8o8',
516         'lceil',
517         '8o9',
518         'rceil',
519         '8oa',
520         'lfloor',
521         '8ob',
522         'rfloor',
523         '8p9',
524         'lang',
525         '8pa',
526         'rang',
527         '9ea',
528         'loz',
529         '9j0',
530         'spades',
531         '9j3',
532         'clubs',
533         '9j5',
534         'hearts',
535         '9j6',
536         'diams',
537         'ai',
538         'OElig',
539         'aj',
540         'oelig',
541         'b0',
542         'Scaron',
543         'b1',
544         'scaron',
545         'bo',
546         'Yuml',
547         'm6',
548         'circ',
549         'ms',
550         'tilde',
551         '802',
552         'ensp',
553         '803',
554         'emsp',
555         '809',
556         'thinsp',
557         '80c',
558         'zwnj',
559         '80d',
560         'zwj',
561         '80e',
562         'lrm',
563         '80f',
564         'rlm',
565         '80j',
566         'ndash',
567         '80k',
568         'mdash',
569         '80o',
570         'lsquo',
571         '80p',
572         'rsquo',
573         '80q',
574         'sbquo',
575         '80s',
576         'ldquo',
577         '80t',
578         'rdquo',
579         '80u',
580         'bdquo',
581         '810',
582         'dagger',
583         '811',
584         'Dagger',
585         '81g',
586         'permil',
587         '81p',
588         'lsaquo',
589         '81q',
590         'rsaquo',
591         '85c',
592         'euro'
593     ],
594
595          
596     /**
597      * Encodes the specified string using raw entities. This means only the required XML base entities will be encoded.
598      *
599      * @method encodeRaw
600      * @param {String} text Text to encode.
601      * @param {Boolean} attr Optional flag to specify if the text is attribute contents.
602      * @return {String} Entity encoded text.
603      */
604     encodeRaw: function(text, attr)
605     {
606         var t = this;
607         return text.replace(attr ? this.attrsCharsRegExp : this.textCharsRegExp, function(chr) {
608             return t.baseEntities[chr] || chr;
609         });
610     },
611     /**
612      * Encoded the specified text with both the attributes and text entities. This function will produce larger text contents
613      * since it doesn't know if the context is within a attribute or text node. This was added for compatibility
614      * and is exposed as the DOMUtils.encode function.
615      *
616      * @method encodeAllRaw
617      * @param {String} text Text to encode.
618      * @return {String} Entity encoded text.
619      */
620     encodeAllRaw: function(text) {
621         var t = this;
622         return ('' + text).replace(this.rawCharsRegExp, function(chr) {
623             return t.baseEntities[chr] || chr;
624         });
625     },
626     /**
627      * Encodes the specified string using numeric entities. The core entities will be
628      * encoded as named ones but all non lower ascii characters will be encoded into numeric entities.
629      *
630      * @method encodeNumeric
631      * @param {String} text Text to encode.
632      * @param {Boolean} attr Optional flag to specify if the text is attribute contents.
633      * @return {String} Entity encoded text.
634      */
635     encodeNumeric: function(text, attr) {
636         var t = this;
637         return text.replace(attr ? this.attrsCharsRegExp : this.textCharsRegExp, function(chr) {
638             // Multi byte sequence convert it to a single entity
639             if (chr.length > 1) {
640                 return '&#' + (1024 * (chr.charCodeAt(0) - 55296) + (chr.charCodeAt(1) - 56320) + 65536) + ';';
641             }
642             return t.baseEntities[chr] || '&#' + chr.charCodeAt(0) + ';';
643         });
644     },
645     /**
646      * Encodes the specified string using named entities. The core entities will be encoded
647      * as named ones but all non lower ascii characters will be encoded into named entities.
648      *
649      * @method encodeNamed
650      * @param {String} text Text to encode.
651      * @param {Boolean} attr Optional flag to specify if the text is attribute contents.
652      * @param {Object} entities Optional parameter with entities to use.
653      * @return {String} Entity encoded text.
654      */
655     encodeNamed: function(text, attr, entities) {
656         var t = this;
657         entities = entities || this.namedEntities;
658         return text.replace(attr ? this.attrsCharsRegExp : this.textCharsRegExp, function(chr) {
659             return t.baseEntities[chr] || entities[chr] || chr;
660         });
661     },
662     /**
663      * Returns an encode function based on the name(s) and it's optional entities.
664      *
665      * @method getEncodeFunc
666      * @param {String} name Comma separated list of encoders for example named,numeric.
667      * @param {String} entities Optional parameter with entities to use instead of the built in set.
668      * @return {function} Encode function to be used.
669      */
670     getEncodeFunc: function(name, entities) {
671         entities = this.buildEntitiesLookup(entities) || this.namedEntities;
672         var t = this;
673         function encodeNamedAndNumeric(text, attr) {
674             return text.replace(attr ? t.attrsCharsRegExp : t.textCharsRegExp, function(chr) {
675                 return t.baseEntities[chr] || entities[chr] || '&#' + chr.charCodeAt(0) + ';' || chr;
676             });
677         }
678
679         function encodeCustomNamed(text, attr) {
680             return t.encodeNamed(text, attr, entities);
681         }
682         // Replace + with , to be compatible with previous TinyMCE versions
683         name = this.makeMap(name.replace(/\+/g, ','));
684         // Named and numeric encoder
685         if (name.named && name.numeric) {
686             return this.encodeNamedAndNumeric;
687         }
688         // Named encoder
689         if (name.named) {
690             // Custom names
691             if (entities) {
692                 return encodeCustomNamed;
693             }
694             return this.encodeNamed;
695         }
696         // Numeric
697         if (name.numeric) {
698             return this.encodeNumeric;
699         }
700         // Raw encoder
701         return this.encodeRaw;
702     },
703     /**
704      * Decodes the specified string, this will replace entities with raw UTF characters.
705      *
706      * @method decode
707      * @param {String} text Text to entity decode.
708      * @return {String} Entity decoded string.
709      */
710     decode: function(text)
711     {
712         var  t = this;
713         return text.replace(this.entityRegExp, function(all, numeric) {
714             if (numeric) {
715                 numeric = 'x' === numeric.charAt(0).toLowerCase() ? parseInt(numeric.substr(1), 16) : parseInt(numeric, 10);
716                 // Support upper UTF
717                 if (numeric > 65535) {
718                     numeric -= 65536;
719                     return String.fromCharCode(55296 + (numeric >> 10), 56320 + (1023 & numeric));
720                 }
721                 return t.asciiMap[numeric] || String.fromCharCode(numeric);
722             }
723             return t.reverseEntities[all] || t.namedEntities[all] || t.nativeDecode(all);
724         });
725     },
726     nativeDecode : function (text) {
727         return text;
728     },
729     makeMap : function (items, delim, map) {
730                 var i;
731                 items = items || [];
732                 delim = delim || ',';
733                 if (typeof items == "string") {
734                         items = items.split(delim);
735                 }
736                 map = map || {};
737                 i = items.length;
738                 while (i--) {
739                         map[items[i]] = {};
740                 }
741                 return map;
742         }
743 };
744     
745     
746     
747 Roo.htmleditor.TidyEntities.init();