html-parser.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Set up window for Node.js
  3. */
  4. var _window = (typeof window !== 'undefined' ? window : this)
  5. /*
  6. * Parsing HTML strings
  7. */
  8. function canParseHtmlNatively () {
  9. var Parser = _window.DOMParser
  10. var canParse = false
  11. // Adapted from https://gist.github.com/1129031
  12. // Firefox/Opera/IE throw errors on unsupported types
  13. try {
  14. // WebKit returns null on unsupported types
  15. if (new Parser().parseFromString('', 'text/html')) {
  16. canParse = true
  17. }
  18. } catch (e) {}
  19. return canParse
  20. }
  21. function createHtmlParser () {
  22. var Parser = function () {}
  23. // For Node.js environments
  24. if (typeof document === 'undefined') {
  25. var jsdom = require('jsdom')
  26. Parser.prototype.parseFromString = function (string) {
  27. return jsdom.jsdom(string, {
  28. features: {
  29. FetchExternalResources: [],
  30. ProcessExternalResources: false
  31. }
  32. })
  33. }
  34. } else {
  35. if (!shouldUseActiveX()) {
  36. Parser.prototype.parseFromString = function (string) {
  37. var doc = document.implementation.createHTMLDocument('')
  38. doc.open()
  39. doc.write(string)
  40. doc.close()
  41. return doc
  42. }
  43. } else {
  44. Parser.prototype.parseFromString = function (string) {
  45. var doc = new window.ActiveXObject('htmlfile')
  46. doc.designMode = 'on' // disable on-page scripts
  47. doc.open()
  48. doc.write(string)
  49. doc.close()
  50. return doc
  51. }
  52. }
  53. }
  54. return Parser
  55. }
  56. function shouldUseActiveX () {
  57. var useActiveX = false
  58. try {
  59. document.implementation.createHTMLDocument('').open()
  60. } catch (e) {
  61. if (window.ActiveXObject) useActiveX = true
  62. }
  63. return useActiveX
  64. }
  65. module.exports = canParseHtmlNatively() ? _window.DOMParser : createHtmlParser()