UNPKG

1.15 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var UserAgent = require("fbjs/lib/UserAgent");
14
15var invariant = require("fbjs/lib/invariant");
16
17var isOldIE = UserAgent.isBrowser('IE <= 9'); // Provides a dom node that will not execute scripts
18// https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation.createHTMLDocument
19// https://developer.mozilla.org/en-US/Add-ons/Code_snippets/HTML_to_DOM
20
21function getSafeBodyFromHTML(html) {
22 var doc;
23 var root = null; // Provides a safe context
24
25 if (!isOldIE && document.implementation && document.implementation.createHTMLDocument) {
26 doc = document.implementation.createHTMLDocument('foo');
27 !doc.documentElement ? process.env.NODE_ENV !== "production" ? invariant(false, 'Missing doc.documentElement') : invariant(false) : void 0;
28 doc.documentElement.innerHTML = html;
29 root = doc.getElementsByTagName('body')[0];
30 }
31
32 return root;
33}
34
35module.exports = getSafeBodyFromHTML;
\No newline at end of file