Files
cocos4/platforms/nodejs/builtin/node-adapter/Node.js
T
2025-10-10 20:17:12 +08:00

51 lines
1020 B
JavaScript

const EventTarget = require('./EventTarget');
const jsbWindow = globalThis.window;
class Node extends EventTarget {
constructor () {
super();
this.childNodes = [];
this.parentNode = jsbWindow.__canvas;
}
appendChild (node) {
if (node instanceof Node) {
this.childNodes.push(node);
} else {
throw new TypeError('Failed to executed \'appendChild\' on \'Node\': parameter 1 is not of type \'Node\'.');
}
}
insertBefore (newNode, referenceNode) {
//REFINE:
return newNode;
}
replaceChild (newChild, oldChild) {
//REFINE:
return oldChild;
}
cloneNode () {
const copyNode = Object.create(this);
Object.assign(copyNode, this);
return copyNode;
}
removeChild (node) {
const index = this.childNodes.findIndex((child) => child === node);
if (index > -1) {
return this.childNodes.splice(index, 1);
}
return null;
}
contains (node) {
return this.childNodes.indexOf(node) > -1;
}
}
module.exports = Node;