From 2d6e89645c2f138c7e085ab46bc8c870f29115bc Mon Sep 17 00:00:00 2001 From: jun-sheaf <34705014+jun-sheaf@users.noreply.github.com> Date: Wed, 22 Jul 2020 23:16:19 +0200 Subject: [PATCH 1/6] Replaces several DOM methods with weaker typings and closes microsoft#4689 The following changes have been made: * All occurences of `getElementById` takes an optional type parameter for manual typing. * All occurences of `getElementsByClassName` takes an optional type parameter for manual typing. * `parentElement` now types to `Element` (See microsoft#4689) --- baselines/dom.generated.d.ts | 16 ++++++++-------- inputfiles/addedTypes.json | 4 ++-- inputfiles/overridingTypes.json | 27 +++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index 81e514517..4624c221e 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -4769,16 +4769,16 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad * Returns a reference to the first object with the specified value of the ID or NAME attribute. * @param elementId String that specifies the ID value. Case-insensitive. */ - getElementById(elementId: string): HTMLElement | null; + getElementById(elementId: string): E | null; /** * 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; + getElementsByClassName(classNames: string): HTMLCollectionOf; /** * 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. */ - getElementsByName(elementName: string): NodeListOf; + getElementsByName(elementName: string): NodeListOf; /** * Retrieves a collection of objects based on the specified element name. * @param name Specifies the name of an element. @@ -4968,7 +4968,7 @@ interface DocumentEvent { /** A minimal document object that has no parent. It is used as a lightweight version of Document that stores a segment of a document structure comprised of nodes just like a standard document. The key difference is that because the document fragment isn't part of the active document tree structure, changes made to the fragment don't affect the document, cause reflow, or incur any performance impact that can occur when changes are made. */ interface DocumentFragment extends Node, NonElementParentNode, ParentNode { readonly ownerDocument: Document; - getElementById(elementId: string): HTMLElement | null; + getElementById(elementId: string): E | null; } declare var DocumentFragment: { @@ -5155,7 +5155,7 @@ 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; + getElementsByClassName(classNames: string): HTMLCollectionOf; getElementsByTagName(qualifiedName: K): HTMLCollectionOf; getElementsByTagName(qualifiedName: K): HTMLCollectionOf; getElementsByTagName(qualifiedName: string): HTMLCollectionOf; @@ -10772,7 +10772,7 @@ interface Node extends EventTarget { /** * Returns the parent element. */ - readonly parentElement: HTMLElement | null; + readonly parentElement: Element | null; /** * Returns the parent. */ @@ -11031,7 +11031,7 @@ interface NonElementParentNode { /** * Returns the first element within node's descendants whose ID is elementId. */ - getElementById(elementId: string): Element | null; + getElementById(elementId: string): E | null; } interface NotificationEventMap { @@ -14348,7 +14348,7 @@ interface SVGSVGElement extends SVGGraphicsElement, DocumentEvent, SVGFitToViewB forceRedraw(): void; getComputedStyle(elt: Element, pseudoElt?: string | null): CSSStyleDeclaration; getCurrentTime(): number; - getElementById(elementId: string): Element; + getElementById(elementId: string): E | null; getEnclosureList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf; getIntersectionList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf; pauseAnimations(): void; diff --git a/inputfiles/addedTypes.json b/inputfiles/addedTypes.json index ad831c4f4..477caecba 100644 --- a/inputfiles/addedTypes.json +++ b/inputfiles/addedTypes.json @@ -620,7 +620,7 @@ "getElementsByClassName": { "name": "getElementsByClassName", "override-signatures": [ - "getElementsByClassName(classNames: string): HTMLCollectionOf" + "getElementsByClassName(classNames: string): HTMLCollectionOf" ] }, "closest": { @@ -1022,7 +1022,7 @@ "name": "getElementById", "exposed": "Window", "override-signatures": [ - "getElementById(elementId: string): HTMLElement | null" + "getElementById(elementId: string): E | null" ] } } diff --git a/inputfiles/overridingTypes.json b/inputfiles/overridingTypes.json index dbcc6100f..e6f95e015 100644 --- a/inputfiles/overridingTypes.json +++ b/inputfiles/overridingTypes.json @@ -1,6 +1,19 @@ { "mixins": { "mixin": { + "NonElementParentNode": { + "name": "NonElementParentNode", + "methods": { + "method": { + "getElementById": { + "name": "getElementById", + "override-signatures": [ + "getElementById(elementId: string): E | null" + ] + } + } + } + }, "GlobalFetch": { "name": "GlobalFetch", "methods": { @@ -528,7 +541,7 @@ "getElementById": { "name": "getElementById", "override-signatures": [ - "getElementById(elementId: string): HTMLElement | null" + "getElementById(elementId: string): E | null" ] }, "elementsFromPoint": { @@ -548,13 +561,13 @@ "getElementsByClassName": { "name": "getElementsByClassName", "override-signatures": [ - "getElementsByClassName(classNames: string): HTMLCollectionOf" + "getElementsByClassName(classNames: string): HTMLCollectionOf" ] }, "getElementsByName": { "name": "getElementsByName", "override-signatures": [ - "getElementsByName(elementName: string): NodeListOf" + "getElementsByName(elementName: string): NodeListOf" ] }, "createTreeWalker": { @@ -704,7 +717,7 @@ "override-type": "Node & ParentNode | null" }, "parentElement": { - "override-type": "HTMLElement | null" + "override-type": "Element | null" }, "childNodes": { "override-type": "NodeListOf" @@ -967,6 +980,12 @@ "name": "SVGSVGElement", "methods": { "method": { + "getElementById": { + "name": "getElementById", + "override-signatures": [ + "getElementById(elementId: string): E | null" + ] + }, "getEnclosureList": { "name": "getEnclosureList", "override-signatures": [ From 546df069d5e0a8dd26f86dbba728648a9c3909bc Mon Sep 17 00:00:00 2001 From: Randolf C <34705014+jun-sheaf@users.noreply.github.com> Date: Sun, 18 Oct 2020 02:41:23 +0200 Subject: [PATCH 2/6] See https://github.com/microsoft/TypeScript/issues/4689#issuecomment-690503791 --- baselines/dom.generated.d.ts | 12 ++++++------ inputfiles/addedTypes.json | 4 ++-- inputfiles/overridingTypes.json | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index 4624c221e..59740d0f9 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -4769,11 +4769,11 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad * Returns a reference to the first object with the specified value of the ID or NAME attribute. * @param elementId String that specifies the ID value. Case-insensitive. */ - getElementById(elementId: string): E | null; + getElementById(elementId: string): E | null; /** * 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; + getElementsByClassName(classNames: string): HTMLCollectionOf; /** * 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. @@ -4968,7 +4968,7 @@ interface DocumentEvent { /** A minimal document object that has no parent. It is used as a lightweight version of Document that stores a segment of a document structure comprised of nodes just like a standard document. The key difference is that because the document fragment isn't part of the active document tree structure, changes made to the fragment don't affect the document, cause reflow, or incur any performance impact that can occur when changes are made. */ interface DocumentFragment extends Node, NonElementParentNode, ParentNode { readonly ownerDocument: Document; - getElementById(elementId: string): E | null; + getElementById(elementId: string): E | null; } declare var DocumentFragment: { @@ -5155,7 +5155,7 @@ 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; + getElementsByClassName(classNames: string): HTMLCollectionOf; getElementsByTagName(qualifiedName: K): HTMLCollectionOf; getElementsByTagName(qualifiedName: K): HTMLCollectionOf; getElementsByTagName(qualifiedName: string): HTMLCollectionOf; @@ -11031,7 +11031,7 @@ interface NonElementParentNode { /** * Returns the first element within node's descendants whose ID is elementId. */ - getElementById(elementId: string): E | null; + getElementById(elementId: string): E | null; } interface NotificationEventMap { @@ -14348,7 +14348,7 @@ interface SVGSVGElement extends SVGGraphicsElement, DocumentEvent, SVGFitToViewB forceRedraw(): void; getComputedStyle(elt: Element, pseudoElt?: string | null): CSSStyleDeclaration; getCurrentTime(): number; - getElementById(elementId: string): E | null; + getElementById(elementId: string): E | null; getEnclosureList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf; getIntersectionList(rect: SVGRect, referenceElement: SVGElement | null): NodeListOf; pauseAnimations(): void; diff --git a/inputfiles/addedTypes.json b/inputfiles/addedTypes.json index 477caecba..82e805629 100644 --- a/inputfiles/addedTypes.json +++ b/inputfiles/addedTypes.json @@ -620,7 +620,7 @@ "getElementsByClassName": { "name": "getElementsByClassName", "override-signatures": [ - "getElementsByClassName(classNames: string): HTMLCollectionOf" + "getElementsByClassName(classNames: string): HTMLCollectionOf" ] }, "closest": { @@ -1022,7 +1022,7 @@ "name": "getElementById", "exposed": "Window", "override-signatures": [ - "getElementById(elementId: string): E | null" + "getElementById(elementId: string): E | null" ] } } diff --git a/inputfiles/overridingTypes.json b/inputfiles/overridingTypes.json index e6f95e015..1a528e37b 100644 --- a/inputfiles/overridingTypes.json +++ b/inputfiles/overridingTypes.json @@ -8,7 +8,7 @@ "getElementById": { "name": "getElementById", "override-signatures": [ - "getElementById(elementId: string): E | null" + "getElementById(elementId: string): E | null" ] } } @@ -541,7 +541,7 @@ "getElementById": { "name": "getElementById", "override-signatures": [ - "getElementById(elementId: string): E | null" + "getElementById(elementId: string): E | null" ] }, "elementsFromPoint": { @@ -561,7 +561,7 @@ "getElementsByClassName": { "name": "getElementsByClassName", "override-signatures": [ - "getElementsByClassName(classNames: string): HTMLCollectionOf" + "getElementsByClassName(classNames: string): HTMLCollectionOf" ] }, "getElementsByName": { @@ -983,7 +983,7 @@ "getElementById": { "name": "getElementById", "override-signatures": [ - "getElementById(elementId: string): E | null" + "getElementById(elementId: string): E | null" ] }, "getEnclosureList": { From 6481d2f584fe6d500531456b303856a4299ae41a Mon Sep 17 00:00:00 2001 From: Randolf C <34705014+jun-sheaf@users.noreply.github.com> Date: Sun, 18 Oct 2020 15:53:35 +0200 Subject: [PATCH 3/6] See https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/885#issuecomment-711124551 --- baselines/dom.generated.d.ts | 4 ++++ inputfiles/idl/DOM.widl | 2 +- inputfiles/overridingTypes.json | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index 2d227e122..ac54ecb15 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -6615,6 +6615,7 @@ interface HTMLElement extends Element, DocumentAndElementEventHandlers, ElementC readonly offsetParent: Element | null; readonly offsetTop: number; readonly offsetWidth: number; + readonly parentElement: HTMLElement | undefined; spellcheck: boolean; title: string; translate: boolean; @@ -13001,7 +13002,9 @@ interface SVGElement extends Element, DocumentAndElementEventHandlers, DocumentA /** @deprecated */ readonly className: any; readonly ownerSVGElement: SVGSVGElement | null; + readonly parentElement: SVGElement | null; readonly viewportElement: SVGElement | null; + getElementsByClassName(classNames: string): HTMLCollectionOf; addEventListener(type: K, listener: (this: SVGElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: SVGElement, ev: SVGElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void; @@ -13589,6 +13592,7 @@ interface SVGForeignObjectElement extends SVGGraphicsElement { readonly width: SVGAnimatedLength; readonly x: SVGAnimatedLength; readonly y: SVGAnimatedLength; + getElementsByClassName(classNames: string): HTMLCollectionOf; addEventListener(type: K, listener: (this: SVGForeignObjectElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: SVGForeignObjectElement, ev: SVGElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/inputfiles/idl/DOM.widl b/inputfiles/idl/DOM.widl index 0313de518..b96848ee5 100644 --- a/inputfiles/idl/DOM.widl +++ b/inputfiles/idl/DOM.widl @@ -214,7 +214,7 @@ interface Node : EventTarget { readonly attribute Document? ownerDocument; Node getRootNode(optional GetRootNodeOptions options = {}); readonly attribute Node? parentNode; - readonly attribute Element? parentElement; + readonly attribute HTMLElement? parentElement; boolean hasChildNodes(); [SameObject] readonly attribute NodeList childNodes; readonly attribute Node? firstChild; diff --git a/inputfiles/overridingTypes.json b/inputfiles/overridingTypes.json index f9a81f5db..2384603da 100644 --- a/inputfiles/overridingTypes.json +++ b/inputfiles/overridingTypes.json @@ -510,6 +510,18 @@ }, "override-index-signatures": [] }, + "HTMLElement": { + "name": "HTMLElement", + "properties": { + "property": { + "parentElement": { + "name": "parentElement", + "read-only": 1, + "override-type": "HTMLElement | undefined" + } + } + } + }, "Document": { "name": "Document", "methods": { @@ -909,6 +921,21 @@ "deprecated": 1, "name": "className", "type": "any" + }, + "parentElement": { + "name": "parentElement", + "read-only": 1, + "override-type": "SVGElement | null" + } + } + }, + "methods": { + "method": { + "getElementsByClassName": { + "name": "getElementsByClassName", + "override-signatures": [ + "getElementsByClassName(classNames: string): HTMLCollectionOf" + ] } } } @@ -954,6 +981,19 @@ } } }, + "SVGForeignObjectElement": { + "name": "SVGForeignObjectElement", + "methods": { + "method": { + "getElementsByClassName": { + "name": "getElementsByClassName", + "override-signatures": [ + "getElementsByClassName(classNames: string): HTMLCollectionOf" + ] + } + } + } + }, "FormData": { "name": "FormData", "methods": { From 260912152a7cfd91d912759b402cb5d1101a49e6 Mon Sep 17 00:00:00 2001 From: Randolf C <34705014+jun-sheaf@users.noreply.github.com> Date: Sun, 18 Oct 2020 16:11:24 +0200 Subject: [PATCH 4/6] Replaced undefined with null --- baselines/dom.generated.d.ts | 2 +- inputfiles/overridingTypes.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index ac54ecb15..cec22bb63 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -6615,7 +6615,7 @@ interface HTMLElement extends Element, DocumentAndElementEventHandlers, ElementC readonly offsetParent: Element | null; readonly offsetTop: number; readonly offsetWidth: number; - readonly parentElement: HTMLElement | undefined; + readonly parentElement: HTMLElement | null; spellcheck: boolean; title: string; translate: boolean; diff --git a/inputfiles/overridingTypes.json b/inputfiles/overridingTypes.json index 2384603da..ec8f505e2 100644 --- a/inputfiles/overridingTypes.json +++ b/inputfiles/overridingTypes.json @@ -517,7 +517,7 @@ "parentElement": { "name": "parentElement", "read-only": 1, - "override-type": "HTMLElement | undefined" + "override-type": "HTMLElement | null" } } } From 11a7dd08b1e031aba85c3763fac94bbc1874975d Mon Sep 17 00:00:00 2001 From: Randolf C <34705014+jun-sheaf@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:37:43 +0200 Subject: [PATCH 5/6] Update DOM.widl --- inputfiles/idl/DOM.widl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inputfiles/idl/DOM.widl b/inputfiles/idl/DOM.widl index b96848ee5..0313de518 100644 --- a/inputfiles/idl/DOM.widl +++ b/inputfiles/idl/DOM.widl @@ -214,7 +214,7 @@ interface Node : EventTarget { readonly attribute Document? ownerDocument; Node getRootNode(optional GetRootNodeOptions options = {}); readonly attribute Node? parentNode; - readonly attribute HTMLElement? parentElement; + readonly attribute Element? parentElement; boolean hasChildNodes(); [SameObject] readonly attribute NodeList childNodes; readonly attribute Node? firstChild; From 39e338ef3cced02f48ebea181830848a7dba6cfe Mon Sep 17 00:00:00 2001 From: Randolf C <34705014+jun-sheaf@users.noreply.github.com> Date: Fri, 11 Dec 2020 20:10:42 +0100 Subject: [PATCH 6/6] See https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/885#discussion_r541143034_ --- baselines/dom.generated.d.ts | 2 +- inputfiles/overridingTypes.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index cec22bb63..a9d361ea7 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -4770,7 +4770,7 @@ interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShad * 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. */ - getElementsByName(elementName: string): NodeListOf; + getElementsByName(elementName: string): NodeListOf; /** * Retrieves a collection of objects based on the specified element name. * @param name Specifies the name of an element. diff --git a/inputfiles/overridingTypes.json b/inputfiles/overridingTypes.json index ec8f505e2..0cb7e6e95 100644 --- a/inputfiles/overridingTypes.json +++ b/inputfiles/overridingTypes.json @@ -567,7 +567,7 @@ "getElementsByName": { "name": "getElementsByName", "override-signatures": [ - "getElementsByName(elementName: string): NodeListOf" + "getElementsByName(elementName: string): NodeListOf" ] }, "createTreeWalker": {