Skip to content

Commit 06e84c3

Browse files
authored
fix(gather-internals): handle non-HTMLElement nodes (#5161)
Use `Node` instead of `HTMLElement` when checking the element internals prop value so we include all node types, such as SVGs. This is the follow up to a [similar problem in the `get-aria-value` pr](#5109 (comment)). I also added an eslint rule to prevent us from using `HTMLElements` in the future so we don't make the same mistake again.
1 parent e841a33 commit 06e84c3

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

eslint.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ module.exports = [
8484
'CallExpression[callee.object.property.name=actualNode][callee.property.name=contains]',
8585
message:
8686
"Don't use node.contains(node2) as it doesn't work across shadow DOM. Use axe.utils.contains(node, node2) instead."
87+
},
88+
{
89+
// window.HTMLElement
90+
selector:
91+
'MemberExpression[object.name=window][property.name=HTMLElement]',
92+
message:
93+
"Don't use window.HTMLElement as it doesn't include all DOM node types, such as SVGs. Use window.Node instead."
94+
},
95+
{
96+
// globalThis.HTMLElement
97+
selector:
98+
'MemberExpression[object.name=globalThis][property.name=HTMLElement]',
99+
message:
100+
"Don't use globalThis.HTMLElement as it doesn't include all DOM node types, such as SVGs. Use globalThis.Node instead."
87101
}
88102
]
89103
},

lib/gather-internals/walk-tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function walkTree(tree = document.body) {
3737

3838
// convert idref to node ancestry
3939
// aria props can use unattached nodes but those won't be used by the browser to calculate the prop value
40-
if (value instanceof globalThis.HTMLElement) {
40+
if (value instanceof globalThis.Node) {
4141
internals[prop] = value.isConnected
4242
? { type: 'HTMLElement', value: getAncestry(value) }
4343
: undefined;

0 commit comments

Comments
 (0)