Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,9 @@ export class Button implements ComponentInterface, AnchorInterface, ButtonInterf
* The ARIA state has to stay live, since `ion-input-password-toggle` rewrites
* `aria-label` and `aria-pressed` on its `ion-button` on every toggle. We keep
* `aria-disabled` out of the watch because the `<Host>` below renders it from the
* `disabled` prop and those writes would clobber a developer's value, and `role` out
* because a post-load write stays on the host too, which would put the same role on
* two elements in the accessibility tree.
* `disabled` prop and those writes would clobber a developer's value.
*/
this.ariaController = createAriaAttributeController(this.el, () => forceUpdate(this), ['aria-disabled', 'role']);
this.ariaController = createAriaAttributeController(this.el, () => forceUpdate(this), ['aria-disabled']);
this.tabindexController = createTabindexController(this.el, () => forceUpdate(this));
}

Expand Down
72 changes: 60 additions & 12 deletions core/src/components/button/test/a11y/button.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,18 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
await expect(nativeButton).toHaveAttribute('aria-disabled', 'true');
});

test('should not sync role from the host', async ({ page }) => {
test('should sync role from the host', async ({ page }) => {
await page.setContent(`<ion-button role="switch">Button</ion-button>`, config);

const host = page.locator('ion-button');
const nativeButton = host.locator('button');

// The initial copy moves role onto the native button, as it always has.
await expect(nativeButton).toHaveAttribute('role', 'switch');

// A later write is only read, so it stays on the host. Copying it as well would put
// the same role on both elements, and two of that role in the accessibility tree.
await host.evaluate((el) => {
el.setAttribute('role', 'checkbox');
el.setAttribute('aria-label', 'barrier');
});
await expect(nativeButton).toHaveAttribute('aria-label', 'barrier');
await expect(nativeButton).toHaveAttribute('role', 'switch');
await host.evaluate((el) => el.setAttribute('role', 'checkbox'));

await expect(nativeButton).toHaveAttribute('role', 'checkbox');
await expect(host).toHaveAttribute('role', '');
});

test('should keep syncing after the button is detached and reattached', async ({ page }) => {
Expand Down Expand Up @@ -275,6 +270,11 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
});

test('should sync updates, empty values and removals after the initial copy', async ({ page }) => {
Comment thread
ShaneK marked this conversation as resolved.
Resolved
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/31659',
});

await page.setContent(`<ion-button aria-description="initial">Button</ion-button>`, config);

const host = page.locator('ion-button');
Expand All @@ -284,21 +284,69 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
await expect(host).not.toHaveAttribute('aria-description');
await expect(nativeButton).toHaveAttribute('aria-description', 'initial');

// Post-load writes stay on the host and are copied to the native button.
// A later write is copied to the native button and left on the host as an empty string.
await host.evaluate((el) => el.setAttribute('aria-description', 'second'));
await expect(host).toHaveAttribute('aria-description', 'second');
await expect(nativeButton).toHaveAttribute('aria-description', 'second');
await expect(host).toHaveAttribute('aria-description', '');

// An empty string is a valid ARIA attribute value.
await host.evaluate((el) => el.setAttribute('aria-description', ''));
await expect(nativeButton).toHaveAttribute('aria-description', '');

// A removal of a post-load write does reach the native button.
await host.evaluate((el) => el.setAttribute('aria-description', 'third'));
await expect(nativeButton).toHaveAttribute('aria-description', 'third');
await host.evaluate((el) => el.removeAttribute('aria-description'));
await expect(host).not.toHaveAttribute('aria-description');
await expect(nativeButton).not.toHaveAttribute('aria-description');
});

test('should not leave an aria-label on the host when the same value is written again', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/31659',
});

await page.setContent(`<ion-button aria-label="Scan">Button</ion-button>`, config);

const host = page.locator('ion-button');
const nativeButton = host.locator('button');

await host.evaluate((el) => el.setAttribute('aria-label', 'Scan'));

await expect(host).toHaveAttribute('aria-label', '');
await expect(nativeButton).toHaveAttribute('aria-label', 'Scan');
});

test('should leave a state written after load on the host so it can be read back', async ({ page }) => {
await page.setContent(`<ion-button aria-pressed="false">Button</ion-button>`, config);

const host = page.locator('ion-button');
const nativeButton = host.locator('button');
const toggle = () =>
host.evaluate((el) =>
el.setAttribute('aria-pressed', el.getAttribute('aria-pressed') === 'true' ? 'false' : 'true')
);

await toggle();
await expect(nativeButton).toHaveAttribute('aria-pressed', 'true');

await toggle();
await expect(nativeButton).toHaveAttribute('aria-pressed', 'false');
await expect(host).toHaveAttribute('aria-pressed', 'false');
});

test('should leave an aria-labelledby written after load on the host', async ({ page }) => {
await page.setContent(`<span id="label">Scan</span><ion-button>Button</ion-button>`, config);

const host = page.locator('ion-button');

await host.evaluate((el) => el.setAttribute('aria-labelledby', 'label'));

await expect(host.locator('button')).toHaveAttribute('aria-labelledby', 'label');
await expect(host).toHaveAttribute('aria-labelledby', 'label');
});

test('should keep a value from the initial markup when the host attribute is removed', async ({ page }) => {
await page.setContent(`<ion-button aria-label="initial">Button</ion-button>`, config);

Expand Down
19 changes: 13 additions & 6 deletions core/src/components/card/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,17 @@ export class Card implements ComponentInterface, AnchorInterface, ButtonInterfac

componentWillLoad() {
/**
* Only the initial copy takes the attribute off the host, so an `aria-label` written
* after load stays on the host too. That's harmless here, because unlike `ion-item`
* the card host renders no role of its own, so nothing reads the leftover copy.
* A non-clickable card renders no native element to apply the copy to, so an
* `aria-label` written after load has to stay on the host.
*/
this.ariaController = createAttributeController(this.el, ['aria-label'], () => forceUpdate(this));
this.ariaController = createAttributeController(
this.el,
['aria-label'],
() => forceUpdate(this),
[],
undefined,
() => !this.isClickable()
);

// A card that isn't clickable has no native button or link, so `tabindex` stays on the host.
this.tabindexController = createTabindexController(
Expand All @@ -105,12 +111,13 @@ export class Card implements ComponentInterface, AnchorInterface, ButtonInterfac
}

/**
* Changing `href` or `button` adds or removes the native element, so `tabindex` has to
* move with it.
* Changing `href` or `button` adds or removes the native element, so `tabindex` and a
* label written after load have to move with it.
*/
@Watch('href')
@Watch('button')
clickableChanged() {
this.ariaController?.refresh();
this.tabindexController?.refresh();
}

Expand Down
49 changes: 47 additions & 2 deletions core/src/components/card/test/a11y/card.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,34 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
await expect(host).not.toHaveAttribute('aria-label');
await expect(nativeCard).toHaveAttribute('aria-label', 'initial');

// Post-load writes stay on the host and are copied to the native element.
// A later write is copied to the native element and left on the host as an empty string.
await host.evaluate((el) => el.setAttribute('aria-label', 'second'));
await expect(host).toHaveAttribute('aria-label', 'second');
await expect(nativeCard).toHaveAttribute('aria-label', 'second');
await expect(host).toHaveAttribute('aria-label', '');

// An empty string is a valid ARIA attribute value.
await host.evaluate((el) => el.setAttribute('aria-label', ''));
await expect(nativeCard).toHaveAttribute('aria-label', '');

// A removal of a post-load write does reach the native element.
await host.evaluate((el) => el.setAttribute('aria-label', 'third'));
await expect(nativeCard).toHaveAttribute('aria-label', 'third');
await host.evaluate((el) => el.removeAttribute('aria-label'));
await expect(host).not.toHaveAttribute('aria-label');
await expect(nativeCard).not.toHaveAttribute('aria-label');
});

test('should keep an aria-label written after load on the host of a non-clickable card', async ({ page }) => {
await page.setContent(`<ion-card aria-label="initial">Card</ion-card>`, config);

const host = page.locator('ion-card');

await host.evaluate((el) => el.setAttribute('aria-label', 'updated'));
await page.waitForChanges();

await expect(host).toHaveAttribute('aria-label', 'updated');
});

test('should apply aria-label to the native element when the card becomes clickable', async ({ page }) => {
await page.setContent(`<ion-card aria-label="label">Card</ion-card>`, config);

Expand All @@ -136,6 +149,38 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
await expect(host.locator('[part="native"]')).toHaveAttribute('aria-label', 'label');
});

test('should put an aria-label written after load back on the host when the card stops being clickable', async ({
page,
}) => {
await page.setContent(`<ion-card button="true">Card</ion-card>`, config);

const host = page.locator('ion-card');

await host.evaluate((el) => el.setAttribute('aria-label', 'updated'));
await expect(host).toHaveAttribute('aria-label', '');

await host.evaluate((el: HTMLIonCardElement) => (el.button = false));
await page.waitForChanges();

await expect(host).toHaveAttribute('aria-label', 'updated');
});

test('should put an aria-label written after load back on the host when the card loses its href', async ({
page,
}) => {
await page.setContent(`<ion-card href="#">Card</ion-card>`, config);

const host = page.locator('ion-card');

await host.evaluate((el) => el.setAttribute('aria-label', 'updated'));
await expect(host).toHaveAttribute('aria-label', '');

await host.evaluate((el: HTMLIonCardElement) => (el.href = undefined));
await page.waitForChanges();

await expect(host).toHaveAttribute('aria-label', 'updated');
});

test('should not sync ARIA attributes other than aria-label', async ({ page }) => {
await page.setContent(`<ion-card button="true" aria-label="label">Card</ion-card>`, config);

Expand Down
22 changes: 15 additions & 7 deletions core/src/components/item/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,17 @@ export class Item implements ComponentInterface, AnchorInterface, ButtonInterfac
buttonChanged() {
// Update the focusable option when the button option is changed
this.focusable = this.isFocusable();
this.ariaController?.refresh();
this.tabindexController?.refresh();
}

/**
* Adding or removing `href` adds or removes the native element, so `tabindex` has to move
* with it. The same goes for `button` in `buttonChanged`.
* Adding or removing `href` adds or removes the native element, so `tabindex` and a label
* written after load have to move with it. The same goes for `button` in `buttonChanged`.
*/
@Watch('href')
hrefChanged() {
this.ariaController?.refresh();
this.tabindexController?.refresh();
}

Expand Down Expand Up @@ -200,12 +202,18 @@ export class Item implements ComponentInterface, AnchorInterface, ButtonInterfac

componentWillLoad() {
/**
* Only the initial copy takes the attribute off the host, so an `aria-label` written
* after load names both the native element and the Host, which is a `listitem` when
* the item is in an `ion-list`. The two names always agree, so a screen reader just
* reads it twice.
* A non-clickable item renders a `div`, which ARIA doesn't allow a name on, so an
* `aria-label` written after load also stays on the Host, which is a `listitem`
* when the item is in an `ion-list`.
*/
this.ariaController = createAttributeController(this.el, ['aria-label'], () => forceUpdate(this));
this.ariaController = createAttributeController(
this.el,
['aria-label'],
() => forceUpdate(this),
[],
undefined,
() => !this.isClickable()
);

// An item that isn't clickable has no native button or link, so `tabindex` stays on the host.
this.tabindexController = createTabindexController(
Expand Down
50 changes: 48 additions & 2 deletions core/src/components/item/test/a11y/item.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,67 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
await expect(host).not.toHaveAttribute('aria-label');
await expect(nativeItem).toHaveAttribute('aria-label', 'initial');

// Post-load writes stay on the host and are copied to the native element.
// A later write is copied to the native element and left on the host as an empty string.
await host.evaluate((el) => el.setAttribute('aria-label', 'second'));
await expect(host).toHaveAttribute('aria-label', 'second');
await expect(nativeItem).toHaveAttribute('aria-label', 'second');
await expect(host).toHaveAttribute('aria-label', '');

// An empty string is a valid ARIA attribute value.
await host.evaluate((el) => el.setAttribute('aria-label', ''));
await expect(nativeItem).toHaveAttribute('aria-label', '');

// A removal of a post-load write does reach the native element.
await host.evaluate((el) => el.setAttribute('aria-label', 'third'));
await expect(nativeItem).toHaveAttribute('aria-label', 'third');
await host.evaluate((el) => el.removeAttribute('aria-label'));
await expect(host).not.toHaveAttribute('aria-label');
await expect(nativeItem).not.toHaveAttribute('aria-label');
});

test('should keep an aria-label written after load on the host of a non-clickable item', async ({ page }) => {
await page.setContent(`<ion-list><ion-item aria-label="initial">Item</ion-item></ion-list>`, config);

const host = page.locator('ion-item');

await host.evaluate((el) => el.setAttribute('aria-label', 'updated'));
await page.waitForChanges();

await expect(host).toHaveAttribute('aria-label', 'updated');
});

test('should move an aria-label written after load off the host when the item becomes clickable', async ({
page,
}) => {
await page.setContent(`<ion-list><ion-item>Item</ion-item></ion-list>`, config);

const host = page.locator('ion-item');

await host.evaluate((el) => el.setAttribute('aria-label', 'updated'));
await page.waitForChanges();
await expect(host).toHaveAttribute('aria-label', 'updated');

await host.evaluate((el: HTMLIonItemElement) => (el.button = true));
await page.waitForChanges();

await expect(host).toHaveAttribute('aria-label', '');
await expect(host.locator('button')).toHaveAttribute('aria-label', 'updated');
});

test('should move an aria-label written after load off the host when the item gets an href', async ({ page }) => {
await page.setContent(`<ion-list><ion-item>Item</ion-item></ion-list>`, config);

const host = page.locator('ion-item');

await host.evaluate((el) => el.setAttribute('aria-label', 'updated'));
await page.waitForChanges();

await host.evaluate((el: HTMLIonItemElement) => (el.href = '#'));
await page.waitForChanges();

await expect(host).toHaveAttribute('aria-label', '');
await expect(host.locator('a')).toHaveAttribute('aria-label', 'updated');
});

test('should not sync ARIA attributes other than aria-label', async ({ page }) => {
await page.setContent(`<ion-item button="true" aria-label="label">Item</ion-item>`, config);

Expand Down
Loading
Loading