JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
73d85c19ee7013c31935876d43b2b0e8182d1fa3
[ckeditor.git] / _source / plugins / entities / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, 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         var entities =\r
9 \r
10                 // Base HTML entities.\r
11                 'nbsp,gt,lt,quot,' +\r
12 \r
13                 // Latin-1 Entities\r
14                 'iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,' +\r
15                 'not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,' +\r
16                 'cedil,sup1,ordm,raquo,frac14,frac12,frac34,iquest,times,divide,' +\r
17 \r
18                 // Symbols\r
19                 'fnof,bull,hellip,prime,Prime,oline,frasl,weierp,image,real,trade,' +\r
20                 'alefsym,larr,uarr,rarr,darr,harr,crarr,lArr,uArr,rArr,dArr,hArr,' +\r
21                 'forall,part,exist,empty,nabla,isin,notin,ni,prod,sum,minus,lowast,' +\r
22                 'radic,prop,infin,ang,and,or,cap,cup,int,there4,sim,cong,asymp,ne,' +\r
23                 'equiv,le,ge,sub,sup,nsub,sube,supe,oplus,otimes,perp,sdot,lceil,' +\r
24                 'rceil,lfloor,rfloor,lang,rang,loz,spades,clubs,hearts,diams,' +\r
25 \r
26                 // Other Special Characters\r
27                 'circ,tilde,ensp,emsp,thinsp,zwnj,zwj,lrm,rlm,ndash,mdash,lsquo,' +\r
28                 'rsquo,sbquo,ldquo,rdquo,bdquo,dagger,Dagger,permil,lsaquo,rsaquo,' +\r
29                 'euro';\r
30 \r
31         // Latin Letters Entities\r
32         var latin =\r
33                 'Agrave,Aacute,Acirc,Atilde,Auml,Aring,AElig,Ccedil,Egrave,Eacute,' +\r
34                 'Ecirc,Euml,Igrave,Iacute,Icirc,Iuml,ETH,Ntilde,Ograve,Oacute,Ocirc,' +\r
35                 'Otilde,Ouml,Oslash,Ugrave,Uacute,Ucirc,Uuml,Yacute,THORN,szlig,' +\r
36                 'agrave,aacute,acirc,atilde,auml,aring,aelig,ccedil,egrave,eacute,' +\r
37                 'ecirc,euml,igrave,iacute,icirc,iuml,eth,ntilde,ograve,oacute,ocirc,' +\r
38                 'otilde,ouml,oslash,ugrave,uacute,ucirc,uuml,yacute,thorn,yuml,' +\r
39                 'OElig,oelig,Scaron,scaron,Yuml';\r
40 \r
41         // Greek Letters Entities.\r
42         var greek =\r
43                 'Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,' +\r
44                 'Nu,Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,Omega,alpha,' +\r
45                 'beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa,lambda,mu,nu,xi,' +\r
46                 'omicron,pi,rho,sigmaf,sigma,tau,upsilon,phi,chi,psi,omega,thetasym,' +\r
47                 'upsih,piv';\r
48 \r
49         function buildTable( entities )\r
50         {\r
51                 var table = {},\r
52                         regex = [];\r
53 \r
54                 // Entities that the browsers DOM don't transform to the final char\r
55                 // automatically.\r
56                 var specialTable =\r
57                         {\r
58                                 nbsp    : '\u00A0',             // IE | FF\r
59                                 shy             : '\u00AD',             // IE\r
60                                 gt              : '\u003E',             // IE | FF |   --   | Opera\r
61                                 lt              : '\u003C'              // IE | FF | Safari | Opera\r
62                         };\r
63 \r
64                 entities = entities.replace( /\b(nbsp|shy|gt|lt|amp)(?:,|$)/g, function( match, entity )\r
65                         {\r
66                                 table[ specialTable[ entity ] ] = '&' + entity + ';';\r
67                                 regex.push( specialTable[ entity ] );\r
68                                 return '';\r
69                         });\r
70 \r
71                 // Transforms the entities string into an array.\r
72                 entities = entities.split( ',' );\r
73 \r
74                 // Put all entities inside a DOM element, transforming them to their\r
75                 // final chars.\r
76                 var div = document.createElement( 'div' ),\r
77                         chars;\r
78                 div.innerHTML = '&' + entities.join( ';&' ) + ';';\r
79                 chars = div.innerHTML;\r
80                 div = null;\r
81 \r
82                 // Add all chars to the table.\r
83                 for ( var i = 0 ; i < chars.length ; i++ )\r
84                 {\r
85                         var charAt = chars.charAt( i );\r
86                         table[ charAt ] = '&' + entities[ i ] + ';';\r
87                         regex.push( charAt );\r
88                 }\r
89 \r
90                 table.regex = regex.join( '' );\r
91 \r
92                 return table;\r
93         }\r
94 \r
95         CKEDITOR.plugins.add( 'entities',\r
96         {\r
97                 afterInit : function( editor )\r
98                 {\r
99                         var config = editor.config;\r
100 \r
101                         if ( !config.entities )\r
102                                 return;\r
103 \r
104                         var dataProcessor = editor.dataProcessor,\r
105                                 htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
106 \r
107                         if ( htmlFilter )\r
108                         {\r
109                                 var selectedEntities = entities;\r
110 \r
111                                 if ( config.entities_latin )\r
112                                         selectedEntities += ',' + latin;\r
113 \r
114                                 if ( config.entities_greek )\r
115                                         selectedEntities += ',' + greek;\r
116 \r
117                                 if ( config.entities_additional )\r
118                                         selectedEntities += ',' + config.entities_additional;\r
119 \r
120                                 var entitiesTable = buildTable( selectedEntities );\r
121 \r
122                                 // Create the Regex used to find entities in the text.\r
123                                 var entitiesRegex = '[' + entitiesTable.regex + ']';\r
124                                 delete entitiesTable.regex;\r
125 \r
126                                 if ( config.entities_processNumerical )\r
127                                         entitiesRegex = '[^ -~]|' + entitiesRegex ;\r
128 \r
129                                 entitiesRegex = new RegExp( entitiesRegex, 'g' );\r
130 \r
131                                 function getChar( character )\r
132                                 {\r
133                                         return entitiesTable[ character ] || ( '&#' + character.charCodeAt(0) + ';' );\r
134                                 }\r
135 \r
136                                 htmlFilter.addRules(\r
137                                         {\r
138                                                 text : function( text )\r
139                                                 {\r
140                                                         return text.replace( entitiesRegex, getChar );\r
141                                                 }\r
142                                         });\r
143                         }\r
144                 }\r
145         });\r
146 })();\r
147 \r
148 /**\r
149  * Whether to use HTML entities in the output.\r
150  * @type Boolean\r
151  * @default true\r
152  * @example\r
153  * config.entities = false;\r
154  */\r
155 CKEDITOR.config.entities = true;\r
156 \r
157 /**\r
158  * Whether to convert some Latin characters (Latin alphabet No&#46; 1, ISO 8859-1)\r
159  * to HTML entities. The list of entities can be found at the\r
160  * <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
161  * @type Boolean\r
162  * @default true\r
163  * @example\r
164  * config.entities_latin = false;\r
165  */\r
166 CKEDITOR.config.entities_latin = true;\r
167 \r
168 /**\r
169  * Whether to convert some symbols, mathematical symbols, and Greek letters to\r
170  * HTML entities. This may be more relevant for users typing text written in Greek.\r
171  * The list of entities can be found at the\r
172  * <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
173  * @type Boolean\r
174  * @default true\r
175  * @example\r
176  * config.entities_greek = false;\r
177  */\r
178 CKEDITOR.config.entities_greek = true;\r
179 \r
180 /**\r
181  * Whether to convert all remaining characters, not comprised in the ASCII\r
182  * character table, to their relative numeric representation of HTML entity.\r
183  * For example, the phrase "This is Chinese: &#27721;&#35821;." is outputted\r
184  * as "This is Chinese: &amp;#27721;&amp;#35821;."\r
185  * @type Boolean\r
186  * @default false\r
187  * @example\r
188  * config.entities_processNumerical = true;\r
189  */\r
190 CKEDITOR.config.entities_processNumerical = false;\r
191 \r
192 /**\r
193  * An additional list of entities to be used. It's a string containing each\r
194  * entry separated by a comma. Entities names or number must be used, exclusing\r
195  * the "&amp;" preffix and the ";" termination.\r
196  * @default '#39'  // The single quote (') character.\r
197  * @type String\r
198  * @example\r
199  */\r
200 CKEDITOR.config.entities_additional = '#39';\r