No formatting inside html tag

我目前用的 WordPress 版本为 2.8.4。发现一个 bug,就是在 post html 源码编辑状态(非 Visual 编辑),输入时为

<div>
我希望 WordPress 自动格式化post时不要自说自话把html code加进html tag里。

不知 WordPress 能听明白吗?
</div>

保存后查看一下数据库,post_content 忠于原文,前台输出时却经过 WordPress 的格式转换,变成了


<div>
我希望 WordPress 自动格式化post时不要自说自话把html code加进html tag里。</p>
<p>不知 WordPress 能听明白吗?
</p></div>

html p tag 都不配对了。

这个 bug 不严重,因为 WordPress 自带的 TinyMCE editor 不会插入 div tag。TinyMCE 倒可以插入 b-quote,输入时

<blockquote>
我希望 WordPress 自动格式化post时不要自说自话把html code加进html tag里。

不知 WordPress 能听明白吗?
</blockquote>

前台输出

<blockquote><p>
我希望 WordPress 自动格式化post时不要自说自话把html code加进html tag里。</p>
<p>不知 WordPress 能听明白吗?
</p></blockquote>

完全正确,没有触发 bug。这个小 bug 让我联想到很多 wysiwyg editor 和 output formatting 时不尽如人意的地方。

我在 magento 和 phplist 里都是禁用 wysiwyg editor 的,在 WordPress 里虽没有禁用它,但我有意识地仅仅使用它的 htmlentity。象我习惯于手写 html 代码以求更多输出控制的人,觉得 WordPress output formatting logic 也不好,更好的逻辑是:

  • 如果post_content是有效的DOM,仅对最外层的 text nodes 作 html p wrapping,对任何 html tag 内部的不做任何处理。其他诸如 htmlentity filter 都不需要,因为 wysiwyg editor 已经作了这项工作。
  • 如果post_content是无效的DOM,WordPress 默认的 formatting 会作一些修复。我没考察过修复的智能程度,我的要求是一开始就不要写出无效的 DOM,所以不想多讨论这种情况下的处理。

举个例子说,我的 formatting logic 就是把

anything outside html tag is wrapped by p tag.

do not touch anything inside html. user should be responsible for it.
<div>
我希望 WordPress 自动格式化post时不要自说自话把html code加进html tag里。

不知 WordPress 能听明白吗?
</div>

输出为

<p>anything outside html tag is wrapped by p tag
</p><p>
do not touch anything inside html. user should be responsible for it.</p>
<div>
我希望 WordPress 自动格式化post时不要自说自话把html code加进html tag里。

不知 WordPress 能听明白吗?
</div>

Leave a comment

Your email address will not be published. Required fields are marked *