Skip to content

Commit 1632eba

Browse files
RReversersaschanaz
authored andcommitted
Simplify / fix HTMLCollection overrides
Use an extra optional type param on HTMLCollection for `namedItem` override. This simplifies / removes quite a lot of emitter code that was necessary to maintain just for this override. Instead, this PR is using regular backward-compatible type system capabilities. As an additional benefit, this removes a phantom HTMLCollectionOf class that previously appeared as a valid global, but doesn't really exist in JavaScript global object.
1 parent f9d5f11 commit 1632eba

File tree

5 files changed

+47
-64
lines changed

5 files changed

+47
-64
lines changed

baselines/dom.generated.d.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6020,30 +6020,21 @@ declare var HTMLCanvasElement: {
60206020
};
60216021

60226022
/** A generic collection (array-like object similar to arguments) of elements (in document order) and offers methods and properties for selecting from the list. */
6023-
interface HTMLCollectionBase {
6023+
interface HTMLCollection<E extends Element = Element, N = never> {
60246024
/** Sets or retrieves the number of objects in a collection. */
60256025
readonly length: number;
60266026
/** Retrieves an object from various collections. */
6027-
item(index: number): Element | null;
6028-
[index: number]: Element;
6029-
}
6030-
6031-
interface HTMLCollection extends HTMLCollectionBase {
6027+
item(index: number): E | null;
60326028
/** Retrieves a select object or an object from an options collection. */
6033-
namedItem(name: string): Element | null;
6029+
namedItem(name: string): E | N | null;
6030+
[index: number]: E;
60346031
}
60356032

60366033
declare var HTMLCollection: {
60376034
prototype: HTMLCollection;
60386035
new(): HTMLCollection;
60396036
};
60406037

6041-
interface HTMLCollectionOf<T extends Element> extends HTMLCollectionBase {
6042-
item(index: number): T | null;
6043-
namedItem(name: string): T | null;
6044-
[index: number]: T;
6045-
}
6046-
60476038
/** Provides special properties (beyond those of the regular HTMLElement interface it also has available to it by inheritance) for manipulating definition list (<dl>) elements. */
60486039
interface HTMLDListElement extends HTMLElement {
60496040
/** @deprecated */
@@ -6282,7 +6273,7 @@ declare var HTMLFontElement: {
62826273
};
62836274

62846275
/** A collection of HTML form control elements. */
6285-
interface HTMLFormControlsCollection extends HTMLCollectionBase {
6276+
interface HTMLFormControlsCollection extends HTMLCollection<Element, RadioNodeList> {
62866277
/**
62876278
* Returns the item with ID or name name from the collection.
62886279
*
@@ -7331,7 +7322,7 @@ declare var HTMLOptionElement: {
73317322
};
73327323

73337324
/** HTMLOptionsCollection is an interface representing a collection of HTML option elements (in document order) and offers methods and properties for traversing the list as well as optionally altering its items. This type is returned solely by the "options" property of select. */
7334-
interface HTMLOptionsCollection extends HTMLCollectionOf<HTMLOptionElement> {
7325+
interface HTMLOptionsCollection extends HTMLCollection<HTMLOptionElement> {
73357326
/**
73367327
* Returns the number of elements in the collection.
73377328
*

baselines/dom.iterable.generated.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,10 @@ interface HTMLAllCollection {
7070
[Symbol.iterator](): IterableIterator<Element>;
7171
}
7272

73-
interface HTMLCollectionBase {
73+
interface HTMLCollection<E extends Element = Element, N = never> {
7474
[Symbol.iterator](): IterableIterator<Element>;
7575
}
7676

77-
interface HTMLCollectionOf<T extends Element> {
78-
[Symbol.iterator](): IterableIterator<T>;
79-
}
80-
8177
interface HTMLFormElement {
8278
[Symbol.iterator](): IterableIterator<Element>;
8379
}

inputfiles/addedTypes.jsonc

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -497,45 +497,6 @@
497497
]
498498
}
499499
},
500-
"HTMLCollectionOf": {
501-
"name": "HTMLCollectionOf",
502-
"typeParameters": [
503-
{
504-
"name": "T",
505-
"extends": "Element"
506-
}
507-
],
508-
"exposed": "Window",
509-
"extends": "HTMLCollection",
510-
"methods": {
511-
"method": {
512-
"item": {
513-
"getter": true,
514-
"signature": [
515-
{
516-
"nullable": true,
517-
"overrideType": "T",
518-
"param": [
519-
{
520-
"name": "index",
521-
"type": "unsigned long"
522-
}
523-
]
524-
}
525-
],
526-
"specs": "html5",
527-
"name": "item"
528-
},
529-
"namedItem": {
530-
"name": "namedItem",
531-
"overrideSignatures": [
532-
"namedItem(name: string): T | null"
533-
]
534-
}
535-
}
536-
},
537-
"noInterfaceObject": true
538-
},
539500
"Element": {
540501
"name": "Element",
541502
"methods": {

inputfiles/overridingTypes.jsonc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@
13531353
]
13541354
},
13551355
"HTMLOptionsCollection": {
1356-
"extends": "HTMLCollectionOf<HTMLOptionElement>"
1356+
"extends": "HTMLCollection<HTMLOptionElement>"
13571357
},
13581358
"SubtleCrypto": {
13591359
"methods": {
@@ -2800,6 +2800,41 @@
28002800
}
28012801
}
28022802
},
2803+
"HTMLCollection": {
2804+
"typeParameters": [
2805+
{
2806+
"name": "E",
2807+
"extends": "Element",
2808+
"default": "Element"
2809+
},
2810+
{
2811+
"name": "N",
2812+
"default": "never"
2813+
}
2814+
],
2815+
"methods": {
2816+
"method": {
2817+
"item": {
2818+
"name": "item",
2819+
"overrideSignatures": [
2820+
"item(index: number): E | null"
2821+
]
2822+
},
2823+
"namedItem": {
2824+
"name": "namedItem",
2825+
"overrideSignatures": [
2826+
"namedItem(name: string): E | N | null"
2827+
]
2828+
}
2829+
}
2830+
},
2831+
"overrideIndexSignatures": [
2832+
"[index: number]: E"
2833+
]
2834+
},
2835+
"HTMLFormControlsCollection": {
2836+
"extends": "HTMLCollection<Element, RadioNodeList>"
2837+
},
28032838
"HTMLMediaElement": {
28042839
"properties": {
28052840
"property": {

src/build/emitter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const extendConflictsBaseTypes: Record<
2424
string,
2525
{ extendType: string[]; memberNames: Set<string> }
2626
> = {
27-
HTMLCollection: {
28-
extendType: ["HTMLFormControlsCollection"],
29-
memberNames: new Set(["namedItem"]),
30-
},
27+
// HTMLCollection: {
28+
// extendType: ["HTMLFormControlsCollection"],
29+
// memberNames: new Set(["namedItem"]),
30+
// },
3131
};
3232

3333
// Namespaces that have been in form of interfaces for years

0 commit comments

Comments
 (0)