From b216bcc17e667a2fb09bede7be0d1c67e731d767 Mon Sep 17 00:00:00 2001 From: Danial Date: Fri, 27 Jul 2018 08:27:32 +1200 Subject: [PATCH] Hide left hand of badge if no label (#1585) * hide left side if no text * fix whitespace at start of template * for-the-badge done * center text * add logo support * update snapshot test * make first - in static badge optional * add no label support to non static badges * Add test for label * update to not allow use of false/0/null * fixup --- __snapshots__/make-badge.spec.js | 2 +- lib/badge-data.js | 2 +- lib/badge-data.spec.js | 3 +++ server.js | 2 +- templates/flat-square-template.svg | 10 ++++++---- templates/flat-template.svg | 14 ++++++++------ templates/for-the-badge-template.svg | 10 ++++++---- templates/plastic-template.svg | 14 ++++++++------ 8 files changed, 34 insertions(+), 23 deletions(-) diff --git a/__snapshots__/make-badge.spec.js b/__snapshots__/make-badge.spec.js index 49ca8ac4ec..c1c2ac2404 100644 --- a/__snapshots__/make-badge.spec.js +++ b/__snapshots__/make-badge.spec.js @@ -1,5 +1,5 @@ exports['The badge generator SVG should always produce the same SVG (unless we have changed something!) 1'] = ` -cactuscactusgrowngrown + cactuscactusgrowngrown ` exports['The badge generator JSON should always produce the same JSON (unless we have changed something!) 1'] = ` diff --git a/lib/badge-data.js b/lib/badge-data.js index c31f1c0cc8..7fd8ef5fe2 100644 --- a/lib/badge-data.js +++ b/lib/badge-data.js @@ -66,7 +66,7 @@ function setBadgeColor(badgeData, color) { } function makeLabel(defaultLabel, overrides) { - return overrides.label || defaultLabel; + return '' + (overrides.label === undefined ? defaultLabel || '' : overrides.label); } function makeLogo(defaultNamedLogo, overrides) { diff --git a/lib/badge-data.spec.js b/lib/badge-data.spec.js index 7fa0c1291d..518ed7e7c5 100644 --- a/lib/badge-data.spec.js +++ b/lib/badge-data.spec.js @@ -42,6 +42,9 @@ describe('Badge data helpers', function() { test(makeLabel, () => { given('my badge', {}).expect('my badge'); given('my badge', { label: 'no, my badge' }).expect('no, my badge'); + given('my badge', { label: false }).expect('false'); + given('my badge', { label: 0 }).expect('0'); + given('my badge', { label: '' }).expect(''); }); test(makeLogo, () => { diff --git a/server.js b/server.js index fedf2154fe..73bfe7db0b 100644 --- a/server.js +++ b/server.js @@ -7808,7 +7808,7 @@ cache(function(data, match, sendBadge, request) { })); // Any badge. -camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/, +camp.route(/^\/(:|badge\/)(([^-]|--)*?)-?(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/, function(data, match, end, ask) { var subject = escapeFormat(match[2]); var status = escapeFormat(match[4]); diff --git a/templates/flat-square-template.svg b/templates/flat-square-template.svg index ed25c320b3..9da2a98fa5 100644 --- a/templates/flat-square-template.svg +++ b/templates/flat-square-template.svg @@ -1,14 +1,16 @@ - + - + {{?it.logo}} {{?}} - {{=it.escapedText[0]}} - {{=it.escapedText[1]}} + {{?it.text[0].length}} + {{=it.escapedText[0]}} + {{?}} + {{=it.escapedText[1]}} {{?(it.links[0] && it.links[0].length)}} diff --git a/templates/flat-template.svg b/templates/flat-template.svg index 164000c97c..1e722f4d2f 100644 --- a/templates/flat-template.svg +++ b/templates/flat-template.svg @@ -1,4 +1,4 @@ - + @@ -9,7 +9,7 @@ - + @@ -18,10 +18,12 @@ {{?it.logo}} {{?}} - {{=it.escapedText[0]}} - {{=it.escapedText[0]}} - {{=it.escapedText[1]}} - {{=it.escapedText[1]}} + {{?it.text[0].length}} + {{=it.escapedText[0]}} + {{=it.escapedText[0]}} + {{?}} + {{=it.escapedText[1]}} + {{=it.escapedText[1]}} {{?(it.links[0] && it.links[0].length)}} diff --git a/templates/for-the-badge-template.svg b/templates/for-the-badge-template.svg index c9f21754a1..7b8807d115 100644 --- a/templates/for-the-badge-template.svg +++ b/templates/for-the-badge-template.svg @@ -1,16 +1,18 @@ - + - + {{?it.logo}} {{?}} - {{=it.escapedText[0]}} + {{?it.text[0].length}} + {{=it.escapedText[0]}} + {{?}} {{=it.escapedText[1]}} - {{?(it.links[0] && it.links[0].length)}} + {{?(it.text[0].length && it.links[0] && it.links[0].length)}} diff --git a/templates/plastic-template.svg b/templates/plastic-template.svg index 365e1850f0..fbeb2487a3 100644 --- a/templates/plastic-template.svg +++ b/templates/plastic-template.svg @@ -1,4 +1,4 @@ - + @@ -11,7 +11,7 @@ - + @@ -20,10 +20,12 @@ {{?it.logo}} {{?}} - {{=it.escapedText[0]}} - {{=it.escapedText[0]}} - {{=it.escapedText[1]}} - {{=it.escapedText[1]}} + {{?it.text[0].length}} + {{=it.escapedText[0]}} + {{=it.escapedText[0]}} + {{?}} + {{=it.escapedText[1]}} + {{=it.escapedText[1]}} {{?(it.links[0] && it.links[0].length)}}