Skip to content

Commit 93268a7

Browse files
committed
feat: add aria-label handling to UIButton and corresponding tests
1 parent e491752 commit 93268a7

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*!
2+
* Jodit Editor (https://xdsoft.net/jodit/)
3+
* Released under MIT see LICENSE.txt in the project root for license information.
4+
* Copyright (c) 2013-2026 Valerii Chupurnov. All rights reserved. https://xdsoft.net
5+
*/
6+
7+
describe('Test UIButton', () => {
8+
const { UIButton } = Jodit.modules;
9+
10+
let editor;
11+
beforeEach(() => {
12+
editor = getJodit();
13+
});
14+
15+
describe('aria-label', () => {
16+
it('should set aria-label from tooltip when text is empty', () => {
17+
const button = new UIButton(editor);
18+
button.state.tooltip = 'Bold';
19+
20+
expect(button.button.getAttribute('aria-label')).eq('Bold');
21+
});
22+
23+
it('should not set aria-label when text is provided', () => {
24+
const button = new UIButton(editor);
25+
button.state.text = 'Bold';
26+
button.state.tooltip = 'Make text bold';
27+
28+
expect(button.button.getAttribute('aria-label')).is.null;
29+
});
30+
31+
it('should remove aria-label when text is set after tooltip', () => {
32+
const button = new UIButton(editor);
33+
button.state.tooltip = 'Bold';
34+
35+
expect(button.button.getAttribute('aria-label')).eq('Bold');
36+
37+
button.state.text = 'Bold';
38+
39+
expect(button.button.getAttribute('aria-label')).is.null;
40+
});
41+
42+
it('should restore aria-label when text is cleared', async () => {
43+
const button = new UIButton(editor);
44+
button.state.tooltip = 'Bold';
45+
button.state.text = 'Bold';
46+
47+
expect(button.button.getAttribute('aria-label')).is.null;
48+
49+
button.state.text = '';
50+
51+
await editor.async.requestIdlePromise();
52+
expect(button.button.getAttribute('aria-label')).eq('Bold');
53+
});
54+
55+
it('should not set aria-label when both text and tooltip are empty', () => {
56+
const button = new UIButton(editor);
57+
58+
expect(button.button.getAttribute('aria-label')).is.null;
59+
});
60+
});
61+
});

src/core/ui/button/button/button.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export class UIButton extends UIElement implements IUIButton {
138138
@watch('state.text', { immediately: false })
139139
protected onChangeText(): void {
140140
this.text.textContent = this.jodit.i18n(this.state.text);
141+
this.__updateAriaLabel();
141142
}
142143

143144
@watch('state.text', { immediately: false })
@@ -177,7 +178,18 @@ export class UIButton extends UIElement implements IUIButton {
177178
attr(this.container, 'title', i8nTooltip);
178179
}
179180

180-
attr(this.container, 'aria-label', i8nTooltip);
181+
this.__updateAriaLabel();
182+
}
183+
184+
private __updateAriaLabel(): void {
185+
const hasText = this.state.text.trim().length > 0;
186+
attr(
187+
this.button,
188+
'aria-label',
189+
!hasText && this.state.tooltip
190+
? this.jodit.i18n(this.state.tooltip)
191+
: null
192+
);
181193
}
182194

183195
@watch('state.tabIndex', { immediately: false })

test/loader.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)