]>
Lady’s Gitweb - Lemon/blob - window/mod.js
1 // 🍋🏷 Lemon ∷ window/mod.js
2 // ====================================================================
4 // Copyright © 2022 Lady [@ Lady’s Computer].
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/>.
10 import { DOMImplementation
, xhtmlNamespace
} from "./deps.js";
12 { // Polyfill document.
13 globalThis
.document
= new DOMImplementation().createDocument(
18 const { documentElement
} = document
;
19 documentElement
.appendChild(
20 document
.createElementNS(xhtmlNamespace
, "head"),
22 documentElement
.appendChild(
23 document
.createElementNS(xhtmlNamespace
, "body"),
27 { // Polyfill `element.append`.
28 Object
.getPrototypeOf(
29 globalThis
.document
.documentElement
,
30 ).append = function (...children
) {
31 for (const child
of children
) {
33 typeof child
=== "string"
34 ? this.ownerDocument
.createTextNode(child
)
41 { // Apply patches to `Node.nodeType` and `Node.normalize`.
42 const nodePrototype
= Object
.getPrototypeOf(
43 Object
.getPrototypeOf(document
.createDocumentFragment()),
45 const originalNodeTypeGetter
= Object
.getOwnPropertyDescriptor(
49 const originalNodeTypeValue
= nodePrototype
.nodeType
;
50 return () => originalNodeTypeValue
;
52 Object
.defineProperty(nodePrototype
, "nodeType", {
56 * To simulate restrictions on calling `nodeType` only on actual
57 * Nodes, check for inheritance first.
62 // Test whether this object has the node prototype in its
65 let prototype = this === Object(this)
66 ? Object
.getPrototypeOf(this)
69 prototype = Object
.getPrototypeOf(prototype)
71 if (prototype === nodePrototype
) {
72 // This object inherits from the Node prototype.
75 // This object is not yet known to inherits from the Node
82 // This is not a Node.
83 throw new TypeError("nodeType requires this be a Node.");
85 // This is a Node; walk the prototype chain and attempt to get
88 //deno-lint-ignore no-this-alias
90 target
!= null && target
!= nodePrototype
;
91 target
= Object
.getPrototypeOf(target
)
93 if (Object
.hasOwn(target
, "nodeType")) {
94 return Reflect
.get(target
, "nodeType", this);
99 return originalNodeTypeGetter
.call(this);
104 nodePrototype
.normalize = function () {
105 let child
= this.firstChild
;
107 const next
= child
.nextSibling
;
108 if (next
&& next
.nodeType
== 3 && child
.nodeType
== 3) {
109 this.removeChild(next
);
110 child
.appendData(next
.data
);
112 if (child
.nodeType
== 3 && !child
.data
) {
113 this.removeChild(child
);
This page took 0.120119 seconds and 5 git commands to generate.