Skip to content

Commit a61222f

Browse files
authored
fix: Add default aria role to svg and group elements (#9697)
* fix: Add default aria role to svg and group elements * addressed pr feedback
1 parent 956f049 commit a61222f

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

packages/blockly/core/utils/dom.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
// Former goog.module ID: Blockly.utils.dom
88

9-
import type {Svg} from './svg.js';
9+
import * as aria from './aria.js';
10+
import {Svg} from './svg.js';
1011

1112
/**
1213
* Required name space for SVG elements.
@@ -56,6 +57,17 @@ export function createSvgElement<T extends SVGElement>(
5657
opt_parent?: Element | null,
5758
): T {
5859
const e = document.createElementNS(SVG_NS, `${name}`) as T;
60+
/**
61+
* For svg and group (g) elements, we set the role to generic so that they are ignored by assistive technologies.
62+
*/
63+
if (
64+
name === Svg.SVG.toString() ||
65+
name === Svg.G.toString() ||
66+
e.tagName === Svg.SVG.toString() ||
67+
e.tagName === Svg.G.toString()
68+
) {
69+
aria.setRole(e, aria.Role.GENERIC);
70+
}
5971
for (const key in attrs) {
6072
e.setAttribute(key, `${attrs[key]}`);
6173
}

packages/blockly/tests/mocha/utils_test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,51 @@ suite('Utils', function () {
433433
Blockly.utils.dom.removeClass(p, 'zero');
434434
assert.equal(p.className, '', 'Removing "zero"');
435435
});
436+
437+
suite('createSvgElement', function () {
438+
test('svg elements of type g have the generic role by default', function () {
439+
const svgG = Blockly.utils.dom.createSvgElement(
440+
Blockly.utils.Svg.G,
441+
{},
442+
);
443+
const g = Blockly.utils.dom.createSvgElement('g', {});
444+
assert.equal(svgG.getAttribute('role'), 'generic');
445+
assert.equal(g.getAttribute('role'), 'generic');
446+
});
447+
test('svg elements of type svg have the generic role by default', function () {
448+
const svgSvg = Blockly.utils.dom.createSvgElement(
449+
Blockly.utils.Svg.SVG,
450+
{},
451+
);
452+
const svg = Blockly.utils.dom.createSvgElement('svg', {});
453+
assert.equal(svgSvg.getAttribute('role'), 'generic');
454+
assert.equal(svg.getAttribute('role'), 'generic');
455+
});
456+
test('svg elements of type g reflect the role passed in when created', function () {
457+
const svgG = Blockly.utils.dom.createSvgElement(Blockly.utils.Svg.G, {
458+
role: 'button',
459+
});
460+
const g = Blockly.utils.dom.createSvgElement('g', {role: 'button'});
461+
assert.equal(svgG.getAttribute('role'), 'button');
462+
assert.equal(g.getAttribute('role'), 'button');
463+
});
464+
test('svg elements of type svg reflect the role passed in when created', function () {
465+
const svgSvg = Blockly.utils.dom.createSvgElement(
466+
Blockly.utils.Svg.SVG,
467+
{role: 'button'},
468+
);
469+
const svg = Blockly.utils.dom.createSvgElement('svg', {role: 'button'});
470+
assert.equal(svgSvg.getAttribute('role'), 'button');
471+
assert.equal(svg.getAttribute('role'), 'button');
472+
});
473+
test('other svg elements do not default to generic role', function () {
474+
const textElement = Blockly.utils.dom.createSvgElement(
475+
Blockly.utils.Svg.TEXT,
476+
{},
477+
);
478+
assert.equal(textElement.getAttribute('role'), null);
479+
});
480+
});
436481
});
437482

438483
suite('String', function () {

0 commit comments

Comments
 (0)