Skip to content

document.implementation.createHTMLDocument behaves differently than browser #1779

@gavinwahl

Description

@gavinwahl

draft-js has this code in it (paraphrased):

function getSafeBodyFromHTML(html) {
  doc = document.implementation.createHTMLDocument('foo');
  doc.documentElement.innerHTML = html;
  return doc.getElementsByTagName('body')[0];
}

In Chrome 56 and Firefox 45, getSafeBodyFromHTML('') returns an HTMLBodyElement. In jsdom 9.12.0, it returns undefined. getSafeBodyFromHTML('foo') does return an HTMLBodyElement like it's supposed to.

You can reproduce this in node with this code:

> var jsdom = require('jsdom').jsdom;
> var document = jsdom();
> function getSafeBodyFromHTML(html) {
...   doc = document.implementation.createHTMLDocument('foo');
...   doc.documentElement.innerHTML = html;
...   return doc.getElementsByTagName('body')[0];
... }
> getSafeBodyFromHTML('')
undefined
> getSafeBodyFromHTML('asdf')
HTMLBodyElement {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions