JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.2
[ckeditor.git] / _source / plugins / entities / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         // Base HTML entities.\r
9         var htmlbase = 'nbsp,gt,lt,amp';\r
10 \r
11         var entities =\r
12                 // Latin-1 Entities\r
13                 'quot,iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,' +\r
14                 'not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,' +\r
15                 'cedil,sup1,ordm,raquo,frac14,frac12,frac34,iquest,times,divide,' +\r
16 \r
17                 // Symbols\r
18                 'fnof,bull,hellip,prime,Prime,oline,frasl,weierp,image,real,trade,' +\r
19                 'alefsym,larr,uarr,rarr,darr,harr,crarr,lArr,uArr,rArr,dArr,hArr,' +\r
20                 'forall,part,exist,empty,nabla,isin,notin,ni,prod,sum,minus,lowast,' +\r
21                 'radic,prop,infin,ang,and,or,cap,cup,int,there4,sim,cong,asymp,ne,' +\r
22                 'equiv,le,ge,sub,sup,nsub,sube,supe,oplus,otimes,perp,sdot,lceil,' +\r
23                 'rceil,lfloor,rfloor,lang,rang,loz,spades,clubs,hearts,diams,' +\r
24 \r
25                 // Other Special Characters\r
26                 'circ,tilde,ensp,emsp,thinsp,zwnj,zwj,lrm,rlm,ndash,mdash,lsquo,' +\r
27                 'rsquo,sbquo,ldquo,rdquo,bdquo,dagger,Dagger,permil,lsaquo,rsaquo,' +\r
28                 'euro';\r
29 \r
30         // Latin Letters Entities\r
31         var latin =\r
32                 'Agrave,Aacute,Acirc,Atilde,Auml,Aring,AElig,Ccedil,Egrave,Eacute,' +\r
33                 'Ecirc,Euml,Igrave,Iacute,Icirc,Iuml,ETH,Ntilde,Ograve,Oacute,Ocirc,' +\r
34                 'Otilde,Ouml,Oslash,Ugrave,Uacute,Ucirc,Uuml,Yacute,THORN,szlig,' +\r
35                 'agrave,aacute,acirc,atilde,auml,aring,aelig,ccedil,egrave,eacute,' +\r
36                 'ecirc,euml,igrave,iacute,icirc,iuml,eth,ntilde,ograve,oacute,ocirc,' +\r
37                 'otilde,ouml,oslash,ugrave,uacute,ucirc,uuml,yacute,thorn,yuml,' +\r
38                 'OElig,oelig,Scaron,scaron,Yuml';\r
39 \r
40         // Greek Letters Entities.\r
41         var greek =\r
42                 'Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,' +\r
43                 'Nu,Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,Omega,alpha,' +\r
44                 'beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa,lambda,mu,nu,xi,' +\r
45                 'omicron,pi,rho,sigmaf,sigma,tau,upsilon,phi,chi,psi,omega,thetasym,' +\r
46                 'upsih,piv';\r
47 \r
48         /**\r
49          * Create a mapping table between one character and its entity form from a list of entity names.\r
50          * @param reverse {Boolean} Whether to create a reverse map from the entity string form to an actual character.\r
51          */\r
52         function buildTable( entities, reverse )\r
53         {\r
54                 var table = {},\r
55                         regex = [];\r
56 \r
57                 // Entities that the browsers DOM don't transform to the final char\r
58                 // automatically.\r
59                 var specialTable =\r
60                         {\r
61                                 nbsp    : '\u00A0',             // IE | FF\r
62                                 shy             : '\u00AD',             // IE\r
63                                 gt              : '\u003E',             // IE | FF |   --   | Opera\r
64                                 lt              : '\u003C',             // IE | FF | Safari | Opera\r
65                                 amp : '\u0026'          // ALL\r
66                         };\r
67 \r
68                 entities = entities.replace( /\b(nbsp|shy|gt|lt|amp)(?:,|$)/g, function( match, entity )\r
69                         {\r
70                                 var org = reverse ? '&' + entity + ';' : specialTable[ entity ],\r
71                                         result = reverse ? specialTable[ entity ] : '&' + entity + ';';\r
72 \r
73                                 table[ org ] = result;\r
74                                 regex.push( org );\r
75                                 return '';\r
76                         });\r
77 \r
78                 if ( !reverse && entities )\r
79                 {\r
80                         // Transforms the entities string into an array.\r
81                         entities = entities.split( ',' );\r
82 \r
83                         // Put all entities inside a DOM element, transforming them to their\r
84                         // final chars.\r
85                         var div = document.createElement( 'div' ),\r
86                                 chars;\r
87                         div.innerHTML = '&' + entities.join( ';&' ) + ';';\r
88                         chars = div.innerHTML;\r
89                         div = null;\r
90 \r
91                         // Add all chars to the table.\r
92                         for ( var i = 0 ; i < chars.length ; i++ )\r
93                         {\r
94                                 var charAt = chars.charAt( i );\r
95                                 table[ charAt ] = '&' + entities[ i ] + ';';\r
96                                 regex.push( charAt );\r
97                         }\r
98                 }\r
99 \r
100                 table.regex = regex.join( reverse ? '|' : '' );\r
101 \r
102                 return table;\r
103         }\r
104 \r
105         CKEDITOR.plugins.add( 'entities',\r
106         {\r
107                 afterInit : function( editor )\r
108                 {\r
109                         var config = editor.config;\r
110 \r
111                         var dataProcessor = editor.dataProcessor,\r
112                                 htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
113 \r
114                         if ( htmlFilter )\r
115                         {\r
116                                 // Mandatory HTML base entities.\r
117                                 var selectedEntities = '';\r
118 \r
119                                 if ( config.basicEntities !== false )\r
120                                         selectedEntities += htmlbase;\r
121 \r
122                                 if ( config.entities )\r
123                                 {\r
124                                         selectedEntities += ',' + entities;\r
125                                         if ( config.entities_latin )\r
126                                                 selectedEntities += ',' + latin;\r
127 \r
128                                         if ( config.entities_greek )\r
129                                                 selectedEntities += ',' + greek;\r
130 \r
131                                         if ( config.entities_additional )\r
132                                                 selectedEntities += ',' + config.entities_additional;\r
133                                 }\r
134 \r
135                                 var entitiesTable = buildTable( selectedEntities );\r
136 \r
137                                 // Create the Regex used to find entities in the text, leave it matches nothing if entities are empty.\r
138                                 var entitiesRegex = entitiesTable.regex ? '[' + entitiesTable.regex + ']' : 'a^';\r
139                                 delete entitiesTable.regex;\r
140 \r
141                                 if ( config.entities && config.entities_processNumerical )\r
142                                         entitiesRegex = '[^ -~]|' + entitiesRegex ;\r
143 \r
144                                 entitiesRegex = new RegExp( entitiesRegex, 'g' );\r
145 \r
146                                 function getEntity( character )\r
147                                 {\r
148                                         return config.entities_processNumerical == 'force' || !entitiesTable[ character ] ?\r
149                                                    '&#' + character.charCodeAt(0) + ';'\r
150                                                         : entitiesTable[ character ];\r
151                                 }\r
152 \r
153                                 // Decode entities that the browsers has transformed\r
154                                 // at first place.\r
155                                 var baseEntitiesTable = buildTable( [ htmlbase, 'shy' ].join( ',' ) , true ),\r
156                                         baseEntitiesRegex = new RegExp( baseEntitiesTable.regex, 'g' );\r
157 \r
158                                 function getChar( character )\r
159                                 {\r
160                                         return baseEntitiesTable[ character ];\r
161                                 }\r
162 \r
163                                 htmlFilter.addRules(\r
164                                         {\r
165                                                 text : function( text )\r
166                                                 {\r
167                                                         return text.replace( baseEntitiesRegex, getChar )\r
168                                                                         .replace( entitiesRegex, getEntity );\r
169                                                 }\r
170                                         });\r
171                         }\r
172                 }\r
173         });\r
174 })();\r
175 \r
176 /**\r
177  * Whether to escape basic HTML entities in the document, including:\r
178  * <ul>\r
179  * <li><code>nbsp</code></li>\r
180  * <li><code>gt</code></li>\r
181  * <li><code>lt</code></li>\r
182  * <li><code>amp</code></li>\r
183  * </ul>\r
184  * <strong>Note:</strong> It should not be subject to change unless when outputting a non-HTML data format like BBCode.\r
185  * @type Boolean\r
186  * @default <code>true</code>\r
187  * @example\r
188  * config.basicEntities = false;\r
189  */\r
190 CKEDITOR.config.basicEntities = true;\r
191 \r
192 /**\r
193  * Whether to use HTML entities in the output.\r
194  * @name CKEDITOR.config.entities\r
195  * @type Boolean\r
196  * @default <code>true</code>\r
197  * @example\r
198  * config.entities = false;\r
199  */\r
200 CKEDITOR.config.entities = true;\r
201 \r
202 /**\r
203  * Whether to convert some Latin characters (Latin alphabet No&#46; 1, ISO 8859-1)\r
204  * to HTML entities. The list of entities can be found in the\r
205  * <a href="http://www.w3.org/TR/html4/sgml/entities.html#h-24.2.1">W3C HTML 4.01 Specification, section 24.2.1</a>.\r
206  * @name CKEDITOR.config.entities_latin\r
207  * @type Boolean\r
208  * @default <code>true</code>\r
209  * @example\r
210  * config.entities_latin = false;\r
211  */\r
212 CKEDITOR.config.entities_latin = true;\r
213 \r
214 /**\r
215  * Whether to convert some symbols, mathematical symbols, and Greek letters to\r
216  * HTML entities. This may be more relevant for users typing text written in Greek.\r
217  * The list of entities can be found in the\r
218  * <a href="http://www.w3.org/TR/html4/sgml/entities.html#h-24.3.1">W3C HTML 4.01 Specification, section 24.3.1</a>.\r
219  * @name CKEDITOR.config.entities_greek\r
220  * @type Boolean\r
221  * @default <code>true</code>\r
222  * @example\r
223  * config.entities_greek = false;\r
224  */\r
225 CKEDITOR.config.entities_greek = true;\r
226 \r
227 /**\r
228  * Whether to convert all remaining characters not included in the ASCII\r
229  * character table to their relative decimal numeric representation of HTML entity.\r
230  * When set to <code>force</code>, it will convert all entities into this format.\r
231  * For example the phrase "This is Chinese: &#27721;&#35821;." is output\r
232  * as "This is Chinese: &amp;#27721;&amp;#35821;."\r
233  * @name CKEDITOR.config.entities_processNumerical\r
234  * @type Boolean|String\r
235  * @default <code>false</code>\r
236  * @example\r
237  * config.entities_processNumerical = true;\r
238  * config.entities_processNumerical = 'force';          //Converts from "&nbsp;" into "&#160;";\r
239  */\r
240 \r
241 /**\r
242  * A comma separated list of  additional entities to be used. Entity names\r
243  * or numbers must be used in a form that excludes the "&amp;" prefix and the ";" ending.\r
244  * @name CKEDITOR.config.entities_additional\r
245  * @default <code>'#39'</code>  (The single quote (') character.)\r
246  * @type String\r
247  * @example\r
248  * config.entities_additional = '#1049';                // Adds Cyrillic capital letter Short I (&#1049;).\r
249  */\r
250 CKEDITOR.config.entities_additional = '#39';\r