Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions packages/blockly/core/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

import * as aria from './aria.js';
import type {Svg} from './svg.js';

/**
Expand Down Expand Up @@ -56,6 +57,17 @@ export function createSvgElement<T extends SVGElement>(
opt_parent?: Element | null,
): T {
const e = document.createElementNS(SVG_NS, `${name}`) as T;
/**
* For svg and group (g) elements, we set the role to generic so that they are ignored by assistive technologies.
*/
if (
name === 'svg' ||
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than string literals, it might be preferable to use Svg.G.tagName and Svg.SVG.tagName (from utils/svg.ts)

name === 'g' ||
e.tagName === 'svg' ||
e.tagName === 'g'
) {
aria.setRole(e, aria.Role.GENERIC);
}
for (const key in attrs) {
e.setAttribute(key, `${attrs[key]}`);
}
Expand Down
45 changes: 45 additions & 0 deletions packages/blockly/tests/mocha/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,51 @@ suite('Utils', function () {
Blockly.utils.dom.removeClass(p, 'zero');
assert.equal(p.className, '', 'Removing "zero"');
});

suite('createSvgElement', function () {
test('svg elements of type g have the generic role by default', function () {
const svgG = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.G,
{},
);
const g = Blockly.utils.dom.createSvgElement('g', {});
assert.equal(svgG.getAttribute('role'), 'generic');
assert.equal(g.getAttribute('role'), 'generic');
});
test('svg elements of type svg have the generic role by default', function () {
const svgSvg = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.G,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Blockly.utils.Svg.SVG I think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, thanks for catching that!

{},
);
const svg = Blockly.utils.dom.createSvgElement('svg', {});
assert.equal(svgSvg.getAttribute('role'), 'generic');
assert.equal(svg.getAttribute('role'), 'generic');
});
test('svg elements of type g reflect the role passed in when created', function () {
const svgG = Blockly.utils.dom.createSvgElement(Blockly.utils.Svg.G, {
role: 'button',
});
const g = Blockly.utils.dom.createSvgElement('g', {role: 'button'});
assert.equal(svgG.getAttribute('role'), 'button');
assert.equal(g.getAttribute('role'), 'button');
});
test('svg elements of type svg reflect the role passed in when created', function () {
const svgSvg = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.SVG,
{role: 'button'},
);
const svg = Blockly.utils.dom.createSvgElement('svg', {role: 'button'});
assert.equal(svgSvg.getAttribute('role'), 'button');
assert.equal(svg.getAttribute('role'), 'button');
});
test('other svg elements do not default to generic role', function () {
const textElement = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.TEXT,
{},
);
assert.equal(textElement.getAttribute('role'), null);
});
});
});

suite('String', function () {
Expand Down
Loading