]> Lady’s Gitweb - Lemon/blob - dev-deps.js
9a670e71dfd578ae2ea8a7cc52e24b7154ae252c
[Lemon] / dev-deps.js
1 // 🍋🏷 Lemon ∷ dev-deps.js
2 // ====================================================================
3 //
4 // Copyright © 2021–2022 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import { xhtmlNamespace } from "./names.js";
11 import {
12 DOMImplementation,
13 Node,
14 } from "https://esm.sh/@xmldom/xmldom@0.8.2/lib/dom.js";
15
16 { // Polyfill document.
17 globalThis.document = new DOMImplementation().createDocument(
18 xhtmlNamespace,
19 "html",
20 null,
21 );
22 }
23
24 { // Polyfill `element.append()`.
25 Object.getPrototypeOf(
26 globalThis.document.documentElement,
27 ).append = function (...children) {
28 for (const child of children) {
29 this.appendChild(
30 typeof child === "string"
31 ? this.ownerDocument.createTextNode(child)
32 : child,
33 );
34 }
35 };
36 }
37
38 { // Polyfill `Node` and apply patches to `Node.nodeType` and
39 // `Node.normalize`.
40 globalThis.Node = Node;
41 const originalNodeTypeGetter = Object.getOwnPropertyDescriptor(
42 Node.prototype,
43 "nodeType",
44 )?.get ?? (() => {
45 const originalNodeTypeValue = Node.prototype.nodeType;
46 return () => originalNodeTypeValue;
47 })();
48 Object.defineProperty(Node.prototype, "nodeType", {
49 configurable: true,
50 enumerable: true,
51 /**
52 * To simulate restrictions on calling `nodeType` only on actual
53 * Nodes, check for inheritance first.
54 */
55 get() {
56 if (!(this instanceof Node)) {
57 // This is not a Node.
58 throw new TypeError("nodeType requires this be a Node.");
59 } else {
60 // This is a Node; walk the prototype chain and attempt to get
61 // the node type.
62 for (
63 //deno-lint-ignore no-this-alias
64 let target = this;
65 target != null && target != Node.prototype;
66 target = Object.getPrototypeOf(target)
67 ) {
68 if (Object.hasOwn(target, "nodeType")) {
69 return Reflect.get(target, "nodeType", this);
70 } else {
71 continue;
72 }
73 }
74 return originalNodeTypeGetter.call(this);
75 }
76 },
77 set: undefined,
78 });
79 Node.prototype.normalize = function () { // modified from xmldom
80 let child = this.firstChild;
81 while (child) {
82 const next = child.nextSibling;
83 if (
84 next && next.nodeType == Node.TEXT_NODE &&
85 child.nodeType == Node.TEXT_NODE
86 ) {
87 this.removeChild(next);
88 child.appendData(next.data);
89 } else {
90 if (child.nodeType == Node.TEXT_NODE && !child.data) {
91 this.removeChild(child);
92 } else {
93 child.normalize();
94 }
95 child = next;
96 }
97 }
98 };
99 }
100
101 export { assertStrictEquals } from "https://deno.land/std@0.134.0/testing/asserts.ts";
This page took 0.093486 seconds and 3 git commands to generate.