0

I have this HTML text:

<p>
  <span>
    <table>
      <tbody>
        <tr>
          <td>
            <figure></figure>
          </td>
        </tr>
      </tbody>
    </table>
  </span>
  <br>
</p>

Then I execute the following code:

let string = "<p><span><table><tbody><tr><td><figure></figure></td></tr></tbody></table></span><br></p>";
let html = $.parseHTML("<root>" + string + "</root>");
let strHtml = $(html).html();
console.log(strHtml);

The console output is as follows:

<p>
  <span></span>
</p>
<table>
  <tbody>
    <tr>
      <td>
        <figure></figure>
      </td>
    </tr>
  </tbody>
</table>
<br>
<p></p>

The table tag is placed out of the p tag and a p tag is set in front and at the end of the tag table

Is this correct? Any workarounds?

5
  • 6
    "Is this correct?" - yes, this is "correct" in that this is how a DOM parser is supposed to fix this faulty HTML. A table can not be nested into a paragraph in HTML.
    – CBroe
    Commented Jun 12 at 9:28
  • 2
    See <span> technical summary documentation for more info about allowed content. The same for <p>
    – phuzi
    Commented Jun 12 at 9:35
  • 1
    There is no good reason to put anything but text and text elements in a p or span tag. Also have a look at developer.mozilla.org/en-US/docs/Glossary/Semantics
    – mplungjan
    Commented Jun 12 at 9:40
  • 1
    following the phuzi comment, in particular phrasing content is what's expected the span element to contain (as an inline element)
    – Diego D
    Commented Jun 12 at 9:40
  • 1
    Copy-paste HTML into jsfiddle.net (or any other HTML rendering mechanism such as saving as an .html file and loading into a browser), right-click, inspect element -> DOM is exactly as you have it in your result. jsfiddle.net/8cse0g9b
    – fdomn-m
    Commented Jun 12 at 10:33

0

Browse other questions tagged or ask your own question.