      1 /*
      2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
      3 For licensing, see LICENSE.html or http://ckeditor.com/license
      4 */
      5
      6 (function()
      7 {
      8 	var entities =
      9
     10 		// Base HTML entities.
     11 		'nbsp,gt,lt,quot,' +
     12
     13 		// Latin-1 Entities
     14 		'iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,' +
     15 		'not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,' +
     16 		'cedil,sup1,ordm,raquo,frac14,frac12,frac34,iquest,times,divide,' +
     17
     18 		// Symbols
     19 		'fnof,bull,hellip,prime,Prime,oline,frasl,weierp,image,real,trade,' +
     20 		'alefsym,larr,uarr,rarr,darr,harr,crarr,lArr,uArr,rArr,dArr,hArr,' +
     21 		'forall,part,exist,empty,nabla,isin,notin,ni,prod,sum,minus,lowast,' +
     22 		'radic,prop,infin,ang,and,or,cap,cup,int,there4,sim,cong,asymp,ne,' +
     23 		'equiv,le,ge,sub,sup,nsub,sube,supe,oplus,otimes,perp,sdot,lceil,' +
     24 		'rceil,lfloor,rfloor,lang,rang,loz,spades,clubs,hearts,diams,' +
     25
     26 		// Other Special Characters
     27 		'circ,tilde,ensp,emsp,thinsp,zwnj,zwj,lrm,rlm,ndash,mdash,lsquo,' +
     28 		'rsquo,sbquo,ldquo,rdquo,bdquo,dagger,Dagger,permil,lsaquo,rsaquo,' +
     29 		'euro';
     30
     31 	// Latin Letters Entities
     32 	var latin =
     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,szlig,' +
     36 		'agrave,aacute,acirc,atilde,auml,aring,aelig,ccedil,egrave,eacute,' +
     37 		'ecirc,euml,igrave,iacute,icirc,iuml,eth,ntilde,ograve,oacute,ocirc,' +
     38 		'otilde,ouml,oslash,ugrave,uacute,ucirc,uuml,yacute,thorn,yuml,' +
     39 		'OElig,oelig,Scaron,scaron,Yuml';
     40
     41 	// Greek Letters Entities.
     42 	var greek =
     43 		'Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,' +
     44 		'Nu,Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,Omega,alpha,' +
     45 		'beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa,lambda,mu,nu,xi,' +
     46 		'omicron,pi,rho,sigmaf,sigma,tau,upsilon,phi,chi,psi,omega,thetasym,' +
     47 		'upsih,piv';
     48
     49 	function buildTable( entities )
     50 	{
     51 		var table = {},
     52 			regex = [];
     53
     54 		// Entities that the browsers DOM don't transform to the final char
     55 		// automatically.
     56 		var specialTable =
     57 			{
     58 				nbsp	: '\u00A0',		// IE | FF
     59 				shy		: '\u00AD',		// IE
     60 				gt		: '\u003E',		// IE | FF |   --   | Opera
     61 				lt		: '\u003C'		// IE | FF | Safari | Opera
     62 			};
     63
     64 		entities = entities.replace( /\b(nbsp|shy|gt|lt|amp)(?:,|$)/g, function( match, entity )
     65 			{
     66 				table[ specialTable[ entity ] ] = '&' + entity + ';';
     67 				regex.push( specialTable[ entity ] );
     68 				return '';
     69 			});
     70
     71 		// Transforms the entities string into an array.
     72 		entities = entities.split( ',' );
     73
     74 		// Put all entities inside a DOM element, transforming them to their
     75 		// final chars.
     76 		var div = document.createElement( 'div' ),
     77 			chars;
     78 		div.innerHTML = '&' + entities.join( ';&' ) + ';';
     79 		chars = div.innerHTML;
     80 		div = null;
     81
     82 		// Add all chars to the table.
     83 		for ( var i = 0 ; i < chars.length ; i++ )
     84 		{
     85 			var charAt = chars.charAt( i );
     86 			table[ charAt ] = '&' + entities[ i ] + ';';
     87 			regex.push( charAt );
     88 		}
     89
     90 		table.regex = regex.join( '' );
     91
     92 		return table;
     93 	}
     94
     95 	CKEDITOR.plugins.add( 'entities',
     96 	{
     97 		afterInit : function( editor )
     98 		{
     99 			var config = editor.config;
    100
    101 			if ( !config.entities )
    102 				return;
    103
    104 			var dataProcessor = editor.dataProcessor,
    105 				htmlFilter = dataProcessor && dataProcessor.htmlFilter;
    106
    107 			if ( htmlFilter )
    108 			{
    109 				var selectedEntities = entities;
    110
    111 				if ( config.entities_latin )
    112 					selectedEntities += ',' + latin;
    113
    114 				if ( config.entities_greek )
    115 					selectedEntities += ',' + greek;
    116
    117 				if ( config.entities_additional )
    118 					selectedEntities += ',' + config.entities_additional;
    119
    120 				var entitiesTable = buildTable( selectedEntities );
    121
    122 				// Create the Regex used to find entities in the text.
    123 				var entitiesRegex = '[' + entitiesTable.regex + ']';
    124 				delete entitiesTable.regex;
    125
    126 				if ( config.entities_processNumerical )
    127 					entitiesRegex = '[^ -~]|' + entitiesRegex ;
    128
    129 				entitiesRegex = new RegExp( entitiesRegex, 'g' );
    130
    131 				function getChar( character )
    132 				{
    133 					return entitiesTable[ character ] || ( '&#' + character.charCodeAt(0) + ';' );
    134 				}
    135
    136 				htmlFilter.addRules(
    137 					{
    138 						text : function( text )
    139 						{
    140 							return text.replace( entitiesRegex, getChar );
    141 						}
    142 					});
    143 			}
    144 		}
    145 	});
    146 })();
    147
    148 CKEDITOR.config.entities = true;
    149 CKEDITOR.config.entities_latin = true;
    150 CKEDITOR.config.entities_greek = true;
    151 CKEDITOR.config.entities_processNumerical = false;
    152 CKEDITOR.config.entities_additional = '#39';
    153