Use template literals in parse
Also make it output double quotes for consistency with web stuff.
This commit is contained in:
parent
5b4c00d8ca
commit
90f4a94bb2
@ -7,25 +7,25 @@ const findLinks = require("./ircmessageparser/findLinks");
|
|||||||
const merge = require("./ircmessageparser/merge");
|
const merge = require("./ircmessageparser/merge");
|
||||||
|
|
||||||
function createFragment(fragment) {
|
function createFragment(fragment) {
|
||||||
let className = "";
|
let classes = [];
|
||||||
if (fragment.bold) {
|
if (fragment.bold) {
|
||||||
className += " irc-bold";
|
classes.push("irc-bold");
|
||||||
}
|
}
|
||||||
if (fragment.textColor !== undefined) {
|
if (fragment.textColor !== undefined) {
|
||||||
className += " irc-fg" + fragment.textColor;
|
classes.push("irc-fg" + fragment.textColor);
|
||||||
}
|
}
|
||||||
if (fragment.bgColor !== undefined) {
|
if (fragment.bgColor !== undefined) {
|
||||||
className += " irc-bg" + fragment.bgColor;
|
classes.push("irc-bg" + fragment.bgColor);
|
||||||
}
|
}
|
||||||
if (fragment.italic) {
|
if (fragment.italic) {
|
||||||
className += " irc-italic";
|
classes.push("irc-italic");
|
||||||
}
|
}
|
||||||
if (fragment.underline) {
|
if (fragment.underline) {
|
||||||
className += " irc-underline";
|
classes.push("irc-underline");
|
||||||
}
|
}
|
||||||
const escapedText = Handlebars.Utils.escapeExpression(fragment.text);
|
const escapedText = Handlebars.Utils.escapeExpression(fragment.text);
|
||||||
if (className) {
|
if (classes.length) {
|
||||||
return "<span class='" + className.trim() + "'>" + escapedText + "</span>";
|
return `<span class="${classes.join(" ")}">${escapedText}</span>`;
|
||||||
}
|
}
|
||||||
return escapedText;
|
return escapedText;
|
||||||
}
|
}
|
||||||
@ -49,16 +49,10 @@ module.exports = function parse(text) {
|
|||||||
|
|
||||||
if (textPart.link) {
|
if (textPart.link) {
|
||||||
const escapedLink = Handlebars.Utils.escapeExpression(textPart.link);
|
const escapedLink = Handlebars.Utils.escapeExpression(textPart.link);
|
||||||
return (
|
return `<a href="${escapedLink}" target="_blank" rel="noopener">${fragments}</a>`;
|
||||||
"<a href='" + escapedLink + "' target='_blank' rel='noopener'>" +
|
|
||||||
fragments +
|
|
||||||
"</a>");
|
|
||||||
} else if (textPart.channel) {
|
} else if (textPart.channel) {
|
||||||
const escapedChannel = Handlebars.Utils.escapeExpression(textPart.channel);
|
const escapedChannel = Handlebars.Utils.escapeExpression(textPart.channel);
|
||||||
return (
|
return `<span class="inline-channel" role="button" tabindex="0" data-chan="${escapedChannel}">${fragments}</span>`;
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='" + escapedChannel + "'>" +
|
|
||||||
fragments +
|
|
||||||
"</span>");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fragments;
|
return fragments;
|
||||||
|
@ -10,7 +10,7 @@ describe("parse Handlebars helper", () => {
|
|||||||
expected: "<img onerror='location.href="//youtube.com"'>"
|
expected: "<img onerror='location.href="//youtube.com"'>"
|
||||||
}, {
|
}, {
|
||||||
input: "#&\">bug",
|
input: "#&\">bug",
|
||||||
expected: "<span class='inline-channel' role='button' tabindex='0' data-chan='#&">bug'>#&">bug</span>"
|
expected: "<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#&">bug\">#&">bug</span>"
|
||||||
}];
|
}];
|
||||||
|
|
||||||
const actual = testCases.map(testCase => parse(testCase.input));
|
const actual = testCases.map(testCase => parse(testCase.input));
|
||||||
@ -35,20 +35,20 @@ describe("parse Handlebars helper", () => {
|
|||||||
const testCases = [{
|
const testCases = [{
|
||||||
input: "irc://freenode.net/thelounge",
|
input: "irc://freenode.net/thelounge",
|
||||||
expected:
|
expected:
|
||||||
"<a href='irc://freenode.net/thelounge' target='_blank' rel='noopener'>" +
|
"<a href=\"irc://freenode.net/thelounge\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"irc://freenode.net/thelounge" +
|
"irc://freenode.net/thelounge" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}, {
|
}, {
|
||||||
input: "www.nooooooooooooooo.com",
|
input: "www.nooooooooooooooo.com",
|
||||||
expected:
|
expected:
|
||||||
"<a href='http://www.nooooooooooooooo.com' target='_blank' rel='noopener'>" +
|
"<a href=\"http://www.nooooooooooooooo.com\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"www.nooooooooooooooo.com" +
|
"www.nooooooooooooooo.com" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}, {
|
}, {
|
||||||
input: "look at https://thelounge.github.io/ for more information",
|
input: "look at https://thelounge.github.io/ for more information",
|
||||||
expected:
|
expected:
|
||||||
"look at " +
|
"look at " +
|
||||||
"<a href='https://thelounge.github.io/' target='_blank' rel='noopener'>" +
|
"<a href=\"https://thelounge.github.io/\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"https://thelounge.github.io/" +
|
"https://thelounge.github.io/" +
|
||||||
"</a>" +
|
"</a>" +
|
||||||
" for more information",
|
" for more information",
|
||||||
@ -56,14 +56,14 @@ describe("parse Handlebars helper", () => {
|
|||||||
input: "use www.duckduckgo.com for privacy reasons",
|
input: "use www.duckduckgo.com for privacy reasons",
|
||||||
expected:
|
expected:
|
||||||
"use " +
|
"use " +
|
||||||
"<a href='http://www.duckduckgo.com' target='_blank' rel='noopener'>" +
|
"<a href=\"http://www.duckduckgo.com\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"www.duckduckgo.com" +
|
"www.duckduckgo.com" +
|
||||||
"</a>" +
|
"</a>" +
|
||||||
" for privacy reasons"
|
" for privacy reasons"
|
||||||
}, {
|
}, {
|
||||||
input: "svn+ssh://example.org",
|
input: "svn+ssh://example.org",
|
||||||
expected:
|
expected:
|
||||||
"<a href='svn+ssh://example.org' target='_blank' rel='noopener'>" +
|
"<a href=\"svn+ssh://example.org\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"svn+ssh://example.org" +
|
"svn+ssh://example.org" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}];
|
}];
|
||||||
@ -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";
|
"bonuspunkt: your URL parser misparses this URL: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx";
|
||||||
const correctResult =
|
const correctResult =
|
||||||
"bonuspunkt: your URL parser misparses this URL: " +
|
"bonuspunkt: your URL parser misparses this URL: " +
|
||||||
"<a href='https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx' target='_blank' rel='noopener'>" +
|
"<a href=\"https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx" +
|
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989(v=vs.85).aspx" +
|
||||||
"</a>";
|
"</a>";
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ describe("parse Handlebars helper", () => {
|
|||||||
input: "<https://theos.kyriasis.com/~kyrias/stats/archlinux.html>",
|
input: "<https://theos.kyriasis.com/~kyrias/stats/archlinux.html>",
|
||||||
expected:
|
expected:
|
||||||
"<" +
|
"<" +
|
||||||
"<a href='https://theos.kyriasis.com/~kyrias/stats/archlinux.html' target='_blank' rel='noopener'>" +
|
"<a href=\"https://theos.kyriasis.com/~kyrias/stats/archlinux.html\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"https://theos.kyriasis.com/~kyrias/stats/archlinux.html" +
|
"https://theos.kyriasis.com/~kyrias/stats/archlinux.html" +
|
||||||
"</a>" +
|
"</a>" +
|
||||||
">"
|
">"
|
||||||
@ -101,20 +101,20 @@ describe("parse Handlebars helper", () => {
|
|||||||
input: "abc (www.example.com)",
|
input: "abc (www.example.com)",
|
||||||
expected:
|
expected:
|
||||||
"abc (" +
|
"abc (" +
|
||||||
"<a href='http://www.example.com' target='_blank' rel='noopener'>" +
|
"<a href=\"http://www.example.com\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"www.example.com" +
|
"www.example.com" +
|
||||||
"</a>" +
|
"</a>" +
|
||||||
")"
|
")"
|
||||||
}, {
|
}, {
|
||||||
input: "http://example.com/Test_(Page)",
|
input: "http://example.com/Test_(Page)",
|
||||||
expected:
|
expected:
|
||||||
"<a href='http://example.com/Test_(Page)' target='_blank' rel='noopener'>" +
|
"<a href=\"http://example.com/Test_(Page)\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"http://example.com/Test_(Page)" +
|
"http://example.com/Test_(Page)" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}, {
|
}, {
|
||||||
input: "www.example.com/Test_(Page)",
|
input: "www.example.com/Test_(Page)",
|
||||||
expected:
|
expected:
|
||||||
"<a href='http://www.example.com/Test_(Page)' target='_blank' rel='noopener'>" +
|
"<a href=\"http://www.example.com/Test_(Page)\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"www.example.com/Test_(Page)" +
|
"www.example.com/Test_(Page)" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}];
|
}];
|
||||||
@ -144,40 +144,40 @@ describe("parse Handlebars helper", () => {
|
|||||||
const testCases = [{
|
const testCases = [{
|
||||||
input: "#a",
|
input: "#a",
|
||||||
expected:
|
expected:
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#a'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#a\">" +
|
||||||
"#a" +
|
"#a" +
|
||||||
"</span>"
|
"</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "#test",
|
input: "#test",
|
||||||
expected:
|
expected:
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#test'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#test\">" +
|
||||||
"#test" +
|
"#test" +
|
||||||
"</span>"
|
"</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "#äöü",
|
input: "#äöü",
|
||||||
expected:
|
expected:
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#äöü'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#äöü\">" +
|
||||||
"#äöü" +
|
"#äöü" +
|
||||||
"</span>"
|
"</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "inline #channel text",
|
input: "inline #channel text",
|
||||||
expected:
|
expected:
|
||||||
"inline " +
|
"inline " +
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#channel'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#channel\">" +
|
||||||
"#channel" +
|
"#channel" +
|
||||||
"</span>" +
|
"</span>" +
|
||||||
" text"
|
" text"
|
||||||
}, {
|
}, {
|
||||||
input: "#1,000",
|
input: "#1,000",
|
||||||
expected:
|
expected:
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#1,000'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#1,000\">" +
|
||||||
"#1,000" +
|
"#1,000" +
|
||||||
"</span>"
|
"</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "@#a",
|
input: "@#a",
|
||||||
expected:
|
expected:
|
||||||
"@" +
|
"@" +
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#a'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#a\">" +
|
||||||
"#a" +
|
"#a" +
|
||||||
"</span>"
|
"</span>"
|
||||||
}];
|
}];
|
||||||
@ -206,35 +206,35 @@ describe("parse Handlebars helper", () => {
|
|||||||
it("should style like mirc", () => {
|
it("should style like mirc", () => {
|
||||||
const testCases = [{
|
const testCases = [{
|
||||||
input: "\x02bold",
|
input: "\x02bold",
|
||||||
expected: "<span class='irc-bold'>bold</span>"
|
expected: "<span class=\"irc-bold\">bold</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "\x038yellowText",
|
input: "\x038yellowText",
|
||||||
expected: "<span class='irc-fg8'>yellowText</span>"
|
expected: "<span class=\"irc-fg8\">yellowText</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "\x030,0white,white",
|
input: "\x030,0white,white",
|
||||||
expected: "<span class='irc-fg0 irc-bg0'>white,white</span>"
|
expected: "<span class=\"irc-fg0 irc-bg0\">white,white</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "\x034,8yellowBGredText",
|
input: "\x034,8yellowBGredText",
|
||||||
expected: "<span class='irc-fg4 irc-bg8'>yellowBGredText</span>"
|
expected: "<span class=\"irc-fg4 irc-bg8\">yellowBGredText</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "\x1ditalic",
|
input: "\x1ditalic",
|
||||||
expected: "<span class='irc-italic'>italic</span>"
|
expected: "<span class=\"irc-italic\">italic</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "\x1funderline",
|
input: "\x1funderline",
|
||||||
expected: "<span class='irc-underline'>underline</span>"
|
expected: "<span class=\"irc-underline\">underline</span>"
|
||||||
}, {
|
}, {
|
||||||
input: "\x02bold\x038yellow\x02nonBold\x03default",
|
input: "\x02bold\x038yellow\x02nonBold\x03default",
|
||||||
expected:
|
expected:
|
||||||
"<span class='irc-bold'>bold</span>" +
|
"<span class=\"irc-bold\">bold</span>" +
|
||||||
"<span class='irc-bold irc-fg8'>yellow</span>" +
|
"<span class=\"irc-bold irc-fg8\">yellow</span>" +
|
||||||
"<span class='irc-fg8'>nonBold</span>" +
|
"<span class=\"irc-fg8\">nonBold</span>" +
|
||||||
"default"
|
"default"
|
||||||
}, {
|
}, {
|
||||||
input: "\x02bold\x02 \x02bold\x02",
|
input: "\x02bold\x02 \x02bold\x02",
|
||||||
expected:
|
expected:
|
||||||
"<span class='irc-bold'>bold</span>" +
|
"<span class=\"irc-bold\">bold</span>" +
|
||||||
" " +
|
" " +
|
||||||
"<span class='irc-bold'>bold</span>"
|
"<span class=\"irc-bold\">bold</span>"
|
||||||
}];
|
}];
|
||||||
|
|
||||||
const actual = testCases.map(testCase => parse(testCase.input));
|
const actual = testCases.map(testCase => parse(testCase.input));
|
||||||
@ -247,19 +247,19 @@ describe("parse Handlebars helper", () => {
|
|||||||
const testCases = [{
|
const testCases = [{
|
||||||
input: "\x02irc\x0f://\x1dfreenode.net\x0f/\x034,8thelounge",
|
input: "\x02irc\x0f://\x1dfreenode.net\x0f/\x034,8thelounge",
|
||||||
expected:
|
expected:
|
||||||
"<a href='irc://freenode.net/thelounge' target='_blank' rel='noopener'>" +
|
"<a href=\"irc://freenode.net/thelounge\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"<span class='irc-bold'>irc</span>" +
|
"<span class=\"irc-bold\">irc</span>" +
|
||||||
"://" +
|
"://" +
|
||||||
"<span class='irc-italic'>freenode.net</span>" +
|
"<span class=\"irc-italic\">freenode.net</span>" +
|
||||||
"/" +
|
"/" +
|
||||||
"<span class='irc-fg4 irc-bg8'>thelounge</span>" +
|
"<span class=\"irc-fg4 irc-bg8\">thelounge</span>" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}, {
|
}, {
|
||||||
input: "\x02#\x038,9thelounge",
|
input: "\x02#\x038,9thelounge",
|
||||||
expected:
|
expected:
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#thelounge'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#thelounge\">" +
|
||||||
"<span class='irc-bold'>#</span>" +
|
"<span class=\"irc-bold\">#</span>" +
|
||||||
"<span class='irc-bold irc-fg8 irc-bg9'>thelounge</span>" +
|
"<span class=\"irc-bold irc-fg8 irc-bg9\">thelounge</span>" +
|
||||||
"</span>"
|
"</span>"
|
||||||
}];
|
}];
|
||||||
|
|
||||||
@ -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",
|
input: "test \x0312#\x0312\x0312\"te\x0312st\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312a",
|
||||||
expected:
|
expected:
|
||||||
"test " +
|
"test " +
|
||||||
"<span class='inline-channel' role='button' tabindex='0' data-chan='#"testa'>" +
|
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#"testa\">" +
|
||||||
"<span class='irc-fg12'>#"testa</span>" +
|
"<span class=\"irc-fg12\">#"testa</span>" +
|
||||||
"</span>"
|
"</span>"
|
||||||
}];
|
}];
|
||||||
|
|
||||||
@ -290,14 +290,14 @@ describe("parse Handlebars helper", () => {
|
|||||||
input: "like..http://example.com",
|
input: "like..http://example.com",
|
||||||
expected:
|
expected:
|
||||||
"like.." +
|
"like.." +
|
||||||
"<a href='http://example.com' target='_blank' rel='noopener'>" +
|
"<a href=\"http://example.com\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"http://example.com" +
|
"http://example.com" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}, {
|
}, {
|
||||||
input: "like..HTTP://example.com",
|
input: "like..HTTP://example.com",
|
||||||
expected:
|
expected:
|
||||||
"like.." +
|
"like.." +
|
||||||
"<a href='HTTP://example.com' target='_blank' rel='noopener'>" +
|
"<a href=\"HTTP://example.com\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"HTTP://example.com" +
|
"HTTP://example.com" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}];
|
}];
|
||||||
@ -313,7 +313,7 @@ describe("parse Handlebars helper", () => {
|
|||||||
input: "http://example.com/#hash",
|
input: "http://example.com/#hash",
|
||||||
expected:
|
expected:
|
||||||
"" +
|
"" +
|
||||||
"<a href='http://example.com/#hash' target='_blank' rel='noopener'>" +
|
"<a href=\"http://example.com/#hash\" target=\"_blank\" rel=\"noopener\">" +
|
||||||
"http://example.com/#hash" +
|
"http://example.com/#hash" +
|
||||||
"</a>"
|
"</a>"
|
||||||
}];
|
}];
|
||||||
@ -329,8 +329,8 @@ describe("parse Handlebars helper", () => {
|
|||||||
const actual = parse(input);
|
const actual = parse(input);
|
||||||
|
|
||||||
expect(actual).to.equal(
|
expect(actual).to.equal(
|
||||||
"Url: <a href='http://example.com/path' target='_blank' rel='noopener'>http://example.com/path</a> " +
|
"Url: <a href=\"http://example.com/path\" target=\"_blank\" rel=\"noopener\">http://example.com/path</a> " +
|
||||||
"Channel: <span class='inline-channel' role='button' tabindex='0' data-chan='##channel'>##channel</span>"
|
"Channel: <span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"##channel\">##channel</span>"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user