How to prevent CKEditor replacing spaces with  ?

后端 未结 3 1297
醉梦人生
醉梦人生 2021-02-14 20:24

I\'m facing an issue with CKEditor 4, I need to have an output without any html entity so I added config.entities = false; in my config, but some  

3条回答
  •  自闭症患者
    2021-02-14 20:52

    Based on Reinmars accepted answer and the Entities plugin I created a small plugin with an HTML filter which removes redundant   entities. The regular expression could be improved to suit other situations, so please edit this answer.

    /*
     * Remove   entities which were inserted ie. when removing a space and
     * immediately inputting a space.
     *
     * NB: We could also set config.basicEntities to false, but this is stongly
     * adviced against since this also does not turn ie. < into <.
     * @link http://stackoverflow.com/a/16468264/328272
     *
     * Based on StackOverflow answer.
     * @link http://stackoverflow.com/a/14549010/328272
     */
    CKEDITOR.plugins.add('removeRedundantNBSP', {
      afterInit: function(editor) {
        var config = editor.config,
          dataProcessor = editor.dataProcessor,
          htmlFilter = dataProcessor && dataProcessor.htmlFilter;
    
        if (htmlFilter) {
          htmlFilter.addRules({
            text: function(text) {
              return text.replace(/(\w) /g, '$1 ');
            }
          }, {
            applyToAll: true,
            excludeNestedEditable: true
          });
        }
      }
    });
    

提交回复
热议问题