'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, '"')
- .replace(/'/g, ''');
- }
-}
+const th = require('./template-helper');
const ctxDefaults = {
};
+/**
+ *
+ * @param {number} n number
+ * @param {number} p precision
+ * @returns {number} rounded
+ */
function fixedRound(n, p = 2) {
return Number(n.toFixed(p));
}
/**
* 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);
* renderer from https://github.com/badges/shields/tree/master/badge-maker which
* is under the http://creativecommons.org/publicdomain/zero/1.0/ license.
*/
- return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${ctx.width}" height="${ctx.height}" role="img" aria-label="${escapeXml(ctx.accessibleText)}">
- <title>${escapeXml(ctx.accessibleText)}</title>
+ return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${ctx.width}" height="${ctx.height}" role="img" aria-label="${th.xmlEscape(ctx.accessibleText)}">
+ <title>${th.xmlEscape(ctx.accessibleText)}</title>
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#fff" stop-opacity=".7"/>
<stop offset=".1" stop-color="#aaa" stop-opacity=".1"/>
<rect width="${ctx.width}" height="${ctx.height}" fill="url(#s)"/>
</g>
<g fill="${ctx.color}" text-anchor="left" font-family="${ctx.fontFamily}" text-rendering="geometricPrecision" font-size="11" font-weight="bold">
- <text x="${ctx.halfCharWidth}" y="${ctx.verticalMargin}">${escapeXml(ctx.label)}</text>
- <text x="${fixedRound(ctx.halfCharWidth + ctx.labelWidth)}" y="${ctx.verticalMargin}">${escapeXml(ctx.message)}</text>
+ <text x="${ctx.halfCharWidth}" y="${ctx.verticalMargin}">${th.xmlEscape(ctx.label)}</text>
+ <text x="${fixedRound(ctx.halfCharWidth + ctx.labelWidth)}" y="${ctx.verticalMargin}">${th.xmlEscape(ctx.message)}</text>
</g>
</svg>`;
};