Skip to content

Commit 5841c2b

Browse files
committed
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 4a32410 commit 5841c2b

File tree

5 files changed

+136
-189
lines changed

5 files changed

+136
-189
lines changed

baselines/dom.generated.d.ts

+35-44
Original file line numberDiff line numberDiff line change
@@ -4562,12 +4562,12 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
45624562
* Retrieves a collection of all a objects that have a name and/or id property. Objects in this collection are in HTML source order.
45634563
*/
45644564
/** @deprecated */
4565-
readonly anchors: HTMLCollectionOf<HTMLAnchorElement>;
4565+
readonly anchors: HTMLCollection<HTMLAnchorElement>;
45664566
/**
45674567
* Retrieves a collection of all applet objects in the document.
45684568
*/
45694569
/** @deprecated */
4570-
readonly applets: HTMLCollectionOf<HTMLAppletElement>;
4570+
readonly applets: HTMLCollection<HTMLAppletElement>;
45714571
/**
45724572
* Deprecated. Sets or retrieves a value that indicates the background color behind the object.
45734573
*/
@@ -4635,7 +4635,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46354635
/**
46364636
* Retrieves a collection of all embed objects in the document.
46374637
*/
4638-
readonly embeds: HTMLCollectionOf<HTMLEmbedElement>;
4638+
readonly embeds: HTMLCollection<HTMLEmbedElement>;
46394639
/**
46404640
* Sets or gets the foreground (text) color of the document.
46414641
*/
@@ -4644,7 +4644,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46444644
/**
46454645
* Retrieves a collection, in source order, of all form objects in the document.
46464646
*/
4647-
readonly forms: HTMLCollectionOf<HTMLFormElement>;
4647+
readonly forms: HTMLCollection<HTMLFormElement>;
46484648
/** @deprecated */
46494649
readonly fullscreen: boolean;
46504650
/**
@@ -4659,7 +4659,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46594659
/**
46604660
* Retrieves a collection, in source order, of img objects in the document.
46614661
*/
4662-
readonly images: HTMLCollectionOf<HTMLImageElement>;
4662+
readonly images: HTMLCollection<HTMLImageElement>;
46634663
/**
46644664
* Gets the implementation object of the current document.
46654665
*/
@@ -4680,7 +4680,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46804680
/**
46814681
* Retrieves a collection of all a objects that specify the href property and all area objects in the document.
46824682
*/
4683-
readonly links: HTMLCollectionOf<HTMLAnchorElement | HTMLAreaElement>;
4683+
readonly links: HTMLCollection<HTMLAnchorElement | HTMLAreaElement>;
46844684
/**
46854685
* Contains information about the current URL.
46864686
*/
@@ -4702,7 +4702,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
47024702
/**
47034703
* Return an HTMLCollection of the embed elements in the Document.
47044704
*/
4705-
readonly plugins: HTMLCollectionOf<HTMLEmbedElement>;
4705+
readonly plugins: HTMLCollection<HTMLEmbedElement>;
47064706
/**
47074707
* Retrieves a value that indicates the current state of the object.
47084708
*/
@@ -4714,7 +4714,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
47144714
/**
47154715
* Retrieves a collection of all script objects in the document.
47164716
*/
4717-
readonly scripts: HTMLCollectionOf<HTMLScriptElement>;
4717+
readonly scripts: HTMLCollection<HTMLScriptElement>;
47184718
readonly scrollingElement: Element | null;
47194719
readonly timeline: DocumentTimeline;
47204720
/**
@@ -4928,7 +4928,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
49284928
/**
49294929
* Returns a HTMLCollection of the elements in the object on which the method was invoked (a document or an element) that have all the classes given by classNames. The classNames argument is interpreted as a space-separated list of classes.
49304930
*/
4931-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
4931+
getElementsByClassName(classNames: string): HTMLCollection;
49324932
/**
49334933
* Gets a collection of objects based on the value of the NAME or ID attribute.
49344934
* @param elementName Gets a collection of objects based on the value of the NAME or ID attribute.
@@ -4938,9 +4938,9 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
49384938
* Retrieves a collection of objects based on the specified element name.
49394939
* @param name Specifies the name of an element.
49404940
*/
4941-
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
4942-
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
4943-
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
4941+
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollection<HTMLElementTagNameMap[K]>;
4942+
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollection<SVGElementTagNameMap[K]>;
4943+
getElementsByTagName(qualifiedName: string): HTMLCollection;
49444944
/**
49454945
* If namespace and localName are "*" returns a HTMLCollection of all descendant elements.
49464946
*
@@ -4950,9 +4950,9 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
49504950
*
49514951
* Otherwise, returns a HTMLCollection of all descendant elements whose namespace is namespace and local name is localName.
49524952
*/
4953-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
4954-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
4955-
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollectionOf<Element>;
4953+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollection<HTMLElement>;
4954+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollection<SVGElement>;
4955+
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollection;
49564956
/**
49574957
* Returns an object representing the current selection of the document that is loaded into the object displaying a webpage.
49584958
*/
@@ -5307,13 +5307,13 @@ interface Element extends Node, Animatable, ChildNode, InnerHTML, NonDocumentTyp
53075307
/**
53085308
* Returns a HTMLCollection of the elements in the object on which the method was invoked (a document or an element) that have all the classes given by classNames. The classNames argument is interpreted as a space-separated list of classes.
53095309
*/
5310-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
5311-
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
5312-
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
5313-
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
5314-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
5315-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
5316-
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollectionOf<Element>;
5310+
getElementsByClassName(classNames: string): HTMLCollection;
5311+
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollection<HTMLElementTagNameMap[K]>;
5312+
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollection<SVGElementTagNameMap[K]>;
5313+
getElementsByTagName(qualifiedName: string): HTMLCollection;
5314+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollection<HTMLElement>;
5315+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollection<SVGElement>;
5316+
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollection;
53175317
/**
53185318
* Returns true if element has an attribute whose qualified name is qualifiedName, and false otherwise.
53195319
*/
@@ -6562,36 +6562,27 @@ declare var HTMLCanvasElement: {
65626562
};
65636563

65646564
/** A generic collection (array-like object similar to arguments) of elements (in document order) and offers methods and properties for selecting from the list. */
6565-
interface HTMLCollectionBase {
6565+
interface HTMLCollection<E extends Element = Element, N = never> {
65666566
/**
65676567
* Sets or retrieves the number of objects in a collection.
65686568
*/
65696569
readonly length: number;
65706570
/**
65716571
* Retrieves an object from various collections.
65726572
*/
6573-
item(index: number): Element | null;
6574-
[index: number]: Element;
6575-
}
6576-
6577-
interface HTMLCollection extends HTMLCollectionBase {
6573+
item(index: number): E | null;
65786574
/**
65796575
* Retrieves a select object or an object from an options collection.
65806576
*/
6581-
namedItem(name: string): Element | null;
6577+
namedItem(name: string): E | N | null;
6578+
[index: number]: E;
65826579
}
65836580

65846581
declare var HTMLCollection: {
65856582
prototype: HTMLCollection;
65866583
new(): HTMLCollection;
65876584
};
65886585

6589-
interface HTMLCollectionOf<T extends Element> extends HTMLCollectionBase {
6590-
item(index: number): T | null;
6591-
namedItem(name: string): T | null;
6592-
[index: number]: T;
6593-
}
6594-
65956586
/** Provides special properties (beyond those of the regular HTMLElement interface it also has available to it by inheritance) for manipulating definition list (<dl>) elements. */
65966587
interface HTMLDListElement extends HTMLElement {
65976588
/** @deprecated */
@@ -6623,7 +6614,7 @@ declare var HTMLDataElement: {
66236614

66246615
/** Provides special properties (beyond the HTMLElement object interface it also has available to it by inheritance) to manipulate <datalist> elements and their content. */
66256616
interface HTMLDataListElement extends HTMLElement {
6626-
readonly options: HTMLCollectionOf<HTMLOptionElement>;
6617+
readonly options: HTMLCollection<HTMLOptionElement>;
66276618
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDataListElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
66286619
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
66296620
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDataListElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
@@ -6843,7 +6834,7 @@ declare var HTMLFontElement: {
68436834
};
68446835

68456836
/** A collection of HTML form control elements. */
6846-
interface HTMLFormControlsCollection extends HTMLCollectionBase {
6837+
interface HTMLFormControlsCollection extends HTMLCollection<Element, RadioNodeList> {
68476838
/**
68486839
* Returns the item with ID or name name from the collection.
68496840
*
@@ -7606,7 +7597,7 @@ interface HTMLMapElement extends HTMLElement {
76067597
/**
76077598
* Retrieves a collection of the area objects defined for the given map object.
76087599
*/
7609-
readonly areas: HTMLCollection;
7600+
readonly areas: HTMLCollection<HTMLAreaElement>;
76107601
/**
76117602
* Sets or retrieves the name of the object.
76127603
*/
@@ -8147,7 +8138,7 @@ declare var HTMLOptionElement: {
81478138
};
81488139

81498140
/** 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. */
8150-
interface HTMLOptionsCollection extends HTMLCollectionOf<HTMLOptionElement> {
8141+
interface HTMLOptionsCollection extends HTMLCollection<HTMLOptionElement> {
81518142
/**
81528143
* Returns the number of elements in the collection.
81538144
*
@@ -8420,7 +8411,7 @@ interface HTMLSelectElement extends HTMLElement {
84208411
* Sets or retrieves the index of the selected option in a select object.
84218412
*/
84228413
selectedIndex: number;
8423-
readonly selectedOptions: HTMLCollectionOf<HTMLOptionElement>;
8414+
readonly selectedOptions: HTMLCollection<HTMLOptionElement>;
84248415
/**
84258416
* Sets or retrieves the number of rows in the list box.
84268417
*/
@@ -8736,7 +8727,7 @@ interface HTMLTableElement extends HTMLElement {
87368727
/**
87378728
* Sets or retrieves the number of horizontal rows contained in the object.
87388729
*/
8739-
readonly rows: HTMLCollectionOf<HTMLTableRowElement>;
8730+
readonly rows: HTMLCollection<HTMLTableRowElement>;
87408731
/**
87418732
* Sets or retrieves which dividing lines (inner borders) are displayed.
87428733
*/
@@ -8750,7 +8741,7 @@ interface HTMLTableElement extends HTMLElement {
87508741
/**
87518742
* Retrieves a collection of all tBody objects in the table. Objects in this collection are in source order.
87528743
*/
8753-
readonly tBodies: HTMLCollectionOf<HTMLTableSectionElement>;
8744+
readonly tBodies: HTMLCollection<HTMLTableSectionElement>;
87548745
/**
87558746
* Retrieves the tFoot object of the table.
87568747
*/
@@ -8838,7 +8829,7 @@ interface HTMLTableRowElement extends HTMLElement {
88388829
/**
88398830
* Retrieves a collection of all cells in the table row.
88408831
*/
8841-
readonly cells: HTMLCollectionOf<HTMLTableDataCellElement | HTMLTableHeaderCellElement>;
8832+
readonly cells: HTMLCollection<HTMLTableDataCellElement | HTMLTableHeaderCellElement>;
88428833
/** @deprecated */
88438834
ch: string;
88448835
/** @deprecated */
@@ -8888,7 +8879,7 @@ interface HTMLTableSectionElement extends HTMLElement {
88888879
/**
88898880
* Sets or retrieves the number of horizontal rows contained in the object.
88908881
*/
8891-
readonly rows: HTMLCollectionOf<HTMLTableRowElement>;
8882+
readonly rows: HTMLCollection<HTMLTableRowElement>;
88928883
/** @deprecated */
88938884
vAlign: string;
88948885
/**

baselines/dom.iterable.generated.d.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,10 @@ interface HTMLAllCollection {
8181
[Symbol.iterator](): IterableIterator<Element>;
8282
}
8383

84-
interface HTMLCollectionBase {
84+
interface HTMLCollection<E extends Element = Element, N = never> {
8585
[Symbol.iterator](): IterableIterator<Element>;
8686
}
8787

88-
interface HTMLCollectionOf<T extends Element> {
89-
[Symbol.iterator](): IterableIterator<T>;
90-
}
91-
9288
interface HTMLFormElement {
9389
[Symbol.iterator](): IterableIterator<Element>;
9490
}

inputfiles/addedTypes.json

+1-40
Original file line numberDiff line numberDiff line change
@@ -512,45 +512,6 @@
512512
]
513513
}
514514
},
515-
"HTMLCollectionOf": {
516-
"name": "HTMLCollectionOf",
517-
"type-parameters": [
518-
{
519-
"name": "T",
520-
"extends": "Element"
521-
}
522-
],
523-
"exposed": "Window",
524-
"extends": "HTMLCollection",
525-
"methods": {
526-
"method": {
527-
"item": {
528-
"getter": 1,
529-
"signature": [
530-
{
531-
"nullable": 1,
532-
"override-type": "T",
533-
"param": [
534-
{
535-
"name": "index",
536-
"type": "unsigned long"
537-
}
538-
]
539-
}
540-
],
541-
"specs": "html5",
542-
"name": "item"
543-
},
544-
"namedItem": {
545-
"name": "namedItem",
546-
"override-signatures": [
547-
"namedItem(name: string): T | null"
548-
]
549-
}
550-
}
551-
},
552-
"no-interface-object": "1"
553-
},
554515
"EventListenerObject": {
555516
"name": "EventListenerObject",
556517
"methods": {
@@ -608,7 +569,7 @@
608569
"getElementsByClassName": {
609570
"name": "getElementsByClassName",
610571
"override-signatures": [
611-
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
572+
"getElementsByClassName(classNames: string): HTMLCollection"
612573
]
613574
},
614575
"closest": {

0 commit comments

Comments
 (0)