How to prevent CKEditor replacing spaces with  ?

和自甴很熟 提交于 2019-12-04 19:07:28

问题


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   appear when

  • an inline tag is inserted: the space before is replaced with  
  • text is pasted: every space is replaced with   even with config.forcePasteAsPlainText = true;

You can check that on any demo by typing

test test

eg.

Do you know how I can prevent this behaviour?

Thanks!


回答1:


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  , 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.




回答2:


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
      });
    }
  }
});



回答3:


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, ' ');


来源:https://stackoverflow.com/questions/14523293/how-to-prevent-ckeditor-replacing-spaces-with-nbsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!