Jsoup 在错误的位置关闭了 <p> 标签

Jsoup closes <p> tag at the wrong point

出于某种原因,Jsoup 错误地解析了 html 文档,特别是包含 table.

中的一组段落的段落

输入:

<table>
     <tr>
         <td>
             <p>
                <p> Title <br/> New</p>
                <p> Content new </p>
             </p>
        </td>
     </tr>
</table>

解析后:

 <table>
     <tr>
         <td>
            <p> </p>
            <p> Title <br> New</p>
            <p> Content new </p>
            <p></p>
        </td>
     </tr>
</table>

您知道是什么原因造成的吗?我该如何解决?

谢谢。

这是正确的解析行为。因为 <p> 元素是块级元素,所以它们不能嵌套在其他 <p> 元素中。

According to the MDN documentation:

Paragraphs are block-level elements, and notably will automatically close if another block-level element is parsed before the closing </p> tag.

一种可能的解决方案是使用另一个元素,例如 <section><div>。例如:

<table>
     <tr>
         <td>
             <div>
                <p> Title <br/> New</p>
                <p> Content new </p>
             </div>
        </td>
     </tr>
</table>