happy-dom
Version:
Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.
223 lines • 7.37 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ClassMethodBinder_js_1 = __importDefault(require("../../utilities/ClassMethodBinder.cjs"));
const PropertySymbol = __importStar(require("../../PropertySymbol.cjs"));
/**
* NodeList.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/NodeList
*/
class NodeList {
[PropertySymbol.items];
/**
* Constructor.
*
* @param illegalConstructorSymbol Illegal constructor symbol.
* @param items Items.
*/
constructor(illegalConstructorSymbol, items) {
if (illegalConstructorSymbol !== PropertySymbol.illegalConstructor) {
throw new TypeError('Illegal constructor');
}
this[PropertySymbol.items] = items;
const methodBinder = new ClassMethodBinder_js_1.default(this, this.constructor !== NodeList ? [this.constructor, NodeList] : [NodeList]);
const proxy = new Proxy(this, {
get: (target, property) => {
if (property === 'length') {
return items.length;
}
if (property in target || typeof property === 'symbol') {
methodBinder.bind(property);
return target[property];
}
if (property === '') {
return undefined;
}
const index = Number(property);
if (!isNaN(index)) {
return items[index];
}
},
set(target, property, newValue) {
methodBinder.bind(property);
if (typeof property === 'symbol') {
target[property] = newValue;
return true;
}
const index = Number(property);
if (isNaN(index)) {
target[property] = newValue;
}
return true;
},
deleteProperty(target, property) {
if (typeof property === 'symbol') {
delete target[property];
return true;
}
const index = Number(property);
if (isNaN(index)) {
delete target[property];
}
return true;
},
ownKeys() {
return Object.keys(items);
},
has(target, property) {
if (property in target) {
return true;
}
if (typeof property === 'symbol') {
return false;
}
const index = Number(property);
return !isNaN(index) && index >= 0 && index < items.length;
},
defineProperty(target, property, descriptor) {
methodBinder.preventBinding(property);
if (property in target) {
Object.defineProperty(target, property, descriptor);
return true;
}
return false;
},
getOwnPropertyDescriptor(target, property) {
if (property in target || typeof property === 'symbol') {
return;
}
const index = Number(property);
if (!isNaN(index) && items[index]) {
return {
value: items[index],
writable: false,
enumerable: true,
configurable: true
};
}
}
});
return proxy;
}
/**
* Returns length.
*
* @returns Length.
*/
get length() {
return this[PropertySymbol.items].length;
}
/**
* Returns `Symbol.toStringTag`.
*
* @returns `Symbol.toStringTag`.
*/
get [Symbol.toStringTag]() {
return 'NodeList';
}
/**
* Returns `[object NodeList]`.
*
* @returns `[object NodeList]`.
*/
toLocaleString() {
return '[object NodeList]';
}
/**
* Returns `[object NodeList]`.
*
* @returns `[object NodeList]`.
*/
toString() {
return '[object NodeList]';
}
/**
* Returns item by index.
*
* @param index Index.
*/
item(index) {
const nodes = this[PropertySymbol.items];
return index >= 0 && nodes[index] ? nodes[index] : null;
}
/**
* Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
*
* @returns Iterator.
*/
[Symbol.iterator]() {
const items = this[PropertySymbol.items];
return items[Symbol.iterator]();
}
/**
* Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
*
* @returns Iterator.
*/
values() {
return this[PropertySymbol.items].values();
}
/**
* Returns an iterator, allowing you to go through all key/value pairs contained in this object.
*
* @returns Iterator.
*/
entries() {
return this[PropertySymbol.items].entries();
}
/**
* Executes a provided callback function once for each DOMTokenList element.
*
* @param callback Function.
* @param thisArg thisArg.
*/
forEach(callback, thisArg) {
return this[PropertySymbol.items].forEach(callback, thisArg);
}
/**
* Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
*
* @returns Iterator.
*/
keys() {
return this[PropertySymbol.items].keys();
}
}
exports.default = NodeList;
//# sourceMappingURL=NodeList.cjs.map