You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getSafeBodyFromHTML.js 1.1 KiB

3 years ago
1234567891011121314151617181920212223242526272829303132333435
  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. var UserAgent = require("fbjs/lib/UserAgent");
  13. var invariant = require("fbjs/lib/invariant");
  14. var isOldIE = UserAgent.isBrowser('IE <= 9'); // Provides a dom node that will not execute scripts
  15. // https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation.createHTMLDocument
  16. // https://developer.mozilla.org/en-US/Add-ons/Code_snippets/HTML_to_DOM
  17. function getSafeBodyFromHTML(html) {
  18. var doc;
  19. var root = null; // Provides a safe context
  20. if (!isOldIE && document.implementation && document.implementation.createHTMLDocument) {
  21. doc = document.implementation.createHTMLDocument('foo');
  22. !doc.documentElement ? process.env.NODE_ENV !== "production" ? invariant(false, 'Missing doc.documentElement') : invariant(false) : void 0;
  23. doc.documentElement.innerHTML = html;
  24. root = doc.getElementsByTagName('body')[0];
  25. }
  26. return root;
  27. }
  28. module.exports = getSafeBodyFromHTML;