HTMLCollection.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const implSymbol = utils.implSymbol;
  5. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  6. const interfaceName = "HTMLCollection";
  7. exports.is = value => {
  8. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  9. };
  10. exports.isImpl = value => {
  11. return utils.isObject(value) && value instanceof Impl.implementation;
  12. };
  13. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  14. if (exports.is(value)) {
  15. return utils.implForWrapper(value);
  16. }
  17. throw new globalObject.TypeError(`${context} is not of type 'HTMLCollection'.`);
  18. };
  19. function makeWrapper(globalObject, newTarget) {
  20. let proto;
  21. if (newTarget !== undefined) {
  22. proto = newTarget.prototype;
  23. }
  24. if (!utils.isObject(proto)) {
  25. proto = globalObject[ctorRegistrySymbol]["HTMLCollection"].prototype;
  26. }
  27. return Object.create(proto);
  28. }
  29. function makeProxy(wrapper, globalObject) {
  30. let proxyHandler = proxyHandlerCache.get(globalObject);
  31. if (proxyHandler === undefined) {
  32. proxyHandler = new ProxyHandler(globalObject);
  33. proxyHandlerCache.set(globalObject, proxyHandler);
  34. }
  35. return new Proxy(wrapper, proxyHandler);
  36. }
  37. exports.create = (globalObject, constructorArgs, privateData) => {
  38. const wrapper = makeWrapper(globalObject);
  39. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  40. };
  41. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  42. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  43. return utils.implForWrapper(wrapper);
  44. };
  45. exports._internalSetup = (wrapper, globalObject) => {};
  46. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  47. privateData.wrapper = wrapper;
  48. exports._internalSetup(wrapper, globalObject);
  49. Object.defineProperty(wrapper, implSymbol, {
  50. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  51. configurable: true
  52. });
  53. wrapper = makeProxy(wrapper, globalObject);
  54. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  55. if (Impl.init) {
  56. Impl.init(wrapper[implSymbol]);
  57. }
  58. return wrapper;
  59. };
  60. exports.new = (globalObject, newTarget) => {
  61. let wrapper = makeWrapper(globalObject, newTarget);
  62. exports._internalSetup(wrapper, globalObject);
  63. Object.defineProperty(wrapper, implSymbol, {
  64. value: Object.create(Impl.implementation.prototype),
  65. configurable: true
  66. });
  67. wrapper = makeProxy(wrapper, globalObject);
  68. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  69. if (Impl.init) {
  70. Impl.init(wrapper[implSymbol]);
  71. }
  72. return wrapper[implSymbol];
  73. };
  74. const exposed = new Set(["Window"]);
  75. exports.install = (globalObject, globalNames) => {
  76. if (!globalNames.some(globalName => exposed.has(globalName))) {
  77. return;
  78. }
  79. const ctorRegistry = utils.initCtorRegistry(globalObject);
  80. class HTMLCollection {
  81. constructor() {
  82. throw new globalObject.TypeError("Illegal constructor");
  83. }
  84. item(index) {
  85. const esValue = this !== null && this !== undefined ? this : globalObject;
  86. if (!exports.is(esValue)) {
  87. throw new globalObject.TypeError("'item' called on an object that is not a valid instance of HTMLCollection.");
  88. }
  89. if (arguments.length < 1) {
  90. throw new globalObject.TypeError(
  91. `Failed to execute 'item' on 'HTMLCollection': 1 argument required, but only ${arguments.length} present.`
  92. );
  93. }
  94. const args = [];
  95. {
  96. let curArg = arguments[0];
  97. curArg = conversions["unsigned long"](curArg, {
  98. context: "Failed to execute 'item' on 'HTMLCollection': parameter 1",
  99. globals: globalObject
  100. });
  101. args.push(curArg);
  102. }
  103. return utils.tryWrapperForImpl(esValue[implSymbol].item(...args));
  104. }
  105. namedItem(name) {
  106. const esValue = this !== null && this !== undefined ? this : globalObject;
  107. if (!exports.is(esValue)) {
  108. throw new globalObject.TypeError(
  109. "'namedItem' called on an object that is not a valid instance of HTMLCollection."
  110. );
  111. }
  112. if (arguments.length < 1) {
  113. throw new globalObject.TypeError(
  114. `Failed to execute 'namedItem' on 'HTMLCollection': 1 argument required, but only ${arguments.length} present.`
  115. );
  116. }
  117. const args = [];
  118. {
  119. let curArg = arguments[0];
  120. curArg = conversions["DOMString"](curArg, {
  121. context: "Failed to execute 'namedItem' on 'HTMLCollection': parameter 1",
  122. globals: globalObject
  123. });
  124. args.push(curArg);
  125. }
  126. return utils.tryWrapperForImpl(esValue[implSymbol].namedItem(...args));
  127. }
  128. get length() {
  129. const esValue = this !== null && this !== undefined ? this : globalObject;
  130. if (!exports.is(esValue)) {
  131. throw new globalObject.TypeError(
  132. "'get length' called on an object that is not a valid instance of HTMLCollection."
  133. );
  134. }
  135. return esValue[implSymbol]["length"];
  136. }
  137. }
  138. Object.defineProperties(HTMLCollection.prototype, {
  139. item: { enumerable: true },
  140. namedItem: { enumerable: true },
  141. length: { enumerable: true },
  142. [Symbol.toStringTag]: { value: "HTMLCollection", configurable: true },
  143. [Symbol.iterator]: { value: globalObject.Array.prototype[Symbol.iterator], configurable: true, writable: true }
  144. });
  145. ctorRegistry[interfaceName] = HTMLCollection;
  146. Object.defineProperty(globalObject, interfaceName, {
  147. configurable: true,
  148. writable: true,
  149. value: HTMLCollection
  150. });
  151. };
  152. const proxyHandlerCache = new WeakMap();
  153. class ProxyHandler {
  154. constructor(globalObject) {
  155. this._globalObject = globalObject;
  156. }
  157. get(target, P, receiver) {
  158. if (typeof P === "symbol") {
  159. return Reflect.get(target, P, receiver);
  160. }
  161. const desc = this.getOwnPropertyDescriptor(target, P);
  162. if (desc === undefined) {
  163. const parent = Object.getPrototypeOf(target);
  164. if (parent === null) {
  165. return undefined;
  166. }
  167. return Reflect.get(target, P, receiver);
  168. }
  169. if (!desc.get && !desc.set) {
  170. return desc.value;
  171. }
  172. const getter = desc.get;
  173. if (getter === undefined) {
  174. return undefined;
  175. }
  176. return Reflect.apply(getter, receiver, []);
  177. }
  178. has(target, P) {
  179. if (typeof P === "symbol") {
  180. return Reflect.has(target, P);
  181. }
  182. const desc = this.getOwnPropertyDescriptor(target, P);
  183. if (desc !== undefined) {
  184. return true;
  185. }
  186. const parent = Object.getPrototypeOf(target);
  187. if (parent !== null) {
  188. return Reflect.has(parent, P);
  189. }
  190. return false;
  191. }
  192. ownKeys(target) {
  193. const keys = new Set();
  194. for (const key of target[implSymbol][utils.supportedPropertyIndices]) {
  195. keys.add(`${key}`);
  196. }
  197. for (const key of target[implSymbol][utils.supportedPropertyNames]) {
  198. if (!(key in target)) {
  199. keys.add(`${key}`);
  200. }
  201. }
  202. for (const key of Reflect.ownKeys(target)) {
  203. keys.add(key);
  204. }
  205. return [...keys];
  206. }
  207. getOwnPropertyDescriptor(target, P) {
  208. if (typeof P === "symbol") {
  209. return Reflect.getOwnPropertyDescriptor(target, P);
  210. }
  211. let ignoreNamedProps = false;
  212. if (utils.isArrayIndexPropName(P)) {
  213. const index = P >>> 0;
  214. const indexedValue = target[implSymbol].item(index);
  215. if (indexedValue !== null) {
  216. return {
  217. writable: false,
  218. enumerable: true,
  219. configurable: true,
  220. value: utils.tryWrapperForImpl(indexedValue)
  221. };
  222. }
  223. ignoreNamedProps = true;
  224. }
  225. const namedValue = target[implSymbol].namedItem(P);
  226. if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
  227. return {
  228. writable: false,
  229. enumerable: false,
  230. configurable: true,
  231. value: utils.tryWrapperForImpl(namedValue)
  232. };
  233. }
  234. return Reflect.getOwnPropertyDescriptor(target, P);
  235. }
  236. set(target, P, V, receiver) {
  237. if (typeof P === "symbol") {
  238. return Reflect.set(target, P, V, receiver);
  239. }
  240. // The `receiver` argument refers to the Proxy exotic object or an object
  241. // that inherits from it, whereas `target` refers to the Proxy target:
  242. if (target[implSymbol][utils.wrapperSymbol] === receiver) {
  243. const globalObject = this._globalObject;
  244. }
  245. let ownDesc;
  246. if (utils.isArrayIndexPropName(P)) {
  247. const index = P >>> 0;
  248. const indexedValue = target[implSymbol].item(index);
  249. if (indexedValue !== null) {
  250. ownDesc = {
  251. writable: false,
  252. enumerable: true,
  253. configurable: true,
  254. value: utils.tryWrapperForImpl(indexedValue)
  255. };
  256. }
  257. }
  258. if (ownDesc === undefined) {
  259. ownDesc = Reflect.getOwnPropertyDescriptor(target, P);
  260. }
  261. return utils.ordinarySetWithOwnDescriptor(target, P, V, receiver, ownDesc);
  262. }
  263. defineProperty(target, P, desc) {
  264. if (typeof P === "symbol") {
  265. return Reflect.defineProperty(target, P, desc);
  266. }
  267. const globalObject = this._globalObject;
  268. if (utils.isArrayIndexPropName(P)) {
  269. return false;
  270. }
  271. if (!Object.hasOwn(target, P)) {
  272. const creating = !(target[implSymbol].namedItem(P) !== null);
  273. if (!creating) {
  274. return false;
  275. }
  276. }
  277. return Reflect.defineProperty(target, P, desc);
  278. }
  279. deleteProperty(target, P) {
  280. if (typeof P === "symbol") {
  281. return Reflect.deleteProperty(target, P);
  282. }
  283. const globalObject = this._globalObject;
  284. if (utils.isArrayIndexPropName(P)) {
  285. const index = P >>> 0;
  286. return !(target[implSymbol].item(index) !== null);
  287. }
  288. if (target[implSymbol].namedItem(P) !== null && !(P in target)) {
  289. return false;
  290. }
  291. return Reflect.deleteProperty(target, P);
  292. }
  293. preventExtensions() {
  294. return false;
  295. }
  296. }
  297. const Impl = require("../../jsdom/living/nodes/HTMLCollection-impl.js");