From 7810032f7b67916d411319a36f4f5fc91844a903 Mon Sep 17 00:00:00 2001 From: Senya <10187586+cmrd-senya@users.noreply.github.com> Date: Mon, 7 Aug 2023 06:59:40 +0300 Subject: [PATCH] Toolbar: add aria-pressed to toolbar buttons (#3615) --- modules/toolbar.ts | 7 +++++- test/unit/modules/toolbar.spec.ts | 42 +++++++++++++++++++------------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/modules/toolbar.ts b/modules/toolbar.ts index 0ee20fe701..157a05d43f 100644 --- a/modules/toolbar.ts +++ b/modules/toolbar.ts @@ -164,6 +164,7 @@ class Toolbar extends Module { } } else if (range == null) { input.classList.remove('ql-active'); + input.setAttribute('aria-pressed', 'false'); } else if (input.hasAttribute('value')) { // both being null should match (default values) // '1' should match with 1 (headers) @@ -173,8 +174,11 @@ class Toolbar extends Module { formats[format].toString() === input.getAttribute('value')) || (formats[format] == null && !input.getAttribute('value')); input.classList.toggle('ql-active', isActive); + input.setAttribute('aria-pressed', isActive.toString()); } else { - input.classList.toggle('ql-active', formats[format] != null); + const isActive = formats[format] != null; + input.classList.toggle('ql-active', isActive); + input.setAttribute('aria-pressed', isActive.toString()); } }); } @@ -185,6 +189,7 @@ function addButton(container: HTMLElement, format: string, value?: unknown) { const input = document.createElement('button'); input.setAttribute('type', 'button'); input.classList.add(`ql-${format}`); + input.setAttribute('aria-pressed', 'false'); if (value != null) { // @ts-expect-error input.value = value; diff --git a/test/unit/modules/toolbar.spec.ts b/test/unit/modules/toolbar.spec.ts index 62e9861147..be69dde667 100644 --- a/test/unit/modules/toolbar.spec.ts +++ b/test/unit/modules/toolbar.spec.ts @@ -28,8 +28,8 @@ describe('Toolbar', () => { addControls(container, ['bold', 'italic']); expect(container).toEqualHTML(` - - + + `); }); @@ -42,12 +42,12 @@ describe('Toolbar', () => { ]); expect(container).toEqualHTML(` - - + + - - + + `); }); @@ -57,8 +57,8 @@ describe('Toolbar', () => { addControls(container, ['bold', { header: '2' }]); expect(container).toEqualHTML(` - - + + `); }); @@ -108,14 +108,14 @@ describe('Toolbar', () => { - - - - + + + + - - + + - - + + `); }); @@ -176,8 +176,10 @@ describe('Toolbar', () => { ) as HTMLButtonElement; quill.setSelection(7); expect(boldButton.classList.contains('ql-active')).toBe(true); + expect(boldButton.attributes['aria-pressed'].value).toBe('true'); quill.setSelection(2); expect(boldButton.classList.contains('ql-active')).toBe(false); + expect(boldButton.attributes['aria-pressed'].value).toBe('false'); }); test('link', () => { @@ -187,8 +189,10 @@ describe('Toolbar', () => { ) as HTMLButtonElement; quill.setSelection(12); expect(linkButton.classList.contains('ql-active')).toBe(true); + expect(linkButton.attributes['aria-pressed'].value).toBe('true'); quill.setSelection(2); expect(linkButton.classList.contains('ql-active')).toBe(false); + expect(linkButton.attributes['aria-pressed'].value).toBe('false'); }); test('dropdown', () => { @@ -217,12 +221,18 @@ describe('Toolbar', () => { quill.setSelection(17); expect(centerButton.classList.contains('ql-active')).toBe(true); expect(leftButton.classList.contains('ql-active')).toBe(false); + expect(centerButton.attributes['aria-pressed'].value).toBe('true'); + expect(leftButton.attributes['aria-pressed'].value).toBe('false'); quill.setSelection(2); expect(centerButton.classList.contains('ql-active')).toBe(false); expect(leftButton.classList.contains('ql-active')).toBe(true); + expect(centerButton.attributes['aria-pressed'].value).toBe('false'); + expect(leftButton.attributes['aria-pressed'].value).toBe('true'); quill.blur(); expect(centerButton.classList.contains('ql-active')).toBe(false); expect(leftButton.classList.contains('ql-active')).toBe(false); + expect(centerButton.attributes['aria-pressed'].value).toBe('false'); + expect(leftButton.attributes['aria-pressed'].value).toBe('false'); }); }); });