diff --git a/app/assets/javascripts/discourse/app/components/bookmark-list.js b/app/assets/javascripts/discourse/app/components/bookmark-list.js index 6ea4c9cb634..15dd1f42cab 100644 --- a/app/assets/javascripts/discourse/app/components/bookmark-list.js +++ b/app/assets/javascripts/discourse/app/components/bookmark-list.js @@ -75,10 +75,9 @@ export default Component.extend(Scrolling, { @action screenExcerptForExternalLink(event) { - if (event.target && event.target.tagName === "A") { - let link = event.target; - if (shouldOpenInNewTab(link.href)) { - openLinkInNewTab(link); + if (event?.target?.tagName === "A") { + if (shouldOpenInNewTab(event.target.href)) { + openLinkInNewTab(event, event.target); } } }, diff --git a/app/assets/javascripts/discourse/app/components/html-with-links.js b/app/assets/javascripts/discourse/app/components/html-with-links.js index a49b1dfcc90..9149668fb8e 100644 --- a/app/assets/javascripts/discourse/app/components/html-with-links.js +++ b/app/assets/javascripts/discourse/app/components/html-with-links.js @@ -8,7 +8,7 @@ export default Component.extend({ click(event) { if (event?.target?.tagName === "A") { if (shouldOpenInNewTab(event.target.href)) { - openLinkInNewTab(event.target); + openLinkInNewTab(event, event.target); } } }, diff --git a/app/assets/javascripts/discourse/app/lib/click-track.js b/app/assets/javascripts/discourse/app/lib/click-track.js index 52cadb5cff0..52b45ec9b4e 100644 --- a/app/assets/javascripts/discourse/app/lib/click-track.js +++ b/app/assets/javascripts/discourse/app/lib/click-track.js @@ -5,7 +5,6 @@ import User from "discourse/models/user"; import { ajax } from "discourse/lib/ajax"; import getURL, { samePrefix } from "discourse-common/lib/get-url"; import { isTesting } from "discourse-common/config/environment"; -import discourseLater from "discourse-common/lib/later"; import { selectedText } from "discourse/lib/utilities"; import { wantsNewWindow } from "discourse/lib/intercept-click"; import deprecated from "discourse-common/lib/deprecated"; @@ -59,7 +58,7 @@ export function shouldOpenInNewTab(href) { return !isInternal && openExternalInNewTab; } -export function openLinkInNewTab(link) { +export function openLinkInNewTab(event, link) { let href = (link.href || link.dataset.href || "").trim(); if (href === "") { return; @@ -69,23 +68,7 @@ export function openLinkInNewTab(link) { newWindow.opener = null; newWindow.focus(); - // Hack to prevent changing current window.location. - // e.preventDefault() does not work. - if (!link.dataset.href) { - link.classList.add("no-href"); - link.dataset.href = link.href; - link.dataset.autoRoute = true; - link.removeAttribute("href"); - - discourseLater(() => { - if (link) { - link.classList.remove("no-href"); - link.setAttribute("href", link.dataset.href); - delete link.dataset.href; - delete link.dataset.autoRoute; - } - }, 50); - } + event.preventDefault(); } export default { @@ -185,7 +168,7 @@ export default { if (!wantsNewWindow(e)) { if (shouldOpenInNewTab(href)) { - openLinkInNewTab(link); + openLinkInNewTab(e, link); } else { trackPromise.finally(() => { if (DiscourseURL.isInternal(href) && samePrefix(href)) { diff --git a/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js b/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js index bf53abb11f1..67e8ea605b5 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js @@ -3,7 +3,6 @@ import { module, skip, test } from "qunit"; import ClickTrack from "discourse/lib/click-track"; import DiscourseURL from "discourse/lib/url"; import User from "discourse/models/user"; -import discourseLater from "discourse-common/lib/later"; import pretender from "discourse/tests/helpers/create-pretender"; import sinon from "sinon"; import { setPrefix } from "discourse-common/lib/get-url"; @@ -191,36 +190,6 @@ module("Unit | Utility | click-track", function (hooks) { assert.ok(track(generateClickEventOn(".mailto"))); }); - test("removes the href and put it as a data attribute", async function (assert) { - User.currentProp("external_links_in_new_tab", true); - - assert.notOk(track(generateClickEventOn("a"))); - - let link = fixture("a"); - assert.ok(link.classList.contains("no-href")); - assert.strictEqual(link.dataset.href, "http://www.google.com/"); - assert.blank(link.getAttribute("href")); - assert.ok(link.dataset.autoRoute); - assert.ok(window.open.calledWith("http://www.google.com/", "_blank")); - }); - - test("restores the href after a while", async function (assert) { - assert.expect(2); - - assert.notOk(track(generateClickEventOn("a"))); - - assert.timeout(75); - - const done = assert.async(); - discourseLater(() => { - assert.strictEqual( - fixture("a").getAttribute("href"), - "http://www.google.com" - ); - done(); - }); - }); - function badgeClickCount(assert, id, expected) { track(generateClickEventOn(`#${id}`)); const badge = fixture(`#${id}`).querySelector("span.badge");