-{ // Polyfill document.
- globalThis.document = new DOMImplementation().createDocument(
- xhtmlNamespace,
- "html",
- null,
- );
-}
-
-{ // Polyfill `element.append()`.
- Object.getPrototypeOf(
- globalThis.document.documentElement,
- ).append = function (...children) {
- for (const child of children) {
- this.appendChild(
- typeof child === "string"
- ? this.ownerDocument.createTextNode(child)
- : child,
- );
- }
- };
-}
-
-{ // Polyfill `Node` and apply patches to `Node.nodeType` and
- // `Node.normalize`.
- globalThis.Node = Node;
- const originalNodeTypeGetter = Object.getOwnPropertyDescriptor(
- Node.prototype,
- "nodeType",
- )?.get ?? (() => {
- const originalNodeTypeValue = Node.prototype.nodeType;
- return () => originalNodeTypeValue;
- })();
- Object.defineProperty(Node.prototype, "nodeType", {
- configurable: true,
- enumerable: true,
- /**
- * To simulate restrictions on calling `nodeType` only on actual
- * Nodes, check for inheritance first.
- */
- get() {
- if (!(this instanceof Node)) {
- // This is not a Node.
- throw new TypeError("nodeType requires this be a Node.");
- } else {
- // This is a Node; walk the prototype chain and attempt to get
- // the node type.
- for (
- //deno-lint-ignore no-this-alias
- let target = this;
- target != null && target != Node.prototype;
- target = Object.getPrototypeOf(target)
- ) {
- if (Object.hasOwn(target, "nodeType")) {
- return Reflect.get(target, "nodeType", this);
- } else {
- continue;
- }
- }
- return originalNodeTypeGetter.call(this);
- }
- },
- set: undefined,
- });
- Node.prototype.normalize = function () { // modified from xmldom
- let child = this.firstChild;
- while (child) {
- const next = child.nextSibling;
- if (
- next && next.nodeType == Node.TEXT_NODE &&
- child.nodeType == Node.TEXT_NODE
- ) {
- this.removeChild(next);
- child.appendData(next.data);
- } else {
- if (child.nodeType == Node.TEXT_NODE && !child.data) {
- this.removeChild(child);
- } else {
- child.normalize();
- }
- child = next;
- }
- }
- };
-}