html-domparser.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * DOMParser HTML extension
  3. * 2012-09-04
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. * Public domain.
  7. * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  8. *
  9. * SPDX-FileCopyrightText: 2012 Eli Grey, http://eligrey.com
  10. * SPDX-License-Identifier: CC0-1.0
  11. */
  12. /*! @source https://gist.github.com/1129031 */
  13. /*global document, DOMParser*/
  14. (function(DOMParser) {
  15. "use strict";
  16. var DOMParser_proto = DOMParser.prototype;
  17. var real_parseFromString = DOMParser_proto.parseFromString;
  18. // Firefox/Opera/IE throw errors on unsupported types
  19. try {
  20. // WebKit returns null on unsupported types
  21. if ((new DOMParser).parseFromString("", "text/html")) {
  22. // text/html parsing is natively supported
  23. return;
  24. }
  25. } catch (ex) {}
  26. DOMParser_proto.parseFromString = function(markup, type) {
  27. if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
  28. var doc = document.implementation.createHTMLDocument("");
  29. if (markup.toLowerCase().indexOf('<!doctype') > -1) {
  30. doc.documentElement.innerHTML = markup;
  31. } else {
  32. doc.body.innerHTML = markup;
  33. }
  34. return doc;
  35. } else {
  36. return real_parseFromString.apply(this, arguments);
  37. }
  38. };
  39. }(DOMParser));