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