]> Lady’s Gitweb - Lemon/blobdiff - window/mod.js
[window] Don’t use xmldom/lib/dom.js
[Lemon] / window / mod.js
index 5e97d4f2af8e289dc45496f943192f12056db703..9ddbf4bd6055ddbedfd50140a1dc8c946b9de367 100644 (file)
@@ -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 <https://mozilla.org/MPL/2.0/>.
 
-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();
This page took 0.026853 seconds and 4 git commands to generate.