如何使ckeditor停止删除空div [英] how to make ckeditor stop removing empty divs
问题描述
在stackoverflow上也有与此类似的问题,但是这些问题的答案对我不起作用,因此请不要将此标记为重复。
There are similar questions to this here on stackoverflow, but the answers in those questions do not work for me, so please don't flag this as a duplicate.
在我的cms中,我希望人们能够添加SPA(单页应用程序)的内容页面。这些类型的应用程序除了具有一些属性的div外什么都没有,是很常见的,并且使用javascript将应用程序加载到div中。因此,我希望用户能够创建仅包含具有以下属性的空div的内容:
In my cms, I want people to be able to add content pages that are SPA (Single Page Applications). It is very common for these kinds of apps to have nothing but a div with some attributes and javascript is used to load the app into the div. So I want users to be able to create content that consists only of an empty div with some attributes like this:
<div data-cscal-eventsource="default" data-cscal-height="550" id="eventCal" class="cs-eventcal"></div>
不过ckeditor(目前使用的是4.6.2版)始终会删除空div。通过谷歌搜索和发现类似的问题,我在配置中尝试了各种操作:
However ckeditor (I'm using version 4.6.2 at the moment) always removes the empty div. From googling and finding similar questions here I have tried various things in config:
allowedContent : true
应该关闭所有过滤功能,但仍会被删除。
is supposed to turn off all filtering but it still gets removed.
CKEDITOR.dtd.$removeEmpty['div'] = false;
,但对我不起作用。
在将allowedContent设置为true之前,我尝试了ExtraAllowedContent这样的各种操作:
before setting allowedContent to true I tried various things with extraAllowedContent like this:
extraAllowedContent: 'div(*){*}[*]; ol li span a(*){*}[*]'
如果div中有一些文字例如,
If the div has some text it works like this for example:
<div data-cscal-eventsource="default" data-cscal-height="550" id="eventCal" class="cs-eventcal">hello</div>
但我不想要该文本,使用nbsp也不起作用。
but I don't want that text and using a nbsp doesn't work either.
如何使CKEditor离开我的空div?我的div是编辑器中唯一的内容,我的SPA的脚本和CSS是从编辑器外部添加的。我只需要能够添加div而不将其删除。我正在尝试使用sourcedialog添加div。
How can I make CKEditor leave my empty div alone? My div is the only content in the editor, the script and css for my SPA is added from outside the editor. I just need to be able to add the div without it being removed. I am trying to add the div using the sourcedialog.
我不想修改ckeditor源代码来解决此问题。
I do not want to modify the ckeditor source code to solve this.
为清晰起见,我完整的代码是这样的,您可以在注释中看到我尝试过的一些内容:
My complete code for clarity is like this, you can see in comments some of the things I have tried:
(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);
推荐答案
非常棘手-就我而言,我需要设置
Pretty tricky - in my case I needed to set
config.ignoreEmptyParagraph = false;
最终显示DIV ...
to finally having the DIV shown ... beside
CKEDITOR.dtd.$removeEmpty.div = 0
并且完全允许使用DIVS ...
and having DIVS allowed at all ...
这篇关于如何使ckeditor停止删除空div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!