diff --git a/app/assets/javascripts/discourse/lib/click-track.js.es6 b/app/assets/javascripts/discourse/lib/click-track.js.es6 index 327ca311f74..c638cd73026 100644 --- a/app/assets/javascripts/discourse/lib/click-track.js.es6 +++ b/app/assets/javascripts/discourse/lib/click-track.js.es6 @@ -127,6 +127,17 @@ export default { const isInternal = DiscourseURL.isInternal(href); + const modifierLeftClicked = (e.ctrlKey || e.metaKey) && e.which === 1; + const middleClicked = e.which === 2; + const openExternalInNewTab = Discourse.User.currentProp( + "external_links_in_new_tab" + ); + + const openWindow = + modifierLeftClicked || + middleClicked || + (!isInternal && openExternalInNewTab); + // If we're on the same site, use the router and track via AJAX if (isInternal && !$link.hasClass("attachment")) { if (tracking) { @@ -140,21 +151,15 @@ export default { dataType: "html" }); } - DiscourseURL.routeTo(href); + if (openWindow) { + window.open(destUrl, "_blank").focus(); + } else { + DiscourseURL.routeTo(href); + } return false; } - const modifierLeftClicked = (e.ctrlKey || e.metaKey) && e.which === 1; - const middleClicked = e.which === 2; - const openExternalInNewTab = Discourse.User.currentProp( - "external_links_in_new_tab" - ); - - if ( - modifierLeftClicked || - middleClicked || - (!isInternal && openExternalInNewTab) - ) { + if (openWindow) { window.open(destUrl, "_blank").focus(); } else { DiscourseURL.redirectTo(destUrl); diff --git a/test/javascripts/lib/click-track-test.js.es6 b/test/javascripts/lib/click-track-test.js.es6 index 146d23df2bb..c1aa8b1e56e 100644 --- a/test/javascripts/lib/click-track-test.js.es6 +++ b/test/javascripts/lib/click-track-test.js.es6 @@ -81,6 +81,26 @@ QUnit.test("does not track clicks to internal links in quotes", assert => { ); }); +QUnit.test("can open links inside quotes in new window", assert => { + sandbox.stub(DiscourseURL, "routeTo"); + sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com"); + + track( + $.Event("click", { + currentTarget: fixture(".quote a:first-child")[0], + ctrlKey: true, + which: 1 + }) + ); + + assert.ok( + window.open.calledWith( + "https://discuss.domain.com/t/welcome-to-meta-discourse-org/1/30", + "_blank" + ) + ); +}); + QUnit.test("does not track clicks to external links in quotes", assert => { track(generateClickEventOn(".quote a:last-child")); assert.ok(DiscourseURL.redirectTo.calledWith("https://google.com"));