JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / entities / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, 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                                 apos    : '\u0027',             // IE\r
67                                 quot    : '\u0022'              // IE\r
68                         };\r
69 \r
70                 entities = entities.replace( /\b(nbsp|shy|gt|lt|amp|apos|quot)(?:,|$)/g, function( match, entity )\r
71                         {\r
72                                 var org = reverse ? '&' + entity + ';' : specialTable[ entity ],\r
73                                         result = reverse ? specialTable[ entity ] : '&' + entity + ';';\r
74 \r
75                                 table[ org ] = result;\r
76                                 regex.push( org );\r
77                                 return '';\r
78                         });\r
79 \r
80                 if ( !reverse && entities )\r
81                 {\r
82                         // Transforms the entities string into an array.\r
83                         entities = entities.split( ',' );\r
84 \r
85                         // Put all entities inside a DOM element, transforming them to their\r
86                         // final chars.\r
87                         var div = document.createElement( 'div' ),\r
88                                 chars;\r
89                         div.innerHTML = '&' + entities.join( ';&' ) + ';';\r
90                         chars = div.innerHTML;\r
91                         div = null;\r
92 \r
93                         // Add all chars to the table.\r
94                         for ( var i = 0 ; i < chars.length ; i++ )\r
95                         {\r
96                                 var charAt = chars.charAt( i );\r
97                                 table[ charAt ] = '&' + entities[ i ] + ';';\r
98                                 regex.push( charAt );\r
99                         }\r
100                 }\r
101 \r
102                 table.regex = regex.join( reverse ? '|' : '' );\r
103 \r
104                 return table;\r
105         }\r
106 \r
107         CKEDITOR.plugins.add( 'entities',\r
108         {\r
109                 afterInit : function( editor )\r
110                 {\r
111                         var config = editor.config;\r
112 \r
113                         var dataProcessor = editor.dataProcessor,\r
114                                 htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
115 \r
116                         if ( htmlFilter )\r
117                         {\r
118                                 // Mandatory HTML base entities.\r
119                                 var selectedEntities = [];\r
120 \r
121                                 if ( config.basicEntities !== false )\r
122                                         selectedEntities.push( htmlbase );\r
123 \r
124                                 if ( config.entities )\r
125                                 {\r
126                                         if ( selectedEntities.length )\r
127                                                 selectedEntities.push( entities );\r
128 \r
129                                         if ( config.entities_latin )\r
130                                                 selectedEntities.push( latin );\r
131 \r
132                                         if ( config.entities_greek )\r
133                                                 selectedEntities.push( greek );\r
134 \r
135                                         if ( config.entities_additional )\r
136                                                 selectedEntities.push( config.entities_additional );\r
137                                 }\r
138 \r
139                                 var entitiesTable = buildTable( selectedEntities.join( ',' ) );\r
140 \r
141                                 // Create the Regex used to find entities in the text, leave it matches nothing if entities are empty.\r
142                                 var entitiesRegex = entitiesTable.regex ? '[' + entitiesTable.regex + ']' : 'a^';\r
143                                 delete entitiesTable.regex;\r
144 \r
145                                 if ( config.entities && config.entities_processNumerical )\r
146                                         entitiesRegex = '[^ -~]|' + entitiesRegex ;\r
147 \r
148                                 entitiesRegex = new RegExp( entitiesRegex, 'g' );\r
149 \r
150                                 function getEntity( character )\r
151                                 {\r
152                                         return config.entities_processNumerical == 'force' || !entitiesTable[ character ] ?\r
153                                                    '&#' + character.charCodeAt(0) + ';'\r
154                                                         : entitiesTable[ character ];\r
155                                 }\r
156 \r
157                                 // Decode entities that the browsers has transformed\r
158                                 // at first place.\r
159                                 var baseEntitiesTable = buildTable( [ htmlbase, 'shy' ].join( ',' ) , true ),\r
160                                         baseEntitiesRegex = new RegExp( baseEntitiesTable.regex, 'g' );\r
161 \r
162                                 function getChar( character )\r
163                                 {\r
164                                         return baseEntitiesTable[ character ];\r
165                                 }\r
166 \r
167                                 htmlFilter.addRules(\r
168                                         {\r
169                                                 text : function( text )\r
170                                                 {\r
171                                                         return text.replace( baseEntitiesRegex, getChar )\r
172                                                                         .replace( entitiesRegex, getEntity );\r
173                                                 }\r
174                                         });\r
175                         }\r
176                 }\r
177         });\r
178 })();\r
179 \r
180 /**\r
181  * Whether to escape basic HTML entities in the document, including:\r
182  * <ul>\r
183  * <li><code>nbsp</code></li>\r
184  * <li><code>gt</code></li>\r
185  * <li><code>lt</code></li>\r
186  * <li><code>amp</code></li>\r
187  * </ul>\r
188  * <strong>Note:</strong> It should not be subject to change unless when outputting a non-HTML data format like BBCode.\r
189  * @type Boolean\r
190  * @default <code>true</code>\r
191  * @example\r
192  * config.basicEntities = false;\r
193  */\r
194 CKEDITOR.config.basicEntities = true;\r
195 \r
196 /**\r
197  * Whether to use HTML entities in the output.\r
198  * @name CKEDITOR.config.entities\r
199  * @type Boolean\r
200  * @default <code>true</code>\r
201  * @example\r
202  * config.entities = false;\r
203  */\r
204 CKEDITOR.config.entities = true;\r
205 \r
206 /**\r
207  * Whether to convert some Latin characters (Latin alphabet No&#46; 1, ISO 8859-1)\r
208  * to HTML entities. The list of entities can be found in the\r
209  * <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
210  * @name CKEDITOR.config.entities_latin\r
211  * @type Boolean\r
212  * @default <code>true</code>\r
213  * @example\r
214  * config.entities_latin = false;\r
215  */\r
216 CKEDITOR.config.entities_latin = true;\r
217 \r
218 /**\r
219  * Whether to convert some symbols, mathematical symbols, and Greek letters to\r
220  * HTML entities. This may be more relevant for users typing text written in Greek.\r
221  * The list of entities can be found in the\r
222  * <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
223  * @name CKEDITOR.config.entities_greek\r
224  * @type Boolean\r
225  * @default <code>true</code>\r
226  * @example\r
227  * config.entities_greek = false;\r
228  */\r
229 CKEDITOR.config.entities_greek = true;\r
230 \r
231 /**\r
232  * Whether to convert all remaining characters not included in the ASCII\r
233  * character table to their relative decimal numeric representation of HTML entity.\r
234  * When set to <code>force</code>, it will convert all entities into this format.\r
235  * For example the phrase "This is Chinese: &#27721;&#35821;." is output\r
236  * as "This is Chinese: &amp;#27721;&amp;#35821;."\r
237  * @name CKEDITOR.config.entities_processNumerical\r
238  * @type Boolean|String\r
239  * @default <code>false</code>\r
240  * @example\r
241  * config.entities_processNumerical = true;\r
242  * config.entities_processNumerical = 'force';          //Converts from "&nbsp;" into "&#160;";\r
243  */\r
244 \r
245 /**\r
246  * A comma separated list of  additional entities to be used. Entity names\r
247  * or numbers must be used in a form that excludes the "&amp;" prefix and the ";" ending.\r
248  * @name CKEDITOR.config.entities_additional\r
249  * @default <code>'#39'</code>  (The single quote (') character.)\r
250  * @type String\r
251  * @example\r
252  * config.entities_additional = '#1049';                // Adds Cyrillic capital letter Short I (&#1049;).\r
253  */\r
254 CKEDITOR.config.entities_additional = '#39';\r