Skip to content

Commit ce86628

Browse files
authored
fix: Improve ARIA fallback generation for HTML-based menu items (#9827)
* fix: Improve ARIA fallback generation for HTML-based menu items * chore: Remove unneeded cast
1 parent 23fcf16 commit ce86628

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

packages/blockly/core/menuitem.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ export class MenuItem {
114114
getAriaLabel(): string {
115115
// This fallback should only be hit by Context Menu items as all
116116
// FieldDropdown options should have an ARIA label.
117-
return this.ariaLabel || String(this.content);
117+
return (
118+
this.ariaLabel ||
119+
(typeof this.content === 'string'
120+
? this.content
121+
: this.content.textContent)
122+
);
118123
}
119124

120125
/** Dispose of this menu item. */

packages/blockly/tests/mocha/menu_item_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,14 @@ suite('Menu items', function () {
173173
this.menuItem.performAction(new Event('click'));
174174
assert.isTrue(called);
175175
});
176+
177+
test('return accurate ARIA labels for HTML elements', function () {
178+
const div = document.createElement('div');
179+
const nestedDiv = document.createElement('div');
180+
nestedDiv.textContent = 'nested';
181+
div.textContent = 'test';
182+
div.appendChild(nestedDiv);
183+
const testMenuItem = new Blockly.MenuItem(div);
184+
assert.equal(testMenuItem.getAriaLabel(), 'testnested');
185+
});
176186
});

0 commit comments

Comments
 (0)