shim.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Minimal DOM shim so the Statum module files can be loaded under QuickJS.
  3. *
  4. * The modules are IIFEs; statum-core.js attaches `window.statum` and calls
  5. * autoInit() at load time. autoInit() only needs `document.readyState` (we
  6. * report 'loading' so it registers a DOMContentLoaded listener instead of
  7. * running), and the rest of the boot path is never exercised by the markdown
  8. * tests.
  9. *
  10. * Set `__STATUM_TEST__` before loading statum-markdown.js to expose
  11. * `statum._testMarkdown` (see the comment at the bottom of that module).
  12. */
  13. export function installShim() {
  14. var listeners = {};
  15. function makeElement() {
  16. return {
  17. tagName: 'DIV', style: {}, dataset: {}, attributes: [], children: [],
  18. setAttribute: function () {}, removeAttribute: function () {},
  19. hasAttribute: function () { return false; },
  20. getAttribute: function () { return null; },
  21. addEventListener: function () {}, removeEventListener: function () {},
  22. appendChild: function (c) { return c; },
  23. removeChild: function (c) { return c; },
  24. replaceWith: function () {},
  25. querySelectorAll: function () { return []; },
  26. querySelector: function () { return null; },
  27. classList: { add: function () {}, remove: function () {}, contains: function () { return false; } },
  28. contains: function () { return false; }
  29. };
  30. }
  31. globalThis.console = {
  32. log: function () {}, warn: function () {}, error: function () {},
  33. info: function () {}, debug: function () {}
  34. };
  35. globalThis.document = {
  36. readyState: 'loading', cookie: '',
  37. head: makeElement(), body: makeElement(), documentElement: makeElement(),
  38. addEventListener: function (t, f) { (listeners[t] = listeners[t] || []).push(f); },
  39. removeEventListener: function () {},
  40. getElementById: function () { return null; },
  41. querySelectorAll: function () { return []; },
  42. querySelector: function () { return null; },
  43. createElement: function () { return makeElement(); },
  44. createTextNode: function (t) { return { nodeType: 3, textContent: t }; }
  45. };
  46. globalThis.window = globalThis;
  47. globalThis.location = { href: 'http://localhost/', protocol: 'http:', addEventListener: function () {} };
  48. globalThis.navigator = { userAgent: 'qjs' };
  49. globalThis.addEventListener = function (t, f) { (listeners[t] = listeners[t] || []).push(f); };
  50. globalThis.removeEventListener = function () {};
  51. globalThis.__STATUM_TEST__ = true;
  52. }