From 3b81c2d470ef0c03acf8500ffe7d96fc1ab07fbd Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 8 Dec 2020 14:36:52 -0500 Subject: [PATCH] FIX: Normalize links by converting them to lower case The server side does this so the same link on the client side with any upper case letters does not warn as a duplicate. --- .../discourse/app/lib/link-lookup.js | 11 ++++- .../tests/unit/lib/link-lookup-test.js | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/discourse/tests/unit/lib/link-lookup-test.js diff --git a/app/assets/javascripts/discourse/app/lib/link-lookup.js b/app/assets/javascripts/discourse/app/lib/link-lookup.js index 0d742da85f6..84bf8d12a07 100644 --- a/app/assets/javascripts/discourse/app/lib/link-lookup.js +++ b/app/assets/javascripts/discourse/app/lib/link-lookup.js @@ -1,6 +1,10 @@ -const _warned = {}; +let _warned = {}; const NO_RESULT = [false, null]; +export function reset() { + _warned = {}; +} + export default class LinkLookup { constructor(links) { this._links = links; @@ -11,7 +15,10 @@ export default class LinkLookup { return NO_RESULT; } - const normalized = href.replace(/^https?:\/\//, "").replace(/\/$/, ""); + const normalized = href + .toLowerCase() + .replace(/^https?:\/\//, "") + .replace(/\/$/, ""); if (_warned[normalized]) { return NO_RESULT; } diff --git a/app/assets/javascripts/discourse/tests/unit/lib/link-lookup-test.js b/app/assets/javascripts/discourse/tests/unit/lib/link-lookup-test.js new file mode 100644 index 00000000000..ee3dd69a9c9 --- /dev/null +++ b/app/assets/javascripts/discourse/tests/unit/lib/link-lookup-test.js @@ -0,0 +1,48 @@ +import LinkLookup, { reset } from "discourse/lib/link-lookup"; +import { module, test } from "qunit"; +import Post from "discourse/models/post"; + +module("Unit | Utility | link-lookup", function (hooks) { + hooks.afterEach(() => reset()); + hooks.beforeEach(function () { + this.post = Post.create(); + this.linkLookup = new LinkLookup({ + "en.wikipedia.org/wiki/handheld_game_console": { + post_number: 1, + }, + }); + }); + + test("works with https", function (assert) { + assert.ok( + this.linkLookup.check( + this.post, + "https://en.wikipedia.org/wiki/handheld_game_console" + )[0] + ); + }); + test("works with http", function (assert) { + assert.ok( + this.linkLookup.check( + this.post, + "http://en.wikipedia.org/wiki/handheld_game_console" + )[0] + ); + }); + test("works with trailing slash", function (assert) { + assert.ok( + this.linkLookup.check( + this.post, + "https://en.wikipedia.org/wiki/handheld_game_console/" + )[0] + ); + }); + test("works with uppercase characters", function (assert) { + assert.ok( + this.linkLookup.check( + this.post, + "https://en.wikipedia.org/wiki/Handheld_game_console" + )[0] + ); + }); +});