index.js 994 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * enables jsdom globally.
  3. */
  4. var KEYS = require('./keys')
  5. var defaultHtml = '<!doctype html><html><head><meta charset="utf-8">' +
  6. '</head><body></body></html>'
  7. module.exports = function globalJsdom (html, options) {
  8. if (html === undefined) {
  9. html = defaultHtml
  10. }
  11. if (options === undefined) {
  12. options = {}
  13. }
  14. // Idempotency
  15. if (global.navigator &&
  16. global.navigator.userAgent &&
  17. global.navigator.userAgent.indexOf('Node.js') > -1 &&
  18. global.document &&
  19. typeof global.document.destroy === 'function') {
  20. return global.document.destroy
  21. }
  22. var jsdom = require('jsdom')
  23. var document = new jsdom.JSDOM(html, options)
  24. var window = document.window
  25. KEYS.forEach(function (key) {
  26. global[key] = window[key]
  27. })
  28. global.document = window.document
  29. global.window = window
  30. window.console = global.console
  31. document.destroy = cleanup
  32. function cleanup () {
  33. KEYS.forEach(function (key) { delete global[key] })
  34. }
  35. return cleanup
  36. }