如何防止CKEditor替换空格 ?

Mat*_*Cat 9 ckeditor

我正面临着CKEditor 4的问题,我需要一个没有任何html实体的输出,所以我config.entities = false;在我的配置中添加了,但有些 出现在

  • 插入内联标记:替换为之前的空格  
  • 粘贴文本:每个空格都被替换为 偶数config.forcePasteAsPlainText = true;

您可以通过键入来检查任何演示

试验测试

例如.

你知道我怎么能防止这种行为吗?

谢谢!

Rei*_*mar 10

这些实体:

// Base HTML entities.
var htmlbase = 'nbsp,gt,lt,amp';
Run Code Online (Sandbox Code Playgroud)

是一个例外.要摆脱它们你可以设置basicEntities: false.但正如文档所说,这是一种不安全的设置.所以,如果你只是想去掉 的话,我应该只使用输出数据的正则表达式(例如,通过添加监听#getData),或者,如果你想更精确,添加自己的规则,htmlFilter就像entities插件在这里所做的.


lme*_*urs 10

基于Reinmars接受的答案和实体插件我创建了一个小插件与HTML过滤器去除多余的 实体.正则表达式可以改进以适应其他情况,所以请编辑这个答案.

/*
 * 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
      });
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 迄今为止最好的答案!但要小心,您缺少正则表达式上的全局修饰符,这意味着只有第一个   将被替换。请将正则表达式更改为:`/(\w) /g` (3认同)