Add an HTTP server for development purposes that replies to requests with both the static resources and on the fly generated pages. This serves the following needs. - Modern module scripts requires an origin supporting CORS policies, which is not supported when pages are read from disk by the browser. - Provide a way for implemting automatic reloading of pages when changes are made to the source files. - Act as the starting point for making interactive features such as comments and analytics.
57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import * as assert from "node:assert/strict";
|
|
import { suite, test } from "node:test";
|
|
import { resolveRefs } from "./resolve-refs.js";
|
|
import type { Element } from "antihtml";
|
|
|
|
|
|
suite("function resolveRefs", () => {
|
|
test("root to root relative href", () => {
|
|
const el = resolveRefs(<a href="/page.html">Link</a>, "/");
|
|
assert.equal((el as Element).attributes.get("href"), "page.html");
|
|
});
|
|
|
|
test("root to subdir relative href", () => {
|
|
const el = resolveRefs(<a href="/dir/page.html">Link</a>, "/");
|
|
assert.equal((el as Element).attributes.get("href"), "dir/page.html");
|
|
});
|
|
|
|
test("subdir to root relative href", () => {
|
|
const el = resolveRefs(<a href="/page.html">Link</a>, "/subdir");
|
|
assert.equal((el as Element).attributes.get("href"), "../page.html");
|
|
});
|
|
|
|
test("subdir to subdir relative href", () => {
|
|
const el = resolveRefs(<a href="/alt/page.html">Link</a>, "/subdir");
|
|
assert.equal((el as Element).attributes.get("href"), "../alt/page.html");
|
|
});
|
|
|
|
test("nested element", () => {
|
|
const el = resolveRefs(<div>Content with <a href="/page.html">Link</a></div>, "/");
|
|
assert.equal((el.childNodes[1] as Element).attributes.get("href"), "page.html");
|
|
});
|
|
|
|
test("returns element if no changes", () => {
|
|
const el = <div>Content with <em>emphasis</em></div>;
|
|
const resEl = resolveRefs(el, "/");
|
|
assert.equal(el, resEl);
|
|
});
|
|
|
|
test("returns new element if changed", () => {
|
|
const el = <div>Content with <a href="/page.html">Link</a></div>;
|
|
const resEl = resolveRefs(el, "/");
|
|
assert.notEqual(el, resEl);
|
|
});
|
|
|
|
test("does not modify input", () => {
|
|
const elFn = () => <div>Content with <a href="/page.html">Link</a></div>;
|
|
const el = elFn();
|
|
resolveRefs(el, "/");
|
|
assert.deepEqual(el, elFn());
|
|
});
|
|
|
|
test("ignores absolute URIs", () => {
|
|
const el = <div>Content with <a href="https://example.org/page.html">Link</a></div>;
|
|
const resEl = resolveRefs(el, "/");
|
|
assert.equal(el, resEl);
|
|
});
|
|
});
|