diff --git a/src/label-helpers.ts b/src/label-helpers.ts index 4e1a3fed..edb4eacf 100644 --- a/src/label-helpers.ts +++ b/src/label-helpers.ts @@ -48,10 +48,29 @@ function getRealLabels(element: Element) { } function isLabelable(element: Element) { - return ( + if ( /BUTTON|METER|OUTPUT|PROGRESS|SELECT|TEXTAREA/.test(element.tagName) || (element.tagName === 'INPUT' && element.getAttribute('type') !== 'hidden') - ) + ) { + return true + } + + // Support form-associated custom elements + if ( + element.tagName.includes('-') && + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call + typeof (element as any).attachInternals === 'function' + ) { + try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call + const internals = (element as any).attachInternals() + return internals.formAssociated === true + } catch { + // Not a form-associated custom element or attachInternals not supported + } + } + + return false } function getLabels(