Merge branch 'v1.3-dev'
[websub-hub] / src / template / badge-svg.js
index 681e0720a2c3a001745655a4426a0a3123387c83..7930a1f72911862777bdeb9d9f4f28bef0d92272 100644 (file)
@@ -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, '&lt;')
-      .replace(/>/g, '&gt;')
-      .replace(/"/g, '&quot;')
-      .replace(/'/g, '&apos;');
-  }
-}
+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 `<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"/>
@@ -74,8 +69,8 @@ module.exports = (ctx, label, message, accessibleText) => {
     <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>`;
 };