Merge pull request #1747 from thelounge/astorije/fix-parse

Fix duplicate text generated when sending channel and link together
This commit is contained in:
Jérémie Astori 2017-11-26 17:23:32 -05:00 committed by GitHub
commit 823ed0153f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -2,6 +2,7 @@
const Handlebars = require("handlebars/runtime"); const Handlebars = require("handlebars/runtime");
const parseStyle = require("./ircmessageparser/parseStyle"); const parseStyle = require("./ircmessageparser/parseStyle");
const anyIntersection = require("./ircmessageparser/anyIntersection");
const findChannels = require("./ircmessageparser/findChannels"); const findChannels = require("./ircmessageparser/findChannels");
const findLinks = require("./ircmessageparser/findLinks"); const findLinks = require("./ircmessageparser/findLinks");
const findEmoji = require("./ircmessageparser/findEmoji"); const findEmoji = require("./ircmessageparser/findEmoji");
@ -66,7 +67,15 @@ module.exports = function parse(text) {
const parts = channelParts const parts = channelParts
.concat(linkParts) .concat(linkParts)
.concat(emojiParts) .concat(emojiParts)
.sort((a, b) => a.start - b.start); .sort((a, b) => a.start - b.start || b.end - a.end)
.reduce((prev, curr) => {
const intersection = prev.some((p) => anyIntersection(p, curr));
if (intersection) {
return prev;
}
return prev.concat([curr]);
}, []);
// Merge the styling information with the channels / URLs / text objects and // Merge the styling information with the channels / URLs / text objects and
// generate HTML strings with the resulting fragments // generate HTML strings with the resulting fragments

View file

@ -312,7 +312,6 @@ describe("parse Handlebars helper", () => {
const testCases = [{ const testCases = [{
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>",
@ -333,4 +332,15 @@ describe("parse Handlebars helper", () => {
"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>"
); );
}); });
it("should handle overlapping parts by using first starting", () => {
const input = "#test-https://example.com";
const actual = parse(input);
expect(actual).to.equal(
"<span class=\"inline-channel\" role=\"button\" tabindex=\"0\" data-chan=\"#test-https://example.com\">" +
"#test-https://example.com" +
"</span>"
);
});
}); });