我遇到了CKEditor 4的问题,我需要有一个没有任何 html 实体的输出,所以我config.entities = false;
在我的配置中添加了,但有些
出现在
- 插入内联标记:之前的空格被替换为
- 文本被粘贴:每个空格都替换为
偶数config.forcePasteAsPlainText = true;
您可以通过键入在任何演示中进行检查
测试测试
例如。
你知道我怎样才能防止这种行为吗?
谢谢!
我遇到了CKEditor 4的问题,我需要有一个没有任何 html 实体的输出,所以我config.entities = false;
在我的配置中添加了,但有些
出现在
偶数config.forcePasteAsPlainText = true;
您可以通过键入在任何演示中进行检查
测试测试
例如。
你知道我怎样才能防止这种行为吗?
谢谢!
基于 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 <.
* @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
});
}
}
});
我需要更改 Imeus 发送的正则表达式,在我的情况下,我使用 TYPO3 并且需要编辑后端编辑器。这个没用。也许它可以帮助另一个有同样问题的人:)
return text.replace(/ /g, ' ');
但不<tag> </tag>
使用 Javascript Regexp这对 CKEditor 尤其有用,因为它会创建类似 的行<p> </p>
,您可能希望保留这些行。
背景:我首先尝试使用环视断言制作单行 Javascript。看来你不能把它们锁起来,至少现在还不能。我的第一种方法不成功:
return text.replace(/(?<!\>) (?!<\/)/gi, " ")
// Removes but not <p> </p>
// It works, but does not remove `<p> blah </p>`.
这是我更新的工作单行代码:
return text.replace(/(?<!\>\s.)( (?!<\/)|(?<!\>) <\/p>)/gi, " ")
这按预期工作。你可以在这里测试它。
但是,这是一种阴暗的做法,因为某些浏览器不完全支持环视。
阅读更多关于断言的信息。
我最终在我的生产代码中使用了什么: 我最终用多个 replace() 做了一些 hacky 方法。这应该适用于所有浏览器。
.trim() // Remove whitespaces
.replace(/\u00a0/g, " ") // Remove unicode non-breaking space
.replace(/((<\w+>)\s*( )\s*(<\/\w+>))/gi, "$2<!--BOOM-->$4") // Replace empty nbsp tags with BOOM
.replace(/ /gi, " ") // remove all
.replace(/((<\w+>)\s*(<!--BOOM-->)\s*(<\/\w+>))/gi, "$2 $4") // Replace BOOM back to empty tags
如果你有更好的建议,我很乐意听到。