From: Lady Date: Sun, 7 Aug 2022 06:09:03 +0000 (-0700) Subject: [window] Don’t use xmldom/lib/dom.js X-Git-Tag: 0.2.1 X-Git-Url: https://git.ladys.computer/Lemon/commitdiff_plain/3f0fb84f4cb391464680ba600592df3b811e44a8?ds=inline;hp=c96322b9c5d2f62c2817f13c644b3d2e53cd3501 [window] Don’t use xmldom/lib/dom.js To allow interop with scripts which depend on “root” xmldom. --- diff --git a/window/deps.js b/window/deps.js index f1d4237..f78b74c 100644 --- a/window/deps.js +++ b/window/deps.js @@ -9,6 +9,5 @@ export { DOMImplementation, - Node, -} from "https://esm.sh/@xmldom/xmldom@0.8.2/lib/dom.js"; +} from "https://esm.sh/@xmldom/xmldom@0.8.2"; export { xhtmlNamespace } from "../names.js"; diff --git a/window/mod.js b/window/mod.js index 5e97d4f..9ddbf4b 100644 --- a/window/mod.js +++ b/window/mod.js @@ -7,7 +7,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at . -import { DOMImplementation, Node, xhtmlNamespace } from "./deps.js"; +import { DOMImplementation, xhtmlNamespace } from "./deps.js"; { // Polyfill document. globalThis.document = new DOMImplementation().createDocument( @@ -39,7 +39,9 @@ import { DOMImplementation, Node, xhtmlNamespace } from "./deps.js"; } { // Apply patches to `Node.nodeType` and `Node.normalize`. - const { TEXT_NODE, prototype: nodePrototype } = Node; + const nodePrototype = Object.getPrototypeOf( + Object.getPrototypeOf(document.createDocumentFragment()), + ); const originalNodeTypeGetter = Object.getOwnPropertyDescriptor( nodePrototype, "nodeType", @@ -55,7 +57,28 @@ import { DOMImplementation, Node, xhtmlNamespace } from "./deps.js"; * Nodes, check for inheritance first. */ get() { - if (!(this instanceof Node)) { + if ( + !(() => { + // Test whether this object has the node prototype in its + // prototype chain. + for ( + let prototype = this === Object(this) + ? Object.getPrototypeOf(this) + : null; + prototype != null; + prototype = Object.getPrototypeOf(prototype) + ) { + if (prototype === nodePrototype) { + // This object inherits from the Node prototype. + return true; + } else { + // This object is not yet known to inherits from the Node + // prototype. + continue; + } + } + })() + ) { // This is not a Node. throw new TypeError("nodeType requires this be a Node."); } else { @@ -82,14 +105,11 @@ import { DOMImplementation, Node, xhtmlNamespace } from "./deps.js"; let child = this.firstChild; while (child) { const next = child.nextSibling; - if ( - next && next.nodeType == TEXT_NODE && - child.nodeType == TEXT_NODE - ) { + if (next && next.nodeType == 3 && child.nodeType == 3) { this.removeChild(next); child.appendData(next.data); } else { - if (child.nodeType == TEXT_NODE && !child.data) { + if (child.nodeType == 3 && !child.data) { this.removeChild(child); } else { child.normalize();