plugin.js 8.0 KB

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