JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.4
[ckeditor.git] / _source / plugins / entities / plugin.js
index 6254dcd..f8329cd 100644 (file)
@@ -1,17 +1,16 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
 (function()\r
 {\r
-       var entities =\r
-\r
-               // Base HTML entities.\r
-               'nbsp,gt,lt,quot,' +\r
+       // Base HTML entities.\r
+       var htmlbase = 'nbsp,gt,lt';\r
 \r
+       var entities =\r
                // Latin-1 Entities\r
-               'iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,' +\r
+               'quot,iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,' +\r
                'not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,' +\r
                'cedil,sup1,ordm,raquo,frac14,frac12,frac34,iquest,times,divide,' +\r
 \r
@@ -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,' +\r
                'upsih,piv';\r
 \r
-       function buildTable( entities )\r
+       /**\r
+        * Create a mapping table between one character and it's entity form from a list of entity names.\r
+        * @param reverse {Boolean} Whether create a reverse map from the entity string form to actual character.\r
+        */\r
+       function buildTable( entities, reverse )\r
        {\r
                var table = {},\r
                        regex = [];\r
@@ -63,31 +66,37 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                entities = entities.replace( /\b(nbsp|shy|gt|lt|amp)(?:,|$)/g, function( match, entity )\r
                        {\r
-                               table[ specialTable[ entity ] ] = '&' + entity + ';';\r
-                               regex.push( specialTable[ entity ] );\r
+                               var org = reverse ? '&' + entity + ';' : specialTable[ entity ],\r
+                                       result = reverse ? specialTable[ entity ] : '&' + entity + ';';\r
+\r
+                               table[ org ] = result;\r
+                               regex.push( org );\r
                                return '';\r
                        });\r
 \r
-               // Transforms the entities string into an array.\r
-               entities = entities.split( ',' );\r
-\r
-               // Put all entities inside a DOM element, transforming them to their\r
-               // final chars.\r
-               var div = document.createElement( 'div' ),\r
-                       chars;\r
-               div.innerHTML = '&' + entities.join( ';&' ) + ';';\r
-               chars = div.innerHTML;\r
-               div = null;\r
-\r
-               // Add all chars to the table.\r
-               for ( var i = 0 ; i < chars.length ; i++ )\r
+               if ( !reverse && entities )\r
                {\r
-                       var charAt = chars.charAt( i );\r
-                       table[ charAt ] = '&' + entities[ i ] + ';';\r
-                       regex.push( charAt );\r
+                       // Transforms the entities string into an array.\r
+                       entities = entities.split( ',' );\r
+\r
+                       // Put all entities inside a DOM element, transforming them to their\r
+                       // final chars.\r
+                       var div = document.createElement( 'div' ),\r
+                               chars;\r
+                       div.innerHTML = '&' + entities.join( ';&' ) + ';';\r
+                       chars = div.innerHTML;\r
+                       div = null;\r
+\r
+                       // Add all chars to the table.\r
+                       for ( var i = 0 ; i < chars.length ; i++ )\r
+                       {\r
+                               var charAt = chars.charAt( i );\r
+                               table[ charAt ] = '&' + entities[ i ] + ';';\r
+                               regex.push( charAt );\r
+                       }\r
                }\r
 \r
-               table.regex = regex.join( '' );\r
+               table.regex = regex.join( reverse ? '|' : '' );\r
 \r
                return table;\r
        }\r
@@ -98,24 +107,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                {\r
                        var config = editor.config;\r
 \r
-                       if ( !config.entities )\r
-                               return;\r
-\r
                        var dataProcessor = editor.dataProcessor,\r
                                htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
 \r
                        if ( htmlFilter )\r
                        {\r
-                               var selectedEntities = entities;\r
+                               // Mandatory HTML base entities.\r
+                               var selectedEntities = htmlbase;\r
 \r
-                               if ( config.entities_latin )\r
-                                       selectedEntities += ',' + latin;\r
+                               if ( config.entities )\r
+                               {\r
+                                       selectedEntities += ',' + entities;\r
+                                       if ( config.entities_latin )\r
+                                               selectedEntities += ',' + latin;\r
 \r
-                               if ( config.entities_greek )\r
-                                       selectedEntities += ',' + greek;\r
+                                       if ( config.entities_greek )\r
+                                               selectedEntities += ',' + greek;\r
 \r
-                               if ( config.entities_additional )\r
-                                       selectedEntities += ',' + config.entities_additional;\r
+                                       if ( config.entities_additional )\r
+                                               selectedEntities += ',' + config.entities_additional;\r
+                               }\r
 \r
                                var entitiesTable = buildTable( selectedEntities );\r
 \r
@@ -123,21 +134,34 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                var entitiesRegex = '[' + entitiesTable.regex + ']';\r
                                delete entitiesTable.regex;\r
 \r
-                               if ( config.entities_processNumerical )\r
+                               if ( config.entities && config.entities_processNumerical )\r
                                        entitiesRegex = '[^ -~]|' + entitiesRegex ;\r
 \r
                                entitiesRegex = new RegExp( entitiesRegex, 'g' );\r
 \r
+                               function getEntity( character )\r
+                               {\r
+                                       return config.entities_processNumerical == 'force' || !entitiesTable[ character ] ?\r
+                                                  '&#' + character.charCodeAt(0) + ';'\r
+                                                       : entitiesTable[ character ];\r
+                               }\r
+\r
+                               // Decode entities that the browsers has transformed\r
+                               // at first place.\r
+                               var baseEntitiesTable = buildTable( [ htmlbase, 'shy' ].join( ',' ) , true ),\r
+                                       baseEntitiesRegex = new RegExp( baseEntitiesTable.regex, 'g' );\r
+\r
                                function getChar( character )\r
                                {\r
-                                       return entitiesTable[ character ] || ( '&#' + character.charCodeAt(0) + ';' );\r
+                                       return baseEntitiesTable[ character ];\r
                                }\r
 \r
                                htmlFilter.addRules(\r
                                        {\r
                                                text : function( text )\r
                                                {\r
-                                                       return text.replace( entitiesRegex, getChar );\r
+                                                       return text.replace( baseEntitiesRegex, getChar )\r
+                                                                       .replace( entitiesRegex, getEntity );\r
                                                }\r
                                        });\r
                        }\r
@@ -147,6 +171,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
 /**\r
  * Whether to use HTML entities in the output.\r
+ * @name CKEDITOR.config.entities\r
  * @type Boolean\r
  * @default true\r
  * @example\r
@@ -158,6 +183,7 @@ CKEDITOR.config.entities = true;
  * Whether to convert some Latin characters (Latin alphabet No&#46; 1, ISO 8859-1)\r
  * to HTML entities. The list of entities can be found at the\r
  * <a href="http://www.w3.org/TR/html4/sgml/entities.html#h-24.2.1">W3C HTML 4.01 Specification, section 24.2.1</a>.\r
+ * @name CKEDITOR.config.entities_latin\r
  * @type Boolean\r
  * @default true\r
  * @example\r
@@ -170,6 +196,7 @@ CKEDITOR.config.entities_latin = true;
  * HTML entities. This may be more relevant for users typing text written in Greek.\r
  * The list of entities can be found at the\r
  * <a href="http://www.w3.org/TR/html4/sgml/entities.html#h-24.3.1">W3C HTML 4.01 Specification, section 24.3.1</a>.\r
+ * @name CKEDITOR.config.entities_greek\r
  * @type Boolean\r
  * @default true\r
  * @example\r
@@ -179,20 +206,23 @@ CKEDITOR.config.entities_greek = true;
 \r
 /**\r
  * Whether to convert all remaining characters, not comprised in the ASCII\r
- * character table, to their relative numeric representation of HTML entity.\r
+ * character table, to their relative decimal numeric representation of HTML entity.\r
+ * When specified as the value 'force', it will simply convert all entities into the above form.\r
  * For example, the phrase "This is Chinese: &#27721;&#35821;." is outputted\r
  * as "This is Chinese: &amp;#27721;&amp;#35821;."\r
- * @type Boolean\r
+ * @name CKEDITOR.config.entities_processNumerical\r
+ * @type Boolean|String\r
  * @default false\r
  * @example\r
  * config.entities_processNumerical = true;\r
+ * config.entities_processNumerical = 'force';         //Convert from "&nbsp;" into "&#160;";\r
  */\r
-CKEDITOR.config.entities_processNumerical = false;\r
 \r
 /**\r
  * An additional list of entities to be used. It's a string containing each\r
  * entry separated by a comma. Entities names or number must be used, exclusing\r
  * the "&amp;" preffix and the ";" termination.\r
+ * @name CKEDITOR.config.entities_additional\r
  * @default '#39'  // The single quote (') character.\r
  * @type String\r
  * @example\r