X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fplugins%2Fentities%2Fplugin.js;h=ce0f34e421afe350d9ffaa49a428e9576479d7e3;hp=6254dcd307ca30a59da89c04d36181c716a7522c;hb=c9fdde67e6384bd5a66adc2b3bba5c4ce9db56c7;hpb=9873d66421922c7aef8be0f5d2ab51e547b19e66 diff --git a/_source/plugins/entities/plugin.js b/_source/plugins/entities/plugin.js index 6254dcd..ce0f34e 100644 --- a/_source/plugins/entities/plugin.js +++ b/_source/plugins/entities/plugin.js @@ -5,11 +5,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license (function() { - var entities = - - // Base HTML entities. - 'nbsp,gt,lt,quot,' + + // Base HTML entities. + var htmlbase = 'nbsp,gt,lt,quot'; + var entities = // Latin-1 Entities 'iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,' + 'not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,' + @@ -46,7 +45,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 'omicron,pi,rho,sigmaf,sigma,tau,upsilon,phi,chi,psi,omega,thetasym,' + 'upsih,piv'; - function buildTable( entities ) + /** + * Create a mapping table between one character and it's entity form from a list of entity names. + * @param reverse {Boolean} Whether create a reverse map from the entity string form to actual character. + */ + function buildTable( entities, reverse ) { var table = {}, regex = []; @@ -63,31 +66,37 @@ For licensing, see LICENSE.html or http://ckeditor.com/license entities = entities.replace( /\b(nbsp|shy|gt|lt|amp)(?:,|$)/g, function( match, entity ) { - table[ specialTable[ entity ] ] = '&' + entity + ';'; - regex.push( specialTable[ entity ] ); + var org = reverse ? '&' + entity + ';' : specialTable[ entity ], + result = reverse ? specialTable[ entity ] : '&' + entity + ';'; + + table[ org ] = result; + regex.push( org ); return ''; }); - // Transforms the entities string into an array. - entities = entities.split( ',' ); - - // Put all entities inside a DOM element, transforming them to their - // final chars. - var div = document.createElement( 'div' ), - chars; - div.innerHTML = '&' + entities.join( ';&' ) + ';'; - chars = div.innerHTML; - div = null; - - // Add all chars to the table. - for ( var i = 0 ; i < chars.length ; i++ ) + if ( !reverse ) { - var charAt = chars.charAt( i ); - table[ charAt ] = '&' + entities[ i ] + ';'; - regex.push( charAt ); + // Transforms the entities string into an array. + entities = entities.split( ',' ); + + // Put all entities inside a DOM element, transforming them to their + // final chars. + var div = document.createElement( 'div' ), + chars; + div.innerHTML = '&' + entities.join( ';&' ) + ';'; + chars = div.innerHTML; + div = null; + + // Add all chars to the table. + for ( var i = 0 ; i < chars.length ; i++ ) + { + var charAt = chars.charAt( i ); + table[ charAt ] = '&' + entities[ i ] + ';'; + regex.push( charAt ); + } } - table.regex = regex.join( '' ); + table.regex = regex.join( reverse ? '|' : '' ); return table; } @@ -98,24 +107,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { var config = editor.config; - if ( !config.entities ) - return; - var dataProcessor = editor.dataProcessor, htmlFilter = dataProcessor && dataProcessor.htmlFilter; if ( htmlFilter ) { - var selectedEntities = entities; + // Mandatory HTML base entities. + var selectedEntities = htmlbase; - if ( config.entities_latin ) - selectedEntities += ',' + latin; + if ( config.entities ) + { + selectedEntities += ',' + entities; + if ( config.entities_latin ) + selectedEntities += ',' + latin; - if ( config.entities_greek ) - selectedEntities += ',' + greek; + if ( config.entities_greek ) + selectedEntities += ',' + greek; - if ( config.entities_additional ) - selectedEntities += ',' + config.entities_additional; + if ( config.entities_additional ) + selectedEntities += ',' + config.entities_additional; + } var entitiesTable = buildTable( selectedEntities ); @@ -123,21 +134,34 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var entitiesRegex = '[' + entitiesTable.regex + ']'; delete entitiesTable.regex; - if ( config.entities_processNumerical ) + if ( config.entities && config.entities_processNumerical ) entitiesRegex = '[^ -~]|' + entitiesRegex ; entitiesRegex = new RegExp( entitiesRegex, 'g' ); + function getEntity( character ) + { + return config.entities_processNumerical == 'force' || !entitiesTable[ character ] ? + '&#' + character.charCodeAt(0) + ';' + : entitiesTable[ character ]; + } + + // Decode entities that the browsers has transformed + // at first place. + var baseEntitiesTable = buildTable( [ htmlbase, 'shy' ].join( ',' ) , true ), + baseEntitiesRegex = new RegExp( baseEntitiesTable.regex, 'g' ); + function getChar( character ) { - return entitiesTable[ character ] || ( '&#' + character.charCodeAt(0) + ';' ); + return baseEntitiesTable[ character ]; } htmlFilter.addRules( { text : function( text ) { - return text.replace( entitiesRegex, getChar ); + return text.replace( baseEntitiesRegex, getChar ) + .replace( entitiesRegex, getEntity ); } }); } @@ -179,13 +203,16 @@ CKEDITOR.config.entities_greek = true; /** * Whether to convert all remaining characters, not comprised in the ASCII - * character table, to their relative numeric representation of HTML entity. + * character table, to their relative decimal numeric representation of HTML entity. + * When specified as the value 'force', it will simply convert all entities into the above form. * For example, the phrase "This is Chinese: 汉语." is outputted * as "This is Chinese: &#27721;&#35821;." * @type Boolean + * @type Boolean|String * @default false * @example * config.entities_processNumerical = true; + * config.entities_processNumerical = 'force'; //Convert from " " into " "; */ CKEDITOR.config.entities_processNumerical = false;