Parsing HTML From Chrome

  • Revision slug: Parsing_HTML_From_Chrome
  • Revision title: Parsing HTML From Chrome
  • Revision id: 344337
  • Created:
  • Creator: JohnJBarton1
  • Is current revision? Yes
  • Comment The old comments where bizarre and no longer correct. I tried some stuff and reported on what works now.

Revision Content

Chrome  25.0.1364.5 dev. At least some well formed HTML can be parsed by lying about the MIMEtype thus:

var doc = parser.parseFromString(html, "text/xml");

Using "text/html" always returns null, even if the the html is correct and simple, eg

new DOMParser().parseFromString("<html><head></head><body></body></html>", "text/html");

The form:

 var doc = document.implementation.createHTMLDocument("");
 doc.body.innerHTML = html;

seems to work fine for one case I tried. I guess it should work well in general.

Revision Source

<p>Chrome&nbsp;<span style="color: rgb(48, 57, 66); font-family: Ubuntu, Arial, sans-serif; font-size: 12px; line-height: normal;">&nbsp;25.0.1364.5 dev</span><span style="line-height: 1.572;">. At least some well formed HTML can be parsed by lying about the MIMEtype thus:</span></p>
<div style="margin-left: 40px;">
  <code>var doc = parser.parseFromString(html, "text/xml");</code></div>
<p>Using "text/html" always returns null, even if the the html is correct and simple, eg</p>
<p><span style="color: rgb(0, 128, 255); font-family: 'dejavu sans mono', monospace; font-size: 11px; line-height: normal; white-space: pre-wrap;">new DOMParser().parseFromString("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;", "text/html");</span></p>
<p>The form:</p>
<div style="margin-left: 40px;">
  <code><span class="webkit-javascript-keyword" style="font-family: 'dejavu sans mono', monospace; font-size: 11px; line-height: 13px; white-space: pre; box-sizing: border-box; color: rgb(170, 13, 145);">&nbsp;var doc = document.implementation.createHTMLDocument("");<br />
  &nbsp;doc.body.innerHTML = html;</span></code></div>
<p>seems to work fine for one case I tried. I guess it should work well in general.</p>
Revert to this revision