stackoverflow 上也有类似的问题,但这些问题的答案对我不起作用,所以请不要将其标记为重复。
在我的 cms 中,我希望人们能够添加 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);