Ticket #5769: 5769_3.patch

File 5769_3.patch, 5.5 KB (added by Tobiasz Cudnik, 14 years ago)

Updated patch for present revision.

  • _source/plugins/entities/plugin.js

     
    55
    66(function()
    77{
    8         var entities =
    9 
    10                 // Base HTML entities.
    11                 'nbsp,gt,lt,quot,' +
     8        // Base HTML entities.
     9        var htmlbase = 'nbsp,gt,lt,quot';
    1210
     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 create a reverse map 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
    107113                        if ( htmlFilter )
    108114                        {
    109                                 var selectedEntities = entities;
     115                                // Mandatory HTML base entities.
     116                                var selectedEntities = htmlbase;
    110117
    111                                 if ( config.entities_latin )
    112                                         selectedEntities += ',' + latin;
     118                                if ( config.entities )
     119                                {
     120                                        selectedEntities += ',' + entities;
     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
    120131                                var entitiesTable = buildTable( selectedEntities );
    121132
     
    123134                                var entitiesRegex = '[' + entitiesTable.regex + ']';
    124135                                delete entitiesTable.regex;
    125136
    126                                 if ( config.entities_processNumerical )
     137                                if ( config.entities && config.entities_processNumerical )
    127138                                        entitiesRegex = '[^ -~]|' + entitiesRegex ;
    128139
    129140                                entitiesRegex = new RegExp( entitiesRegex, 'g' );
    130141
     142                                function getEntity( character )
     143                                {
     144                                        return config.entities_processNumerical == 'force' || !entitiesTable[ character ] ?
     145                                                   '&#' + character.charCodeAt(0) + ';'
     146                                                        : entitiesTable[ character ];
     147                                }
     148
     149                                // Decode entities that the browsers has transformed
     150                                // at first place.
     151                                var baseEntitiesTable = buildTable( [ htmlbase, 'shy' ].join( ',' ) , true ),
     152                                        baseEntitiesRegex = new RegExp( baseEntitiesTable.regex, 'g' );
     153
    131154                                function getChar( character )
    132155                                {
    133                                         return entitiesTable[ character ] || ( '&#' + character.charCodeAt(0) + ';' );
     156                                        return baseEntitiesTable[ character ];
    134157                                }
    135158
    136159                                htmlFilter.addRules(
    137160                                        {
    138161                                                text : function( text )
    139162                                                {
    140                                                         return text.replace( entitiesRegex, getChar );
     163                                                        return text.replace( baseEntitiesRegex, getChar )
     164                                                                        .replace( entitiesRegex, getEntity );
    141165                                                }
    142166                                        });
    143167                        }
     
    179203
    180204/**
    181205 * Whether to convert all remaining characters, not comprised in the ASCII
    182  * character table, to their relative numeric representation of HTML entity.
     206 * character table, to their relative decimal numeric representation of HTML entity.
     207 * When specified as the value 'force', it will simply convert all entities into the above form.
    183208 * For example, the phrase "This is Chinese: &#27721;&#35821;." is outputted
    184209 * as "This is Chinese: &amp;#27721;&amp;#35821;."
    185210 * @type Boolean
     211 * @type Boolean|String
    186212 * @default false
    187213 * @example
    188214 * config.entities_processNumerical = true;
     215 * config.entities_processNumerical = 'force';          //Convert from "&nbsp;" into "&#160;";
    189216 */
    190217CKEDITOR.config.entities_processNumerical = false;
    191218
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy