Skip to content
This repository was archived by the owner on Oct 9, 2020. It is now read-only.

Commit 7d5d9af

Browse files
committed
Apply idPrefix to references in aria-labelledby
To make svgs more accessible to people with screen readers, we can make use of a few elements and attributes: - <title> to set a title for the image - <description> to describe the image - @aria-labelledby to tell screen readers where the title and description are located in the tree Here's some more info on how to best use them: https://css-tricks.com/accessible-svgs/#article-header-id-6 We're using svg-inline-loader over at brigade.com to make sure every svg has unique ids, but after adding accessibility markup we noticed that the references weren't right: <svg viewBox="0 0 24 24" role="img" aria-labelledby="shareIconTitle shareIconDesc"><title id="__C3oQyi-__shareIconTitle">Share Icon</title><desc id="__C3oQyi-__shareIconDesc">Paper airline silhouette.</desc><path d="M21.184 2.073a.543.543 0 0 1 .806.574l-3.127 16.317a.657.657 0 0 1-.861.494l-4.866-1.825-2.075"></path></svg> (I've manually removed some stuff from the above svg to make it shorter). Notice how the references in aria-labelledby are now referencing missing ids. To fix this, we can apply the prefix here as well.
1 parent e5a07d1 commit 7d5d9af

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

lib/transformer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ function createClassPrefix(classPrefix) {
116116
//Prefix classes when multiple classes are present
117117
var classes = tag.attributes[classIdx][1];
118118
var prefixedClassString = "";
119-
119+
120120
classes = classes.replace(/[ ]+/,' ');
121121
classes = classes.split(' ');
122122
classes.forEach(function(classI){
123123
prefixedClassString += classPrefix + classI + ' ';
124124
});
125-
125+
126126
tag.attributes[classIdx][1] = prefixedClassString;
127127
}
128128
}
@@ -155,8 +155,11 @@ function createIdPrefix(idPrefix) {
155155
var id = match.substring(5, match.length -1);
156156
return "url(#" + idPrefix + id + ")";
157157
});
158+
} else if (attr[0] === 'aria-labelledby') {
159+
attr[1] = attr[1].split(' ').map(function (id) {
160+
return idPrefix + id;
161+
}).join(' ');
158162
}
159-
160163
});
161164
}
162165

tests/fixtures/with-ids.svg

Lines changed: 3 additions & 1 deletion
Loading

tests/svg-inline-loader.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ describe('getExtractedSVG()', function(){
5656
var svgWithStyle = require('raw!./fixtures/with-ids.svg');
5757
var processedStyleInsertedSVG = SVGInlineLoader.getExtractedSVG(svgWithStyle, { idPrefix: 'test.prefix-' });
5858

59-
60-
assert.isTrue( processedStyleInsertedSVG.match(/test\.prefix-foo/g).length === 3 );
59+
assert.isTrue( processedStyleInsertedSVG.match(/test\.prefix-foo/g).length === 7 );
6160
// // replaces xlink:href=
6261
assert.isTrue( processedStyleInsertedSVG.match(/xlink:href="#test.prefix-foo"/g).length === 1 );
6362
// // replaces url(#foo)
6463
assert.isTrue( processedStyleInsertedSVG.match(/url\(#test\.prefix-foo\)/g).length === 1 );
64+
// replaces aria-labelledby
65+
assert.isTrue( processedStyleInsertedSVG.match(
66+
/aria-labelledby="test\.prefix-foo-title test\.prefix-foo-description"/g).length === 1 );
6567
});
6668

6769
it('should be able to specify tags to be removed by `removingTags` option', function () {

0 commit comments

Comments
 (0)