首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让ckeditor停止删除空div

如何让ckeditor停止删除空div
EN

Stack Overflow用户
提问于 2017-06-08 00:13:04
回答 4查看 2.5K关注 0票数 3

在stackoverflow上也有类似的问题,但这些问题中的答案对我不起作用,所以请不要将其标记为重复。

在我的内容管理系统中,我希望人们能够添加SPA (单页应用程序)的内容页面。对于这些类型的应用程序来说,除了带有一些属性的div之外什么都没有是很常见的,javascript用于将应用程序加载到div中。因此,我希望用户能够创建只包含空div的内容,这些div具有如下属性:

代码语言:javascript
复制
<div data-cscal-eventsource="default" data-cscal-height="550" id="eventCal" class="cs-eventcal"></div>

然而,ckeditor (我目前使用的是4.6.2版本)总是删除空的div。通过谷歌搜索和在这里找到类似的问题,我在config中尝试了各种方法:

代码语言:javascript
复制
allowedContent : true

应该关闭所有过滤,但它仍然被删除。

代码语言:javascript
复制
CKEDITOR.dtd.$removeEmpty['div'] = false;

在一些答案中也可以找到,但对我不起作用。

在将allowedContent设置为true之前,我尝试了extraAllowedContent的各种方法,如下所示:

代码语言:javascript
复制
extraAllowedContent: 'div(*){*}[*]; ol li span a(*){*}[*]'

如果div包含一些文本,它的工作方式如下所示:

代码语言:javascript
复制
<div data-cscal-eventsource="default" data-cscal-height="550" id="eventCal" class="cs-eventcal">hello</div>

但是我不想要那个文本,使用nbsp也不起作用。

怎样才能让CKEditor离开我的空div呢?我的div是编辑器中唯一的内容,我的SPA的脚本和css是从编辑器外部添加的。我只需要能够添加div而不删除它。我正在尝试使用源对话框添加div。

我不想修改ckeditor源代码来解决这个问题。

为了清晰起见,我的完整代码是这样的,你可以在评论中看到我尝试过的一些东西:

代码语言:javascript
复制
(function ($) {

var xsrfToken = $('[name="__RequestVerificationToken"]:first').val();
var dfUrl = $("#editorconfig").data("dropfileuploadurl") || '/filemanager/upload';
var fbUrl = $("#editorconfig").data("filebrowserurl") || '/filemanager/filedialog?type=file';
var ibUrl = $("#editorconfig").data("imagebrowseurl") || '/filemanager/filedialog?type=image';
var editorId = $("#editorconfig").data("editorid") || 'foo';
var datepickerid = $("#editorconfig").data("datepickerid") || 'foo';
var usingCdn = $("#editorconfig").data("usingcdn");

var editorConfig = {
    toolbar: [['Sourcedialog', 'Maximize'],
['SelectAll', 'RemoveFormat', 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print'],
['Undo', 'Redo', '-', 'Find', 'Replace', 'Bold', 'Italic', 'Underline', '-', 'Strike', 'Superscript'],
    '/',
    ['Blockquote', 'Format'], ['NumberedList', 'BulletedList'],
['Link', 'Unlink', 'Anchor'],
['Image', 'oembed', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar','CodeSnippet']],
    extraPlugins:'oembed,cloudscribe-filedrop,sourcedialog,codesnippet',
    removePlugins: 'scayt,wsc',
    format_tags: 'p;h1;h2;h3;h4;pre;address;div',
    dropFileUploadUrl: dfUrl,
    dropFileXsrfToken:xsrfToken,
    linkWebSizeToOriginal:true,
    forcePasteAsPlainText:true,
    filebrowserWindowHeight:'70%',
    filebrowserWindowWidth:'80%',
    filebrowserBrowseUrl:fbUrl,
    filebrowserImageBrowseUrl: ibUrl,
    allowedContent : true, //temporary trying to disable filtering
    extraAllowedContent: 'div(*){*}[*]; ol li span a(*){*}[*]', // allow all classes and attributes for these tags
    fillEmptyBlocks: false
};

if (usingCdn === true) {
    //alert('using cdn');
    CKEDITOR.plugins.addExternal('widget', '/ckjs/plugins/widget/', 'plugin.js');
    CKEDITOR.plugins.addExternal('widgetselection', '/ckjs/plugins/widgetselection/', 'plugin.js');
    CKEDITOR.plugins.addExternal('lineutils', '/ckjs/plugins/lineutils/', 'plugin.js');
    CKEDITOR.plugins.addExternal('oembed', '/ckjs/plugins/oembed/', 'plugin.js');
    CKEDITOR.plugins.addExternal('cloudscribe-filedrop', '/ckjs/plugins/cloudscribe-filedrop/', 'plugin.js');

}

//editorConfig.protectedSource.push(/<div[^>]*><\/div>/g);
//CKEDITOR.dtd.$removeEmpty['div'] = false;

//$.each(CKEDITOR.dtd.$removeEmpty, function (i, value) {
//    CKEDITOR.dtd.$removeEmpty[i] = 0;
//});

var ck = CKEDITOR.replace(editorId, editorConfig);

ck.on('instanceCreated', function (ev) {
    CKEDITOR.dtd.$removeEmpty['div'] = false;
});

var userLocale = $('#' + datepickerid).data("locale");
$('#' + datepickerid).datetimepicker({
    debug: false,
    widgetPositioning: { horizontal: 'left', vertical: 'bottom' },
    keepOpen: true,
    allowInputToggle: true,
    locale: userLocale
});

})(jQuery);
EN

回答 4

Stack Overflow用户

发布于 2017-06-08 00:16:50

config.basicEntities = false; //in your cke.editor config js file

这对我很有效。

更新

试一试,看看它是否工作,但我可以有空的div没有问题。

代码语言:javascript
复制
for (var tag in CKEDITOR.dtd.$removeEmpty) {
    CKEDITOR.dtd.$removeEmpty[tag] = false;
}

更新2

也试试这个,我的配置中有这个,它不会删除空的div

config.htmlEncodeOutput = false;

票数 2
EN

Stack Overflow用户

发布于 2018-10-24 21:14:42

相当棘手-在我的例子中,我需要设置

代码语言:javascript
复制
config.ignoreEmptyParagraph = false;

最终把DIV展示出来。旁边

代码语言:javascript
复制
CKEDITOR.dtd.$removeEmpty.div = 0

并且允许使用DIVS ...

票数 1
EN

Stack Overflow用户

发布于 2020-09-21 23:23:19

我认为已经很晚了,但我在这里回复以供参考。我们可以在CKEditor样式中设置config.extraAllowedContent = 'div config.js;*id‘;

注意:我使用的是CKEditor 4

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44417887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档