How to prevent CKEditor replacing spaces with  ?

后端 未结 3 1293
醉梦人生
醉梦人生 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 &lt;.
     * @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)&nbsp;/g, '$1 ');
            }
          }, {
            applyToAll: true,
            excludeNestedEditable: true
          });
        }
      }
    });
    
    0 讨论(0)
  • 2021-02-14 21:07

    These entities:

    // Base HTML entities.
    var htmlbase = 'nbsp,gt,lt,amp';
    

    Are an exception. To get rid of them you can set basicEntities: false. But as docs mention this is an insecure setting. So if you only want to remove &nbsp;, then I should just use regexp on output data (e.g. by adding listener for #getData) or, if you want to be more precise, add your own rule to htmlFilter just like entities plugin does here.

    0 讨论(0)
  • 2021-02-14 21:19

    I needed to change the regular expression Imeus sent, in my case, I use TYPO3 and needed to edit the backend editor. This one didn't work. Maybe it can help another one that has the same problem :)

    return text.replace(/&nbsp;/g, ' ');
    
    0 讨论(0)
提交回复
热议问题