| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /**
- * DOM shim for the module-split tests.
- *
- * Extends the markdown harness shim with a CAPTURING console (so scenarios
- * can assert on the `[statum]` warnings/errors the seam emits) and a small
- * element tree builder. Like tests/markdown/shim.js, `document.readyState`
- * is 'loading' so statum-core.js only registers a DOMContentLoaded listener
- * instead of booting.
- */
- export function installShim() {
- var listeners = {};
- var log = { errors: [], warns: [], messages: [] };
- function makeElement(tag) {
- return {
- tagName: (tag || 'div').toUpperCase(),
- style: {}, dataset: {}, attributes: [],
- children: [],
- textContent: '', innerHTML: '',
- get firstChild() { return this.children[0] || null; },
- classList: { add: function () {}, remove: function () {}, contains: function () { return false; } },
- contains: function () { return false; },
- appendChild: function (c) { this.children.push(c); return c; },
- removeChild: function (c) {
- var i = this.children.indexOf(c);
- if (i >= 0) this.children.splice(i, 1);
- return c;
- },
- remove: function () {
- if (this.parentNode) this.parentNode.removeChild(this);
- },
- replaceWith: function () {},
- cloneNode: function () { return makeElement(this.tagName); },
- nextElementSibling: null,
- parentNode: null,
- addEventListener: function () {}, removeEventListener: function () {},
- setAttribute: function (n, v) {
- this.attributes = this.attributes.filter(function (a) { return a.name !== n; });
- this.attributes.push({ name: n, value: String(v) });
- },
- removeAttribute: function (n) {
- this.attributes = this.attributes.filter(function (a) { return a.name !== n; });
- },
- hasAttribute: function (n) {
- return this.attributes.some(function (a) { return a.name === n; });
- },
- getAttribute: function (n) {
- var hit = this.attributes.filter(function (a) { return a.name === n; })[0];
- return hit ? hit.value : null;
- },
- querySelectorAll: function () { return []; },
- querySelector: function () { return null; }
- };
- }
- globalThis.__shim = {
- log: log,
- makeElement: makeElement,
- body: makeElement('body')
- };
- globalThis.console = {
- log: function () { log.messages.push(['log', Array.prototype.slice.call(arguments)]); },
- info: function () { log.messages.push(['info', Array.prototype.slice.call(arguments)]); },
- debug: function () { log.messages.push(['debug', Array.prototype.slice.call(arguments)]); },
- warn: function () {
- log.warns.push(Array.prototype.slice.call(arguments).join(' '));
- log.messages.push(['warn', Array.prototype.slice.call(arguments)]);
- },
- error: function () {
- log.errors.push(Array.prototype.slice.call(arguments).join(' '));
- log.messages.push(['error', Array.prototype.slice.call(arguments)]);
- }
- };
- globalThis.document = {
- readyState: 'loading', cookie: '',
- head: makeElement('head'), body: globalThis.__shim.body,
- documentElement: makeElement('html'),
- addEventListener: function (t, f) { (listeners[t] = listeners[t] || []).push(f); },
- removeEventListener: function () {},
- getElementById: function () { return null; },
- querySelectorAll: function () { return []; },
- querySelector: function () { return null; },
- createElement: function (tag) { return makeElement(tag); },
- createTextNode: function (t) { return { nodeType: 3, textContent: t }; }
- };
- globalThis.window = globalThis;
- globalThis.location = { href: 'http://localhost/', protocol: 'http:', addEventListener: function () {} };
- globalThis.navigator = { userAgent: 'qjs' };
- globalThis.addEventListener = function (t, f) { (listeners[t] = listeners[t] || []).push(f); };
- globalThis.removeEventListener = function () {};
- globalThis.__STATUM_TEST__ = true;
- }
|