这发生在几乎所有的 js 所见即所得。更改“从单词粘贴”行为将涉及修补或扩展 timymce/wysiwyg javascript,但如果您不关心空的 <p> 标记来自哪里并且想要摆脱每个提交的标记,那么您有一些选项。
[在我最近自己不得不再次这样做之后编辑了我的答案]
如果您需要大量的 html 更正/重写,您可能应该查看 HTML Purifier 模块。它有一个高级选项来删除空的和 nbsp 填充的 HTML 元素,以及更正和 xss 过滤。但是速度很慢。
有 Empty Paragraph Killer 模块,但它现在不适用于 D7,所以你可以跳过它。
最直接的解决方案是在自定义模块中编写自己的输入过滤器。这里有一个例子filter module,你可以复制学习。或者你可以复制我的:
/* Implements hook_filter_info(). */
function YOURMODULE_filter_info() {
$filters['kill_empty'] = array(
'title' => t('Kill Empty Paragraphs'),
'description' => t('Remove paragraphs that contain only whitespace (including line breaks and &nbsp;\'s) that are often inserted by editors using WYSIWYGs.'),
'process callback' => '_YOURMODULE_kill_empty',
'tips callback' => '_YOURMODULE_kill_empty_tips',
);
return $filters;
}
/* Process callbacks, where the work is done. */
function _YOURMODULE_kill_empty($text, $filter) {
// Remove all <p> tags containing only nbsp's, white space, or nothing.
return preg_replace('/<p[^>]*>( |\s)*<\/p>/', '', $text);
}
/* Tips for the content editor, if you want them. I usually take these out. */
function _YOURMODULE_kill_empty_tips($filter, $format, $long = FALSE) {
if (!$long) {
// This string will be shown in the content add/edit form.
return t('Use one [enter] to create a new paragraph. More than one will be ignored.');
}
else {
// And this one on the "Filter Tips" page.
return t('To maintain consistancy in content display, empty paragraphs inserted by WYSIWYG editors will be removed.');
}
}
将所有这些放在一个自定义模块中,转到配置并编辑您的文本格式,然后激活“删除空段落”过滤器。应该是这样的。
最后一点是,如果您真的希望它们消失,例如不在数据库中,将永远不会回来,您应该能够使用此过滤器和 Sanitizable module 来删除所有提交时为空<p>s。关于在内容进入数据库之前弄乱内容的常见警告适用。