X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=src%2Ftemplate%2Fbadge-svg.js;h=7930a1f72911862777bdeb9d9f4f28bef0d92272;hb=8cd88ab4087a7fab2ccd6e231c64d7f0f1299f26;hp=681e0720a2c3a001745655a4426a0a3123387c83;hpb=9696c012e6b9a6c58904baa397ca0ebf78112316;p=websub-hub diff --git a/src/template/badge-svg.js b/src/template/badge-svg.js index 681e072..7930a1f 100644 --- a/src/template/badge-svg.js +++ b/src/template/badge-svg.js @@ -1,19 +1,6 @@ 'use strict'; -function escapeXml(s) { - if (typeof s === 'number') { - return s; - } else if (typeof s !== 'string') { - return undefined; - } else { - return s - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"') - .replace(/'/g, '''); - } -} +const th = require('./template-helper'); const ctxDefaults = { @@ -26,6 +13,12 @@ const ctxDefaults = { }; +/** + * + * @param {number} n number + * @param {number} p precision + * @returns {number} rounded + */ function fixedRound(n, p = 2) { return Number(n.toFixed(p)); } @@ -33,19 +26,21 @@ function fixedRound(n, p = 2) { /** * image/svg+xml;charset=utf-8 formatted badge with subscriber count for a topic - * @param {Object} ctx - badge-specific context (not request context) - * @param {String} label - * @param {String} message - * @param {String} accessibleText - * @returns {String} + * @param {object} ctx - badge-specific context (not request context) + * @param {string} label label + * @param {string} message message + * @param {string} accessibleText accessible text + * @returns {string} svg element */ module.exports = (ctx, label, message, accessibleText) => { - ctx = Object.assign({}, ctxDefaults, ctx, { + ctx = { + ...ctxDefaults, + ...ctx, label, message, accessibleText, - }); + }; ctx.verticalMargin = fixedRound(ctx.height * 0.69); ctx.labelWidth = fixedRound(ctx.label.length * ctx.charWidth); ctx.messageWidth = fixedRound(ctx.message.length * ctx.charWidth); @@ -57,8 +52,8 @@ module.exports = (ctx, label, message, accessibleText) => { * renderer from https://github.com/badges/shields/tree/master/badge-maker which * is under the http://creativecommons.org/publicdomain/zero/1.0/ license. */ - return ` - ${escapeXml(ctx.accessibleText)} + return ` + ${th.xmlEscape(ctx.accessibleText)} @@ -74,8 +69,8 @@ module.exports = (ctx, label, message, accessibleText) => { - ${escapeXml(ctx.label)} - ${escapeXml(ctx.message)} + ${th.xmlEscape(ctx.label)} + ${th.xmlEscape(ctx.message)} `; };