From df6c6f026dd1f9e5ff4c7f5691e15d328f55637c Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 28 Mar 2021 04:25:47 +0100 Subject: [PATCH] feat: apply escaping to all string inputs (#68) --- src/index.ts | 4 ++ tap-snapshots/test-badgen.spec.ts-TAP.test.js | 42 +++++++++++++++++++ test/badgen.spec.ts | 21 ++++++++++ 3 files changed, 67 insertions(+) diff --git a/src/index.ts b/src/index.ts index aa772fb..5f17901 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,6 +49,9 @@ export function badgen ({ label = sanitize(label) status = sanitize(status) + color = sanitize(color) + labelColor = sanitize(labelColor) + icon = icon ? sanitize(icon) : icon const accessibleText = createAccessibleText({label, status}) if (style === 'flat') { @@ -98,6 +101,7 @@ function bare ({ status, color, style, scale }) { const stRectWidth = stTextWidth + 115 status = sanitize(status) + color = sanitize(color) if (style === 'flat') { return ` diff --git a/tap-snapshots/test-badgen.spec.ts-TAP.test.js b/tap-snapshots/test-badgen.spec.ts-TAP.test.js index 14bc83b..dd37adf 100644 --- a/tap-snapshots/test-badgen.spec.ts-TAP.test.js +++ b/tap-snapshots/test-badgen.spec.ts-TAP.test.js @@ -5,6 +5,48 @@ * Make sure to inspect the output below. Do not ignore changes! */ 'use strict' +exports[`test/badgen.spec.ts TAP ensure badgen() correctly escapes string inputs > snapshot 1`] = ` + + <escape me>: <escape me> + + + + + + + + + + + + + +` + +exports[`test/badgen.spec.ts TAP ensure bare() correctly escapes string inputs > snapshot 1`] = ` + + <escape me> + + + + + + + + + + + +` + exports[`test/badgen.spec.ts TAP generate badge with { label, status } > snapshot 1`] = ` npm: v1.0.0 diff --git a/test/badgen.spec.ts b/test/badgen.spec.ts index eb8cebc..490b413 100644 --- a/test/badgen.spec.ts +++ b/test/badgen.spec.ts @@ -54,6 +54,18 @@ tap.test('generate badge with { label, status, icon, style }', t => { t.end() }) +tap.test('ensure badgen() correctly escapes string inputs', t => { + const svg = badgen({ + label: '', + status: '', + color: '', + icon: '', + labelColor: '', + }) + t.matchSnapshot(svg, 'snapshot') + t.end() +}) + tap.test('generate bare badge with { status }', t => { const svg = badgen({ status: 'v1.0.0' }) t.ok(typeof svg === 'string', 'successfully generated') @@ -75,6 +87,15 @@ tap.test('generate bare badge with { status, style }', t => { t.end() }) +tap.test('ensure bare() correctly escapes string inputs', t => { + const svg = badgen({ + status: '', + color: '', + }) + t.matchSnapshot(svg, 'snapshot') + t.end() +}) + tap.test('type checking', t => { // @ts-ignore t.throws(() => badgen({}), TypeError, 'throw if status is non-string')