我注意到使用CkEditor的默认表格插件(参见this和this),您可以将表格添加到编辑器中。
我的问题是,现在我需要强制编辑器内容(通过
实际上,当我添加一个表格时,结果如下:
我的问题是,现在我需要强制编辑器内容(通过
editor.getData()
获取)成为HTML5有效内容。实际上,当我添加一个表格时,结果如下:
<table cellspacing="1" cellpadding="1" border="1" style="width: 500px;">
<tr>
<td>
[...]
</td>
[...]
</tr>
[...]
</table>
正如您所看到的,table
标签有一些在HTML5中被弃用的属性。在上面的例子中,您可以看到cellspacing
,border
和cellpadding
。
有人在dev.ckeditor.com上报告了这个问题(请参见this和this other),但没有解释如何解决这个问题。
您知道解决这个问题的一些技巧吗?
我的解决方案
我想分享我的解决方案,基于Reinmar的答案。
我在config.js
文件中添加了以下行:
config.disallowedContent = "table[cellspacing,cellpadding,border,align,summary,bgcolor,frame,rules,width]; td[axis,abbr,scope,align,bgcolor,char,charoff,height,nowrap,valign,width]; th[axis,abbr,align,bgcolor,char,charoff,height,nowrap,valign,width]; tbody[align,char,charoff,valign]; tfoot[align,char,charoff,valign]; thead[align,char,charoff,valign]; tr[align,bgcolor,char,charoff,valign]; col[align,char,charoff,valign,width]; colgroup[align,char,charoff,valign,width]";
因此,我禁用了与表格相关的所有非HTML5属性。