From 179a1d3c2a496a65759de5f82e45a6c7bdb6852e Mon Sep 17 00:00:00 2001 From: Evelyn Hathaway Date: Mon, 6 Jul 2020 07:52:07 -0700 Subject: [PATCH] feat: add accessible text alternative in SVG (#62) * feat: accessible text alternative in SVG * fix: change alt text strategy to support IE 11 --- src/index.ts | 35 +++++++++--- tap-snapshots/test-badgen.spec.ts-TAP.test.js | 55 +++++++++++-------- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index a09a101..b768c69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,14 +49,16 @@ export function badgen ({ label = sanitize(label) status = sanitize(status) + const accessibleText = createAccessibleText({label, status}) if (style === 'flat') { - return ` + return ` + ${accessibleText} - + ` } - return ` + return ` + ${accessibleText} @@ -77,7 +80,7 @@ export function badgen ({ - + - + @@ -129,6 +134,20 @@ function sanitize (str: string): string { return str.replace(/\u0026/g, '&').replace(/\u003C/g, '<') } +function sanitizeAttribute (str: string): string { + return str.replace(/\u0022/g, '\"') +} + +interface AccessibleTextProps { + status: string; + label?: string; +} + +function createAccessibleText({label, status}: AccessibleTextProps): string { + const labelPrefix = label ? `${label}: ` : ''; + return labelPrefix + status; +} + function typeAssert (assertion: boolean, message: string): void { if (!assertion) throw new TypeError(message) } diff --git a/tap-snapshots/test-badgen.spec.ts-TAP.test.js b/tap-snapshots/test-badgen.spec.ts-TAP.test.js index 674c8ef..cea8802 100644 --- a/tap-snapshots/test-badgen.spec.ts-TAP.test.js +++ b/tap-snapshots/test-badgen.spec.ts-TAP.test.js @@ -6,7 +6,8 @@ */ 'use strict' exports[`test/badgen.spec.ts TAP generate badge with { label, status } > snapshot 1`] = ` - + + npm: v1.0.0 @@ -17,7 +18,7 @@ exports[`test/badgen.spec.ts TAP generate badge with { label, status } > snapsho - + - +