Ticket #5769: 5769.patch

File 5769.patch, 4.7 KB (added by Garry Yao, 13 years ago)
  • _source/plugins/entities/plugin.js

     
    1 /*
     1/*
    22Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
    33For licensing, see LICENSE.html or http://ckeditor.com/license
    44*/
    55
    66(function()
    77{
    8         var entities =
    9 
    10                 // Base HTML entities.
    11                 'nbsp,gt,lt,quot,' +
    12 
     8        // Base HTML entities.
     9        var htmlbase = 'nbsp,gt,lt,quot';
     10       
     11        var entities =
    1312                // Latin-1 Entities
    1413                'iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,' +
    1514                'not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,' +
     
    4645                'omicron,pi,rho,sigmaf,sigma,tau,upsilon,phi,chi,psi,omega,thetasym,' +
    4746                'upsih,piv';
    4847
    49         function buildTable( entities )
     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 it's reverse mapping from the entity string form to actual character.
     51         */
     52        function buildTable( entities, reverse )
    5053        {
    5154                var table = {},
    5255                        regex = [];
     
    6366
    6467                entities = entities.replace( /\b(nbsp|shy|gt|lt|amp)(?:,|$)/g, function( match, entity )
    6568                        {
    66                                 table[ specialTable[ entity ] ] = '&' + entity + ';';
    67                                 regex.push( specialTable[ entity ] );
     69                                var org = reverse ? '&' + entity + ';' : specialTable[ entity ],
     70                                        result = reverse ? specialTable[ entity ] : '&' + entity + ';';
     71
     72                                table[ org ] = result;
     73                                regex.push( org );
    6874                                return '';
    6975                        });
    7076
    71                 // Transforms the entities string into an array.
    72                 entities = entities.split( ',' );
     77                if ( !reverse )
     78                {
     79                        // Transforms the entities string into an array.
     80                        entities = entities.split( ',' );
    7381
    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;
     82                        // Put all entities inside a DOM element, transforming them to their
     83                        // final chars.
     84                        var div = document.createElement( 'div' ),
     85                                chars;
     86                        div.innerHTML = '&' + entities.join( ';&' ) + ';';
     87                        chars = div.innerHTML;
     88                        div = null;
    8189
    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                 }
     90                        // Add all chars to the table.
     91                        for ( var i = 0 ; i < chars.length ; i++ )
     92                        {
     93                                var charAt = chars.charAt( i );
     94                                table[ charAt ] = '&' + entities[ i ] + ';';
     95                                regex.push( charAt );
     96                        }
     97                }
    8998
    90                 table.regex = regex.join( '' );
     99                table.regex = regex.join( reverse ? '|' : '' );
    91100
    92101                return table;
    93102        }
     
    98107                {
    99108                        var config = editor.config;
    100109
    101                         if ( !config.entities )
    102                                 return;
    103 
    104110                        var dataProcessor = editor.dataProcessor,
    105111                                htmlFilter = dataProcessor && dataProcessor.htmlFilter;
    106112
     
    108114                        {
    109115                                var selectedEntities = entities;
    110116
    111                                 if ( config.entities_latin )
    112                                         selectedEntities += ',' + latin;
     117                                if ( config.entities )
     118                                {
     119                                        selectedEntities += ',' + htmlbase;
     120
     121                                        if ( config.entities_latin )
     122                                                selectedEntities += ',' + latin;
    113123
    114                                 if ( config.entities_greek )
    115                                         selectedEntities += ',' + greek;
     124                                        if ( config.entities_greek )
     125                                                selectedEntities += ',' + greek;
    116126
    117                                 if ( config.entities_additional )
    118                                         selectedEntities += ',' + config.entities_additional;
     127                                        if ( config.entities_additional )
     128                                                selectedEntities += ',' + config.entities_additional;
     129                                }
    119130
    120                                 var entitiesTable = buildTable( selectedEntities );
     131                                var entitiesTable = buildTable( selectedEntities, !config.entities );
    121132
    122                                 // Create the Regex used to find entities in the text.
    123                                 var entitiesRegex = '[' + entitiesTable.regex + ']';
    124                                 delete entitiesTable.regex;
     133                                var entitiesRegex;
     134                                if ( config.entities )
     135                                {
     136                                        // Create the Regex used to find entities in the text.
     137                                        entitiesRegex = '[' + entitiesTable.regex + ']';
    125138
    126                                 if ( config.entities_processNumerical )
    127                                         entitiesRegex = '[^ -~]|' + entitiesRegex ;
     139                                        if ( config.entities_processNumerical )
     140                                                entitiesRegex = '[^ -~]|' + entitiesRegex ;
     141                                }
     142                                else
     143                                        entitiesRegex = entitiesTable.regex;
    128144
     145                                delete entitiesTable.regex;
    129146                                entitiesRegex = new RegExp( entitiesRegex, 'g' );
    130 
    131147                                function getChar( character )
    132148                                {
    133149                                        return entitiesTable[ character ] || ( '&#' + character.charCodeAt(0) + ';' );
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy