Skip to content

Commit 3596dd1

Browse files
uhyoclaude
andauthored
fix: change Node#parentElement to allow non-HTMLElement parents (#77)
* 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]> * ai: impove CLAUDE.md --------- Co-authored-by: Claude <[email protected]>
1 parent e310332 commit 3596dd1

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

CLAUDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ This project generates alternative TypeScript standard library definitions with
2121

2222
- **`TypeScript/`** - Git submodule containing the official TypeScript repository for source lib files
2323

24+
- Note: `TypeScript/lib/lib.dom.d.ts` and `TypeScript/lib/lib.es5.d.ts` are HUGE. Never try to read the whole file at once. Always grep for specific parts.
25+
2426
- **`tests/`** - Type-level tests using `tsd` to verify the improved type definitions work correctly
2527

2628
## Essential Commands

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)