From 90f4a94bb246bd91a0c2d2e2e54f717d16f8ba32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Tue, 18 Apr 2017 00:53:12 -0400 Subject: [PATCH] Use template literals in parse Also make it output double quotes for consistency with web stuff. --- client/js/libs/handlebars/parse.js | 26 +++----- test/client/js/libs/handlebars/parse.js | 84 ++++++++++++------------- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/client/js/libs/handlebars/parse.js b/client/js/libs/handlebars/parse.js index fa21b898..7e9aebb8 100644 --- a/client/js/libs/handlebars/parse.js +++ b/client/js/libs/handlebars/parse.js @@ -7,25 +7,25 @@ const findLinks = require("./ircmessageparser/findLinks"); const merge = require("./ircmessageparser/merge"); function createFragment(fragment) { - let className = ""; + let classes = []; if (fragment.bold) { - className += " irc-bold"; + classes.push("irc-bold"); } if (fragment.textColor !== undefined) { - className += " irc-fg" + fragment.textColor; + classes.push("irc-fg" + fragment.textColor); } if (fragment.bgColor !== undefined) { - className += " irc-bg" + fragment.bgColor; + classes.push("irc-bg" + fragment.bgColor); } if (fragment.italic) { - className += " irc-italic"; + classes.push("irc-italic"); } if (fragment.underline) { - className += " irc-underline"; + classes.push("irc-underline"); } const escapedText = Handlebars.Utils.escapeExpression(fragment.text); - if (className) { - return "" + escapedText + ""; + if (classes.length) { + return `${escapedText}`; } return escapedText; } @@ -49,16 +49,10 @@ module.exports = function parse(text) { if (textPart.link) { const escapedLink = Handlebars.Utils.escapeExpression(textPart.link); - return ( - "" + - fragments + - ""); + return `${fragments}`; } else if (textPart.channel) { const escapedChannel = Handlebars.Utils.escapeExpression(textPart.channel); - return ( - "" + - fragments + - ""); + return `${fragments}`; } return fragments; diff --git a/test/client/js/libs/handlebars/parse.js b/test/client/js/libs/handlebars/parse.js index 7d1e025a..d3737e98 100644 --- a/test/client/js/libs/handlebars/parse.js +++ b/test/client/js/libs/handlebars/parse.js @@ -10,7 +10,7 @@ describe("parse Handlebars helper", () => { expected: "<img onerror='location.href="//youtube.com"'>" }, { input: "#&\">bug", - expected: "#&">bug" + expected: "#&">bug" }]; const actual = testCases.map(testCase => parse(testCase.input)); @@ -35,20 +35,20 @@ describe("parse Handlebars helper", () => { const testCases = [{ input: "irc://freenode.net/thelounge", expected: - "" + + "" + "irc://freenode.net/thelounge" + "" }, { input: "www.nooooooooooooooo.com", expected: - "" + + "" + "www.nooooooooooooooo.com" + "" }, { input: "look at https://thelounge.github.io/ for more information", expected: "look at " + - "" + + "" + "https://thelounge.github.io/" + "" + " for more information", @@ -56,14 +56,14 @@ describe("parse Handlebars helper", () => { input: "use www.duckduckgo.com for privacy reasons", expected: "use " + - "" + + "" + "www.duckduckgo.com" + "" + " for privacy reasons" }, { input: "svn+ssh://example.org", expected: - "" + + "" + "svn+ssh://example.org" + "" }]; @@ -79,7 +79,7 @@ describe("parse Handlebars helper", () => { "bonuspunkt: your URL parser misparses this URL: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx"; const correctResult = "bonuspunkt: your URL parser misparses this URL: " + - "" + + "" + "https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx" + ""; @@ -93,7 +93,7 @@ describe("parse Handlebars helper", () => { input: "", expected: "<" + - "" + + "" + "https://theos.kyriasis.com/~kyrias/stats/archlinux.html" + "" + ">" @@ -101,20 +101,20 @@ describe("parse Handlebars helper", () => { input: "abc (www.example.com)", expected: "abc (" + - "" + + "" + "www.example.com" + "" + ")" }, { input: "http://example.com/Test_(Page)", expected: - "" + + "" + "http://example.com/Test_(Page)" + "" }, { input: "www.example.com/Test_(Page)", expected: - "" + + "" + "www.example.com/Test_(Page)" + "" }]; @@ -144,40 +144,40 @@ describe("parse Handlebars helper", () => { const testCases = [{ input: "#a", expected: - "" + + "" + "#a" + "" }, { input: "#test", expected: - "" + + "" + "#test" + "" }, { input: "#äöü", expected: - "" + + "" + "#äöü" + "" }, { input: "inline #channel text", expected: "inline " + - "" + + "" + "#channel" + "" + " text" }, { input: "#1,000", expected: - "" + + "" + "#1,000" + "" }, { input: "@#a", expected: "@" + - "" + + "" + "#a" + "" }]; @@ -206,35 +206,35 @@ describe("parse Handlebars helper", () => { it("should style like mirc", () => { const testCases = [{ input: "\x02bold", - expected: "bold" + expected: "bold" }, { input: "\x038yellowText", - expected: "yellowText" + expected: "yellowText" }, { input: "\x030,0white,white", - expected: "white,white" + expected: "white,white" }, { input: "\x034,8yellowBGredText", - expected: "yellowBGredText" + expected: "yellowBGredText" }, { input: "\x1ditalic", - expected: "italic" + expected: "italic" }, { input: "\x1funderline", - expected: "underline" + expected: "underline" }, { input: "\x02bold\x038yellow\x02nonBold\x03default", expected: - "bold" + - "yellow" + - "nonBold" + + "bold" + + "yellow" + + "nonBold" + "default" }, { input: "\x02bold\x02 \x02bold\x02", expected: - "bold" + + "bold" + " " + - "bold" + "bold" }]; const actual = testCases.map(testCase => parse(testCase.input)); @@ -247,19 +247,19 @@ describe("parse Handlebars helper", () => { const testCases = [{ input: "\x02irc\x0f://\x1dfreenode.net\x0f/\x034,8thelounge", expected: - "" + - "irc" + + "" + + "irc" + "://" + - "freenode.net" + + "freenode.net" + "/" + - "thelounge" + + "thelounge" + "" }, { input: "\x02#\x038,9thelounge", expected: - "" + - "#" + - "thelounge" + + "" + + "#" + + "thelounge" + "" }]; @@ -274,8 +274,8 @@ describe("parse Handlebars helper", () => { input: "test \x0312#\x0312\x0312\"te\x0312st\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312a", expected: "test " + - "" + - "#"testa" + + "" + + "#"testa" + "" }]; @@ -290,14 +290,14 @@ describe("parse Handlebars helper", () => { input: "like..http://example.com", expected: "like.." + - "" + + "" + "http://example.com" + "" }, { input: "like..HTTP://example.com", expected: "like.." + - "" + + "" + "HTTP://example.com" + "" }]; @@ -313,7 +313,7 @@ describe("parse Handlebars helper", () => { input: "http://example.com/#hash", expected: "" + - "" + + "" + "http://example.com/#hash" + "" }]; @@ -329,8 +329,8 @@ describe("parse Handlebars helper", () => { const actual = parse(input); expect(actual).to.equal( - "Url: http://example.com/path " + - "Channel: ##channel" + "Url: http://example.com/path " + + "Channel: ##channel" ); }); });