Skip to content

Commit 6cc7715

Browse files
uhyoclaude
andcommitted
Fix Node.parentElement type from HTMLElement to Element
- Change Node.parentElement type from HTMLElement | null to Element | null - This fixes compatibility with SVG elements and other non-HTML elements - Add comprehensive tests to verify the fix works correctly - Resolves issue #74 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent e310332 commit 6cc7715

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

generated/lib.dom.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19836,7 +19836,7 @@ interface Node extends EventTarget {
1983619836
*
1983719837
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/parentElement)
1983819838
*/
19839-
readonly parentElement: HTMLElement | null;
19839+
readonly parentElement: Element | null;
1984019840
/**
1984119841
* Returns the parent.
1984219842
*
@@ -19941,6 +19941,12 @@ interface Node extends EventTarget {
1994119941
readonly DOCUMENT_POSITION_CONTAINED_BY: 0x10;
1994219942
readonly DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 0x20;
1994319943
}
19944+
// /**
19945+
// * Returns the parent element.
19946+
// *
19947+
// * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/parentElement)
19948+
// */
19949+
// readonly parentElement: HTMLElement | null;
1994419950

1994519951
declare var Node: {
1994619952
prototype: Node;

lib/lib.dom.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,12 @@ interface Document
255255
interface DocumentFragment extends Node, NonElementParentNode, ParentNode {
256256
getElementById(elementId: string): Element | null;
257257
}
258+
259+
interface Node extends EventTarget {
260+
/**
261+
* Returns the parent element.
262+
*
263+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/parentElement)
264+
*/
265+
readonly parentElement: Element | null;
266+
}

tests/src/dom.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,29 @@ const test = async (url: string) => {
161161
const svgElement: SVGElement = {} as SVGElement;
162162
const elementOrNull: typeof element = svgElement;
163163
}
164+
165+
// Node.parentElement
166+
{
167+
const div = document.createElement("div");
168+
const parent = div.parentElement;
169+
expectType<Element | null>(parent);
170+
171+
// Verify that the return type is not specifically HTMLElement
172+
expectNotType<HTMLElement | null>(parent);
173+
174+
// Verify that SVGElement can be assigned to parentElement (without type assertion)
175+
const svgElement: SVGElement = {} as SVGElement;
176+
const elementOrNull: typeof parent = svgElement;
177+
178+
// Test with SVG elements
179+
const svgElement2 = document.createElementNS("http://www.w3.org/2000/svg", "circle");
180+
const svgParent = svgElement2.parentElement;
181+
expectType<Element | null>(svgParent);
182+
183+
// Verify SVG elements work with parentElement
184+
if (svgParent) {
185+
expectType<Element>(svgParent);
186+
// Should be able to call methods available on Element
187+
svgParent.getAttribute("id");
188+
}
189+
}

0 commit comments

Comments
 (0)