我在How can I clean up Django bleach + CKeditor debris?发布了一条关于如何从Python / Django中删除侵犯空白段落的相关说明。
但是,我想更直接地询问CKeditor是否提供选择退出添加的空白段落的功能,或者在提交之前删除所有空白段落。
HTML源代码示例,其中一个以编程方式添加了空白段落(缩进行由一个制表符缩进,而不是按空格缩进):
<p>
This little bunny is <em>shy</em>, but also very affectionate.</p>
<p>
</p>
<p>
Would you like to meet her?</p>
每次保存我都会添加一个段落。
除了用户主动之外,如何防止,排除等添加空白段落?
谢谢,
答案 0 :(得分:2)
问题是你在存储到数据库之前删除了开始和最终结束标记,这会留下不匹配的html标记,因此无效的html(...每当你有多个p
块时)。所以,
<p>Something</p>
<p>Something else</p>
变为:
Something</p> // invalid html - closing tag with no corresponding opening tag
<p>Something else // invalid html - opening tag with no corresponding closing tag
CKEditor实际上是在重新加载回编辑器时尝试修复无效的html。
如果您不想要段落标记我建议使用配置选项config.autoParagraph = false;
和/或config.enterMode = CKEDITOR.ENTER_BR;
(CKEditor会在用户使用<br />
时使用根据您的要求键入新行。
另一种可能的解决方案是在保存之前更改您的功能,以便在'<p>'
和''
与'</p>'
之间查找和替换所有出现的'<br />'
最后删除任何尾随'<br />'
。
重复问题的步骤:
一个。您可以转到http://sdk.ckeditor.com/samples/accessibilitychecker.html并点击Source
按钮,然后粘贴以下内容:
<p>Something</p>
<p>Something else</p>
B中。点击Source
,它会正确显示此HTML。
℃。再次点击Source
返回源模式,只显示2个p
元素
d。删除开头段落和结束段落标记:
Something</p> // invalid html - closing tag with no corresponding opening tag
<p>Something else // invalid html - opening tag with no corresponding closing tag
E单击Source
两次以显示输出,然后查看调整后的来源:
<p>Something</p>
<p> </p>
<p>Something else</p>