Skip to content

Commit 550a6a7

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 4d745be commit 550a6a7

File tree

5 files changed

+136
-189
lines changed

5 files changed

+136
-189
lines changed

baselines/dom.generated.d.ts

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,12 +4522,12 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
45224522
* Retrieves a collection of all a objects that have a name and/or id property. Objects in this collection are in HTML source order.
45234523
*/
45244524
/** @deprecated */
4525-
readonly anchors: HTMLCollectionOf<HTMLAnchorElement>;
4525+
readonly anchors: HTMLCollection<HTMLAnchorElement>;
45264526
/**
45274527
* Retrieves a collection of all applet objects in the document.
45284528
*/
45294529
/** @deprecated */
4530-
readonly applets: HTMLCollectionOf<HTMLAppletElement>;
4530+
readonly applets: HTMLCollection<HTMLAppletElement>;
45314531
/**
45324532
* Deprecated. Sets or retrieves a value that indicates the background color behind the object.
45334533
*/
@@ -4595,7 +4595,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
45954595
/**
45964596
* Retrieves a collection of all embed objects in the document.
45974597
*/
4598-
readonly embeds: HTMLCollectionOf<HTMLEmbedElement>;
4598+
readonly embeds: HTMLCollection<HTMLEmbedElement>;
45994599
/**
46004600
* Sets or gets the foreground (text) color of the document.
46014601
*/
@@ -4604,7 +4604,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46044604
/**
46054605
* Retrieves a collection, in source order, of all form objects in the document.
46064606
*/
4607-
readonly forms: HTMLCollectionOf<HTMLFormElement>;
4607+
readonly forms: HTMLCollection<HTMLFormElement>;
46084608
/** @deprecated */
46094609
readonly fullscreen: boolean;
46104610
/**
@@ -4619,7 +4619,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46194619
/**
46204620
* Retrieves a collection, in source order, of img objects in the document.
46214621
*/
4622-
readonly images: HTMLCollectionOf<HTMLImageElement>;
4622+
readonly images: HTMLCollection<HTMLImageElement>;
46234623
/**
46244624
* Gets the implementation object of the current document.
46254625
*/
@@ -4640,7 +4640,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46404640
/**
46414641
* Retrieves a collection of all a objects that specify the href property and all area objects in the document.
46424642
*/
4643-
readonly links: HTMLCollectionOf<HTMLAnchorElement | HTMLAreaElement>;
4643+
readonly links: HTMLCollection<HTMLAnchorElement | HTMLAreaElement>;
46444644
/**
46454645
* Contains information about the current URL.
46464646
*/
@@ -4662,7 +4662,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46624662
/**
46634663
* Return an HTMLCollection of the embed elements in the Document.
46644664
*/
4665-
readonly plugins: HTMLCollectionOf<HTMLEmbedElement>;
4665+
readonly plugins: HTMLCollection<HTMLEmbedElement>;
46664666
/**
46674667
* Retrieves a value that indicates the current state of the object.
46684668
*/
@@ -4674,7 +4674,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
46744674
/**
46754675
* Retrieves a collection of all script objects in the document.
46764676
*/
4677-
readonly scripts: HTMLCollectionOf<HTMLScriptElement>;
4677+
readonly scripts: HTMLCollection<HTMLScriptElement>;
46784678
readonly scrollingElement: Element | null;
46794679
readonly timeline: DocumentTimeline;
46804680
/**
@@ -4889,7 +4889,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
48894889
/**
48904890
* 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.
48914891
*/
4892-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
4892+
getElementsByClassName(classNames: string): HTMLCollection;
48934893
/**
48944894
* Gets a collection of objects based on the value of the NAME or ID attribute.
48954895
* @param elementName Gets a collection of objects based on the value of the NAME or ID attribute.
@@ -4899,9 +4899,9 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
48994899
* Retrieves a collection of objects based on the specified element name.
49004900
* @param name Specifies the name of an element.
49014901
*/
4902-
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
4903-
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
4904-
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
4902+
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollection<HTMLElementTagNameMap[K]>;
4903+
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollection<SVGElementTagNameMap[K]>;
4904+
getElementsByTagName(qualifiedName: string): HTMLCollection;
49054905
/**
49064906
* If namespace and localName are "*" returns a HTMLCollection of all descendant elements.
49074907
*
@@ -4911,9 +4911,9 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
49114911
*
49124912
* Otherwise, returns a HTMLCollection of all descendant elements whose namespace is namespace and local name is localName.
49134913
*/
4914-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
4915-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
4916-
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollectionOf<Element>;
4914+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollection<HTMLElement>;
4915+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollection<SVGElement>;
4916+
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollection;
49174917
/**
49184918
* Returns an object representing the current selection of the document that is loaded into the object displaying a webpage.
49194919
*/
@@ -5269,13 +5269,13 @@ interface Element extends Node, Animatable, ChildNode, InnerHTML, NonDocumentTyp
52695269
/**
52705270
* 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.
52715271
*/
5272-
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
5273-
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
5274-
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
5275-
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
5276-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
5277-
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
5278-
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollectionOf<Element>;
5272+
getElementsByClassName(classNames: string): HTMLCollection;
5273+
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollection<HTMLElementTagNameMap[K]>;
5274+
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollection<SVGElementTagNameMap[K]>;
5275+
getElementsByTagName(qualifiedName: string): HTMLCollection;
5276+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollection<HTMLElement>;
5277+
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollection<SVGElement>;
5278+
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollection;
52795279
/**
52805280
* Returns true if element has an attribute whose qualified name is qualifiedName, and false otherwise.
52815281
*/
@@ -6533,36 +6533,27 @@ declare var HTMLCanvasElement: {
65336533
};
65346534

65356535
/** A generic collection (array-like object similar to arguments) of elements (in document order) and offers methods and properties for selecting from the list. */
6536-
interface HTMLCollectionBase {
6536+
interface HTMLCollection<E extends Element = Element, N = never> {
65376537
/**
65386538
* Sets or retrieves the number of objects in a collection.
65396539
*/
65406540
readonly length: number;
65416541
/**
65426542
* Retrieves an object from various collections.
65436543
*/
6544-
item(index: number): Element | null;
6545-
[index: number]: Element;
6546-
}
6547-
6548-
interface HTMLCollection extends HTMLCollectionBase {
6544+
item(index: number): E | null;
65496545
/**
65506546
* Retrieves a select object or an object from an options collection.
65516547
*/
6552-
namedItem(name: string): Element | null;
6548+
namedItem(name: string): E | N | null;
6549+
[index: number]: E;
65536550
}
65546551

65556552
declare var HTMLCollection: {
65566553
prototype: HTMLCollection;
65576554
new(): HTMLCollection;
65586555
};
65596556

6560-
interface HTMLCollectionOf<T extends Element> extends HTMLCollectionBase {
6561-
item(index: number): T | null;
6562-
namedItem(name: string): T | null;
6563-
[index: number]: T;
6564-
}
6565-
65666557
/** Provides special properties (beyond those of the regular HTMLElement interface it also has available to it by inheritance) for manipulating definition list (<dl>) elements. */
65676558
interface HTMLDListElement extends HTMLElement {
65686559
/** @deprecated */
@@ -6594,7 +6585,7 @@ declare var HTMLDataElement: {
65946585

65956586
/** Provides special properties (beyond the HTMLElement object interface it also has available to it by inheritance) to manipulate <datalist> elements and their content. */
65966587
interface HTMLDataListElement extends HTMLElement {
6597-
readonly options: HTMLCollectionOf<HTMLOptionElement>;
6588+
readonly options: HTMLCollection<HTMLOptionElement>;
65986589
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDataListElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
65996590
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
66006591
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDataListElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
@@ -6814,7 +6805,7 @@ declare var HTMLFontElement: {
68146805
};
68156806

68166807
/** A collection of HTML form control elements. */
6817-
interface HTMLFormControlsCollection extends HTMLCollectionBase {
6808+
interface HTMLFormControlsCollection extends HTMLCollection<Element, RadioNodeList> {
68186809
/**
68196810
* Returns the item with ID or name name from the collection.
68206811
*
@@ -7581,7 +7572,7 @@ interface HTMLMapElement extends HTMLElement {
75817572
/**
75827573
* Retrieves a collection of the area objects defined for the given map object.
75837574
*/
7584-
readonly areas: HTMLCollection;
7575+
readonly areas: HTMLCollection<HTMLAreaElement>;
75857576
/**
75867577
* Sets or retrieves the name of the object.
75877578
*/
@@ -8122,7 +8113,7 @@ declare var HTMLOptionElement: {
81228113
};
81238114

81248115
/** 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. */
8125-
interface HTMLOptionsCollection extends HTMLCollectionOf<HTMLOptionElement> {
8116+
interface HTMLOptionsCollection extends HTMLCollection<HTMLOptionElement> {
81268117
/**
81278118
* Returns the number of elements in the collection.
81288119
*
@@ -8398,7 +8389,7 @@ interface HTMLSelectElement extends HTMLElement {
83988389
* Sets or retrieves the index of the selected option in a select object.
83998390
*/
84008391
selectedIndex: number;
8401-
readonly selectedOptions: HTMLCollectionOf<HTMLOptionElement>;
8392+
readonly selectedOptions: HTMLCollection<HTMLOptionElement>;
84028393
/**
84038394
* Sets or retrieves the number of rows in the list box.
84048395
*/
@@ -8714,7 +8705,7 @@ interface HTMLTableElement extends HTMLElement {
87148705
/**
87158706
* Sets or retrieves the number of horizontal rows contained in the object.
87168707
*/
8717-
readonly rows: HTMLCollectionOf<HTMLTableRowElement>;
8708+
readonly rows: HTMLCollection<HTMLTableRowElement>;
87188709
/**
87198710
* Sets or retrieves which dividing lines (inner borders) are displayed.
87208711
*/
@@ -8728,7 +8719,7 @@ interface HTMLTableElement extends HTMLElement {
87288719
/**
87298720
* Retrieves a collection of all tBody objects in the table. Objects in this collection are in source order.
87308721
*/
8731-
readonly tBodies: HTMLCollectionOf<HTMLTableSectionElement>;
8722+
readonly tBodies: HTMLCollection<HTMLTableSectionElement>;
87328723
/**
87338724
* Retrieves the tFoot object of the table.
87348725
*/
@@ -8816,7 +8807,7 @@ interface HTMLTableRowElement extends HTMLElement {
88168807
/**
88178808
* Retrieves a collection of all cells in the table row.
88188809
*/
8819-
readonly cells: HTMLCollectionOf<HTMLTableDataCellElement | HTMLTableHeaderCellElement>;
8810+
readonly cells: HTMLCollection<HTMLTableDataCellElement | HTMLTableHeaderCellElement>;
88208811
/** @deprecated */
88218812
ch: string;
88228813
/** @deprecated */
@@ -8866,7 +8857,7 @@ interface HTMLTableSectionElement extends HTMLElement {
88668857
/**
88678858
* Sets or retrieves the number of horizontal rows contained in the object.
88688859
*/
8869-
readonly rows: HTMLCollectionOf<HTMLTableRowElement>;
8860+
readonly rows: HTMLCollection<HTMLTableRowElement>;
88708861
/** @deprecated */
88718862
vAlign: string;
88728863
/**

baselines/dom.iterable.generated.d.ts

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -619,45 +619,6 @@
619619
]
620620
}
621621
},
622-
"HTMLCollectionOf": {
623-
"name": "HTMLCollectionOf",
624-
"type-parameters": [
625-
{
626-
"name": "T",
627-
"extends": "Element"
628-
}
629-
],
630-
"exposed": "Window",
631-
"extends": "HTMLCollection",
632-
"methods": {
633-
"method": {
634-
"item": {
635-
"getter": 1,
636-
"signature": [
637-
{
638-
"nullable": 1,
639-
"override-type": "T",
640-
"param": [
641-
{
642-
"name": "index",
643-
"type": "unsigned long"
644-
}
645-
]
646-
}
647-
],
648-
"specs": "html5",
649-
"name": "item"
650-
},
651-
"namedItem": {
652-
"name": "namedItem",
653-
"override-signatures": [
654-
"namedItem(name: string): T | null"
655-
]
656-
}
657-
}
658-
},
659-
"no-interface-object": "1"
660-
},
661622
"EventListenerObject": {
662623
"name": "EventListenerObject",
663624
"methods": {
@@ -715,7 +676,7 @@
715676
"getElementsByClassName": {
716677
"name": "getElementsByClassName",
717678
"override-signatures": [
718-
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
679+
"getElementsByClassName(classNames: string): HTMLCollection"
719680
]
720681
},
721682
"closest": {

0 commit comments

Comments
 (0)