"use strict"; const expect = require("chai").expect; const parse = require("../../../../../client/js/libs/handlebars/parse"); describe("parse Handlebars helper", () => { it("should not introduce xss", () => { const testCases = [{ input: "", expected: "<img onerror='location.href="//youtube.com"'>", }, { input: "#&\">bug", expected: "#&">bug", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should skip control codes", () => { const testCases = [{ input: "text\x01with\x04control\x05codes", expected: "textwithcontrolcodes", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should find urls", () => { 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", }, { 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" + "", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("url with a dot parsed correctly", () => { const input = "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" + ""; const actual = parse(input); expect(actual).to.deep.equal(correctResult); }); it("should balance brackets", () => { const testCases = [{ input: "", expected: "<" + "" + "https://theos.kyriasis.com/~kyrias/stats/archlinux.html" + "" + ">", }, { 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)" + "", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should not find urls", () => { const testCases = [{ input: "text www. text", expected: "text www. text", }, { input: "http://.", expected: "http://.", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should find channels", () => { 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" + "", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should not find channels", () => { const testCases = [{ input: "hi#test", expected: "hi#test", }, { input: "#", expected: "#", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should style like mirc", () => { const testCases = [{ input: "\x02bold", expected: "bold", }, { input: "\x038yellowText", expected: "yellowText", }, { input: "\x030,0white,white", expected: "white,white", }, { input: "\x034,8yellowBGredText", expected: "yellowBGredText", }, { input: "\x1ditalic", expected: "italic", }, { input: "\x1funderline", expected: "underline", }, { input: "\x02bold\x038yellow\x02nonBold\x03default", expected: "bold" + "yellow" + "nonBold" + "default", }, { input: "\x02bold\x02 \x02bold\x02", expected: "bold" + " " + "bold", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should go bonkers like mirc", () => { const testCases = [{ input: "\x02irc\x0f://\x1dfreenode.net\x0f/\x034,8thelounge", expected: "" + "irc" + "://" + "freenode.net" + "/" + "thelounge" + "", }, { input: "\x02#\x038,9thelounge", expected: "" + "#" + "thelounge" + "", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should optimize generated html", () => { const testCases = [{ input: "test \x0312#\x0312\x0312\"te\x0312st\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312\x0312a", expected: "test " + "" + "#"testa" + "", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should trim common protocols", () => { const testCases = [{ input: "like..http://example.com", expected: "like.." + "" + "http://example.com" + "", }, { input: "like..HTTP://example.com", expected: "like.." + "" + "HTTP://example.com" + "", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should not find channel in fragment", () => { const testCases = [{ input: "http://example.com/#hash", expected: "" + "http://example.com/#hash" + "", }]; const actual = testCases.map((testCase) => parse(testCase.input)); const expected = testCases.map((testCase) => testCase.expected); expect(actual).to.deep.equal(expected); }); it("should not overlap parts", () => { const input = "Url: http://example.com/path Channel: ##channel"; const actual = parse(input); expect(actual).to.equal( "Url: http://example.com/path " + "Channel: ##channel" ); }); it("should handle overlapping parts by using first starting", () => { const input = "#test-https://example.com"; const actual = parse(input); expect(actual).to.equal( "" + "#test-https://example.com" + "" ); }); });