JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
ce0f34e421afe350d9ffaa49a428e9576479d7e3
[ckeditor.git] / _source / plugins / entities / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, 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,quot';\r
10 \r
11         var entities =\r
12                 // Latin-1 Entities\r
13                 '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 it's entity form from a list of entity names.\r
50          * @param reverse {Boolean} Whether create a reverse map from the entity string form to 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                         };\r
66 \r
67                 entities = entities.replace( /\b(nbsp|shy|gt|lt|amp)(?:,|$)/g, function( match, entity )\r
68                         {\r
69                                 var org = reverse ? '&' + entity + ';' : specialTable[ entity ],\r
70                                         result = reverse ? specialTable[ entity ] : '&' + entity + ';';\r
71 \r
72                                 table[ org ] = result;\r
73                                 regex.push( org );\r
74                                 return '';\r
75                         });\r
76 \r
77                 if ( !reverse )\r
78                 {\r
79                         // Transforms the entities string into an array.\r
80                         entities = entities.split( ',' );\r
81 \r
82                         // Put all entities inside a DOM element, transforming them to their\r
83                         // final chars.\r
84                         var div = document.createElement( 'div' ),\r
85                                 chars;\r
86                         div.innerHTML = '&' + entities.join( ';&' ) + ';';\r
87                         chars = div.innerHTML;\r
88                         div = null;\r
89 \r
90                         // Add all chars to the table.\r
91                         for ( var i = 0 ; i < chars.length ; i++ )\r
92                         {\r
93                                 var charAt = chars.charAt( i );\r
94                                 table[ charAt ] = '&' + entities[ i ] + ';';\r
95                                 regex.push( charAt );\r
96                         }\r
97                 }\r
98 \r
99                 table.regex = regex.join( reverse ? '|' : '' );\r
100 \r
101                 return table;\r
102         }\r
103 \r
104         CKEDITOR.plugins.add( 'entities',\r
105         {\r
106                 afterInit : function( editor )\r
107                 {\r
108                         var config = editor.config;\r
109 \r
110                         var dataProcessor = editor.dataProcessor,\r
111                                 htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
112 \r
113                         if ( htmlFilter )\r
114                         {\r
115                                 // Mandatory HTML base entities.\r
116                                 var selectedEntities = htmlbase;\r
117 \r
118                                 if ( config.entities )\r
119                                 {\r
120                                         selectedEntities += ',' + entities;\r
121                                         if ( config.entities_latin )\r
122                                                 selectedEntities += ',' + latin;\r
123 \r
124                                         if ( config.entities_greek )\r
125                                                 selectedEntities += ',' + greek;\r
126 \r
127                                         if ( config.entities_additional )\r
128                                                 selectedEntities += ',' + config.entities_additional;\r
129                                 }\r
130 \r
131                                 var entitiesTable = buildTable( selectedEntities );\r
132 \r
133                                 // Create the Regex used to find entities in the text.\r
134                                 var entitiesRegex = '[' + entitiesTable.regex + ']';\r
135                                 delete entitiesTable.regex;\r
136 \r
137                                 if ( config.entities && config.entities_processNumerical )\r
138                                         entitiesRegex = '[^ -~]|' + entitiesRegex ;\r
139 \r
140                                 entitiesRegex = new RegExp( entitiesRegex, 'g' );\r
141 \r
142                                 function getEntity( character )\r
143                                 {\r
144                                         return config.entities_processNumerical == 'force' || !entitiesTable[ character ] ?\r
145                                                    '&#' + character.charCodeAt(0) + ';'\r
146                                                         : entitiesTable[ character ];\r
147                                 }\r
148 \r
149                                 // Decode entities that the browsers has transformed\r
150                                 // at first place.\r
151                                 var baseEntitiesTable = buildTable( [ htmlbase, 'shy' ].join( ',' ) , true ),\r
152                                         baseEntitiesRegex = new RegExp( baseEntitiesTable.regex, 'g' );\r
153 \r
154                                 function getChar( character )\r
155                                 {\r
156                                         return baseEntitiesTable[ character ];\r
157                                 }\r
158 \r
159                                 htmlFilter.addRules(\r
160                                         {\r
161                                                 text : function( text )\r
162                                                 {\r
163                                                         return text.replace( baseEntitiesRegex, getChar )\r
164                                                                         .replace( entitiesRegex, getEntity );\r
165                                                 }\r
166                                         });\r
167                         }\r
168                 }\r
169         });\r
170 })();\r
171 \r
172 /**\r
173  * Whether to use HTML entities in the output.\r
174  * @type Boolean\r
175  * @default true\r
176  * @example\r
177  * config.entities = false;\r
178  */\r
179 CKEDITOR.config.entities = true;\r
180 \r
181 /**\r
182  * Whether to convert some Latin characters (Latin alphabet No&#46; 1, ISO 8859-1)\r
183  * to HTML entities. The list of entities can be found at the\r
184  * <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
185  * @type Boolean\r
186  * @default true\r
187  * @example\r
188  * config.entities_latin = false;\r
189  */\r
190 CKEDITOR.config.entities_latin = true;\r
191 \r
192 /**\r
193  * Whether to convert some symbols, mathematical symbols, and Greek letters to\r
194  * HTML entities. This may be more relevant for users typing text written in Greek.\r
195  * The list of entities can be found at the\r
196  * <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
197  * @type Boolean\r
198  * @default true\r
199  * @example\r
200  * config.entities_greek = false;\r
201  */\r
202 CKEDITOR.config.entities_greek = true;\r
203 \r
204 /**\r
205  * Whether to convert all remaining characters, not comprised in the ASCII\r
206  * character table, to their relative decimal numeric representation of HTML entity.\r
207  * When specified as the value 'force', it will simply convert all entities into the above form.\r
208  * For example, the phrase "This is Chinese: &#27721;&#35821;." is outputted\r
209  * as "This is Chinese: &amp;#27721;&amp;#35821;."\r
210  * @type Boolean\r
211  * @type Boolean|String\r
212  * @default false\r
213  * @example\r
214  * config.entities_processNumerical = true;\r
215  * config.entities_processNumerical = 'force';          //Convert from "&nbsp;" into "&#160;";\r
216  */\r
217 CKEDITOR.config.entities_processNumerical = false;\r
218 \r
219 /**\r
220  * An additional list of entities to be used. It's a string containing each\r
221  * entry separated by a comma. Entities names or number must be used, exclusing\r
222  * the "&amp;" preffix and the ";" termination.\r
223  * @default '#39'  // The single quote (') character.\r
224  * @type String\r
225  * @example\r
226  */\r
227 CKEDITOR.config.entities_additional = '#39';\r