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