Skip to content

Commit

Permalink
fix: make querySelector JSDOM compatible (#2265)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB authored Dec 6, 2023
1 parent 8babd50 commit a9b4ad6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
15 changes: 5 additions & 10 deletions src/components/card/card-action/card-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,11 @@ export class SbbCardAction extends LitElement implements LinkButtonProperties {
private _checkForSlottedActions(): void {
const cardFocusableAttributeName = 'data-card-focusable';

(
this._card.querySelectorAll?.(`[${cardFocusableAttributeName}]:not(${IS_FOCUSABLE_QUERY})`) ??
[]
).forEach((el: HTMLElement) => el.removeAttribute(cardFocusableAttributeName));

(
this._card.querySelectorAll?.(
`${IS_FOCUSABLE_QUERY}:not([${cardFocusableAttributeName}], sbb-card-action)`,
) ?? []
).forEach((el: HTMLElement) => el.setAttribute(cardFocusableAttributeName, ''));
Array.from(this._card.querySelectorAll?.(IS_FOCUSABLE_QUERY) ?? [])
.filter(
(el) => el.tagName !== 'SBB-CARD-ACTION' && !el.hasAttribute(cardFocusableAttributeName),
)
.forEach((el: HTMLElement) => el.setAttribute(cardFocusableAttributeName, ''));
}

protected override render(): TemplateResult {
Expand Down
13 changes: 12 additions & 1 deletion src/components/core/a11y/focus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { interactivityChecker } from './interactivity-checker';

export const IS_FOCUSABLE_QUERY = `:is(button, [href], input, select, textarea, details, summary:not(:disabled), [tabindex]):not([disabled]):not([tabindex="-1"])`;
export const IS_FOCUSABLE_QUERY = [
'button',
'[href]',
'input',
'select',
'textarea',
'details',
'summary:not(:disabled)',
'[tabindex]',
]
.map((selector) => `${selector}:not([disabled],[tabindex="-1"])`)
.join(',');

// Note: the use of this function for more complex scenarios (with many nested elements) may be expensive.
export function getFocusableElements(
Expand Down

0 comments on commit a9b4ad6

Please sign in to comment.