shim.js 2.4 KB

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