Skip to content

Simplify / fix HTMLCollection overrides #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 37 additions & 44 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4562,12 +4562,12 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
* Retrieves a collection of all a objects that have a name and/or id property. Objects in this collection are in HTML source order.
*/
/** @deprecated */
readonly anchors: HTMLCollectionOf<HTMLAnchorElement>;
readonly anchors: HTMLCollection<HTMLAnchorElement>;
/**
* Retrieves a collection of all applet objects in the document.
*/
/** @deprecated */
readonly applets: HTMLCollectionOf<HTMLAppletElement>;
readonly applets: HTMLCollection<HTMLAppletElement>;
/**
* Deprecated. Sets or retrieves a value that indicates the background color behind the object.
*/
Expand Down Expand Up @@ -4635,7 +4635,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
/**
* Retrieves a collection of all embed objects in the document.
*/
readonly embeds: HTMLCollectionOf<HTMLEmbedElement>;
readonly embeds: HTMLCollection<HTMLEmbedElement>;
/**
* Sets or gets the foreground (text) color of the document.
*/
Expand All @@ -4644,7 +4644,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
/**
* Retrieves a collection, in source order, of all form objects in the document.
*/
readonly forms: HTMLCollectionOf<HTMLFormElement>;
readonly forms: HTMLCollection<HTMLFormElement>;
/** @deprecated */
readonly fullscreen: boolean;
/**
Expand All @@ -4659,7 +4659,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
/**
* Retrieves a collection, in source order, of img objects in the document.
*/
readonly images: HTMLCollectionOf<HTMLImageElement>;
readonly images: HTMLCollection<HTMLImageElement>;
/**
* Gets the implementation object of the current document.
*/
Expand All @@ -4680,7 +4680,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
/**
* Retrieves a collection of all a objects that specify the href property and all area objects in the document.
*/
readonly links: HTMLCollectionOf<HTMLAnchorElement | HTMLAreaElement>;
readonly links: HTMLCollection<HTMLAnchorElement | HTMLAreaElement>;
/**
* Contains information about the current URL.
*/
Expand All @@ -4702,7 +4702,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
/**
* Return an HTMLCollection of the embed elements in the Document.
*/
readonly plugins: HTMLCollectionOf<HTMLEmbedElement>;
readonly plugins: HTMLCollection<HTMLEmbedElement>;
/**
* Retrieves a value that indicates the current state of the object.
*/
Expand All @@ -4714,7 +4714,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
/**
* Retrieves a collection of all script objects in the document.
*/
readonly scripts: HTMLCollectionOf<HTMLScriptElement>;
readonly scripts: HTMLCollection<HTMLScriptElement>;
readonly scrollingElement: Element | null;
readonly timeline: DocumentTimeline;
/**
Expand Down Expand Up @@ -4928,7 +4928,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
/**
* 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.
*/
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
getElementsByClassName(classNames: string): HTMLCollection;
/**
* Gets a collection of objects based on the value of the NAME or ID attribute.
* @param elementName Gets a collection of objects based on the value of the NAME or ID attribute.
Expand All @@ -4938,9 +4938,9 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
* Retrieves a collection of objects based on the specified element name.
* @param name Specifies the name of an element.
*/
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollection<HTMLElementTagNameMap[K]>;
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollection<SVGElementTagNameMap[K]>;
getElementsByTagName(qualifiedName: string): HTMLCollection;
/**
* If namespace and localName are "*" returns a HTMLCollection of all descendant elements.
*
Expand All @@ -4950,9 +4950,9 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad
*
* Otherwise, returns a HTMLCollection of all descendant elements whose namespace is namespace and local name is localName.
*/
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollectionOf<Element>;
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollection<HTMLElement>;
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollection<SVGElement>;
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollection;
/**
* Returns an object representing the current selection of the document that is loaded into the object displaying a webpage.
*/
Expand Down Expand Up @@ -5307,13 +5307,13 @@ interface Element extends Node, Animatable, ChildNode, InnerHTML, NonDocumentTyp
/**
* 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.
*/
getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<HTMLElementTagNameMap[K]>;
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollectionOf<SVGElementTagNameMap[K]>;
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollectionOf<Element>;
getElementsByClassName(classNames: string): HTMLCollection;
getElementsByTagName<K extends keyof HTMLElementTagNameMap>(qualifiedName: K): HTMLCollection<HTMLElementTagNameMap[K]>;
getElementsByTagName<K extends keyof SVGElementTagNameMap>(qualifiedName: K): HTMLCollection<SVGElementTagNameMap[K]>;
getElementsByTagName(qualifiedName: string): HTMLCollection;
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollection<HTMLElement>;
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollection<SVGElement>;
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollection;
/**
* Returns true if element has an attribute whose qualified name is qualifiedName, and false otherwise.
*/
Expand Down Expand Up @@ -6562,36 +6562,27 @@ declare var HTMLCanvasElement: {
};

/** A generic collection (array-like object similar to arguments) of elements (in document order) and offers methods and properties for selecting from the list. */
interface HTMLCollectionBase {
interface HTMLCollection<E extends Element = Element, N = never> {
/**
* Sets or retrieves the number of objects in a collection.
*/
readonly length: number;
/**
* Retrieves an object from various collections.
*/
item(index: number): Element | null;
[index: number]: Element;
}

interface HTMLCollection extends HTMLCollectionBase {
item(index: number): E | null;
/**
* Retrieves a select object or an object from an options collection.
*/
namedItem(name: string): Element | null;
namedItem(name: string): E | N | null;
[index: number]: E;
}

declare var HTMLCollection: {
prototype: HTMLCollection;
new(): HTMLCollection;
};

interface HTMLCollectionOf<T extends Element> extends HTMLCollectionBase {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I and probably other people have used it in own code (API which should usable with getElementsByTagNameNS output for example.
So this interface should probably survive with a @deprecated tag.

Copy link
Contributor

@saschanaz saschanaz Dec 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface name also is consistent with NodeListOf.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a deprecated HTMLCollectionOf alias for backwards compatibility. @saschanaz should I maybe do the same for NodeListOf then for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping @saschanaz.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion here, maybe @sandersn ?

item(index: number): T | null;
namedItem(name: string): T | null;
[index: number]: T;
}

/** Provides special properties (beyond those of the regular HTMLElement interface it also has available to it by inheritance) for manipulating definition list (<dl>) elements. */
interface HTMLDListElement extends HTMLElement {
/** @deprecated */
Expand Down Expand Up @@ -6623,7 +6614,7 @@ declare var HTMLDataElement: {

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

/** A collection of HTML form control elements. */
interface HTMLFormControlsCollection extends HTMLCollectionBase {
interface HTMLFormControlsCollection extends HTMLCollection<Element, RadioNodeList> {
/**
* Returns the item with ID or name name from the collection.
*
Expand Down Expand Up @@ -7606,7 +7597,7 @@ interface HTMLMapElement extends HTMLElement {
/**
* Retrieves a collection of the area objects defined for the given map object.
*/
readonly areas: HTMLCollection;
readonly areas: HTMLCollection<HTMLAreaElement>;
/**
* Sets or retrieves the name of the object.
*/
Expand Down Expand Up @@ -8147,7 +8138,7 @@ declare var HTMLOptionElement: {
};

/** 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. */
interface HTMLOptionsCollection extends HTMLCollectionOf<HTMLOptionElement> {
interface HTMLOptionsCollection extends HTMLCollection<HTMLOptionElement> {
/**
* Returns the number of elements in the collection.
*
Expand Down Expand Up @@ -8420,7 +8411,7 @@ interface HTMLSelectElement extends HTMLElement {
* Sets or retrieves the index of the selected option in a select object.
*/
selectedIndex: number;
readonly selectedOptions: HTMLCollectionOf<HTMLOptionElement>;
readonly selectedOptions: HTMLCollection<HTMLOptionElement>;
/**
* Sets or retrieves the number of rows in the list box.
*/
Expand Down Expand Up @@ -8736,7 +8727,7 @@ interface HTMLTableElement extends HTMLElement {
/**
* Sets or retrieves the number of horizontal rows contained in the object.
*/
readonly rows: HTMLCollectionOf<HTMLTableRowElement>;
readonly rows: HTMLCollection<HTMLTableRowElement>;
/**
* Sets or retrieves which dividing lines (inner borders) are displayed.
*/
Expand All @@ -8750,7 +8741,7 @@ interface HTMLTableElement extends HTMLElement {
/**
* Retrieves a collection of all tBody objects in the table. Objects in this collection are in source order.
*/
readonly tBodies: HTMLCollectionOf<HTMLTableSectionElement>;
readonly tBodies: HTMLCollection<HTMLTableSectionElement>;
/**
* Retrieves the tFoot object of the table.
*/
Expand Down Expand Up @@ -8838,7 +8829,7 @@ interface HTMLTableRowElement extends HTMLElement {
/**
* Retrieves a collection of all cells in the table row.
*/
readonly cells: HTMLCollectionOf<HTMLTableDataCellElement | HTMLTableHeaderCellElement>;
readonly cells: HTMLCollection<HTMLTableDataCellElement | HTMLTableHeaderCellElement>;
/** @deprecated */
ch: string;
/** @deprecated */
Expand Down Expand Up @@ -8888,7 +8879,7 @@ interface HTMLTableSectionElement extends HTMLElement {
/**
* Sets or retrieves the number of horizontal rows contained in the object.
*/
readonly rows: HTMLCollectionOf<HTMLTableRowElement>;
readonly rows: HTMLCollection<HTMLTableRowElement>;
/** @deprecated */
vAlign: string;
/**
Expand Down Expand Up @@ -20071,6 +20062,8 @@ type RTCTransport = RTCDtlsTransport | RTCSrtpSdesTransport;
/** @deprecated */
type MouseWheelEvent = WheelEvent;
type WindowProxy = Window;
/** @deprecated */
type HTMLCollectionOf<T extends Element> = HTMLCollection<T>;
type AlignSetting = "center" | "end" | "left" | "right" | "start";
type AnimationPlayState = "finished" | "idle" | "paused" | "running";
type AppendMode = "segments" | "sequence";
Expand Down
6 changes: 1 addition & 5 deletions baselines/dom.iterable.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@ interface HTMLAllCollection {
[Symbol.iterator](): IterableIterator<Element>;
}

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

interface HTMLCollectionOf<T extends Element> {
[Symbol.iterator](): IterableIterator<T>;
}

interface HTMLFormElement {
[Symbol.iterator](): IterableIterator<Element>;
}
Expand Down
52 changes: 12 additions & 40 deletions inputfiles/addedTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,45 +512,6 @@
]
}
},
"HTMLCollectionOf": {
"name": "HTMLCollectionOf",
"type-parameters": [
{
"name": "T",
"extends": "Element"
}
],
"exposed": "Window",
"extends": "HTMLCollection",
"methods": {
"method": {
"item": {
"getter": 1,
"signature": [
{
"nullable": 1,
"override-type": "T",
"param": [
{
"name": "index",
"type": "unsigned long"
}
]
}
],
"specs": "html5",
"name": "item"
},
"namedItem": {
"name": "namedItem",
"override-signatures": [
"namedItem(name: string): T | null"
]
}
}
},
"no-interface-object": "1"
},
"EventListenerObject": {
"name": "EventListenerObject",
"methods": {
Expand Down Expand Up @@ -608,7 +569,7 @@
"getElementsByClassName": {
"name": "getElementsByClassName",
"override-signatures": [
"getElementsByClassName(classNames: string): HTMLCollectionOf<Element>"
"getElementsByClassName(classNames: string): HTMLCollection"
]
},
"closest": {
Expand Down Expand Up @@ -2377,6 +2338,17 @@
"type": "ModuleImports"
}
]
},
{
"deprecated": 1,
"new-type": "HTMLCollectionOf",
"type-parameters": [
{
"name": "T",
"extends": "Element"
}
],
"override-type": "HTMLCollection<T>"
}
]
}
Expand Down
1 change: 1 addition & 0 deletions inputfiles/knownTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"HmacImportParams",
"HmacKeyAlgorithm",
"HmacKeyGenParams",
"HTMLCollectionOf",
"IDBKeyPath",
"IDBValidKey",
"ImageBitmapRenderingContextSettings",
Expand Down
Loading