如何让ckeditor停止删除空div

5

这里在stackoverflow上有与此类似的问题,但是那些问题的答案对我无效,请不要将其标记为重复。

在我的内容管理系统中,我希望用户能够添加单页应用程序(SPA)的内容页面。这种应用程序通常只有一个带有一些属性的div,并使用javascript将应用程序加载到该div中。因此,我希望用户能够创建仅由空div及其某些属性组成的内容,如下所示:

<div data-cscal-eventsource="default" data-cscal-height="550" id="eventCal" class="cs-eventcal"></div>

然而,无论是哪个版本的ckeditor(我目前使用的是4.6.2),它总是会删除空的div。通过谷歌搜索和在这里找到类似的问题,我尝试了各种不同的配置:

allowedContent : true

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

CKEDITOR.dtd.$removeEmpty['div'] = false;

我也发现有些答案中提到了这一点,但却没有对我起作用。

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

extraAllowedContent: 'div(*){*}[*]; ol li span a(*){*}[*]'

如果 div 元素包含一些文本,它会像这样工作,例如:
<div data-cscal-eventsource="default" data-cscal-height="550" id="eventCal" class="cs-eventcal">hello</div>

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

如何让 CKEditor 不操作我的空 div?我的 div 是编辑器中唯一的内容,我的 SPA 的脚本和 CSS 是从编辑器外添加的。我只需要能够添加 div 而不被删除。我正在尝试使用 sourcedialog 添加 div。

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

为了清晰起见,我的完整代码如下,您可以在注释中看到我尝试过的一些东西:

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

1
你解决过这个问题吗?我也遇到了同样的问题,但是我找到的所有答案都没有解决它。 - Stefan
1
不是的,我在我的CMS中加入了一个选项来禁用编辑器,这样用户就可以使用普通的文本区域在空div中放置SPA。 - Joe Audette
4个回答

2
相当棘手 - 在我的情况下,我需要设置
config.ignoreEmptyParagraph = false;

最终显示 DIV ... 旁边
CKEDITOR.dtd.$removeEmpty.div = 0

并且允许使用DIVS...


config.ignoreEmptyParagraph = false; 只有这个对我有效。 - gellezzz

2

config.basicEntities = false; //在您的 cke.editor 配置 JS 文件中

对我而言有效。

更新

尝试这个并查看是否有效,但是我可以毫无问题地使用空 divs。

for (var tag in CKEDITOR.dtd.$removeEmpty) {
    CKEDITOR.dtd.$removeEmpty[tag] = false;
}

更新2

还可以尝试这个方法,在我的配置文件中使用它可以保留空的div标签

config.htmlEncodeOutput = false;


还有其他东西可以尝试。 - jdmdevdotnet
谢谢你的回复,但是我尝试设置htmlEncodeOutput为false后还是不起作用。我也尝试将删除空标签设置为false,无论对于div还是其他所有标签都试过了,但这并没有解决我的问题。 - Joe Audette
似乎有些不对劲,这是我的配置,我刚刚测试过了,它应该可以工作的 ;) - jdmdevdotnet
修订版号 #7474 - jdmdevdotnet
你能验证其他配置更改是否有效吗?也许是你的配置有问题导致它无法工作? - jdmdevdotnet
显示剩余3条评论

1
我认为已经有些晚了,但为了参考,我在这里回复。 我们可以在CKEditor的config.js中设置: config.extraAllowedContent = 'div style;*id'; 注意:我正在使用CKEditor 4。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接