Skip to content

A platform for community discussion. Free, open, simple.

FIX: Handle SVG links in click tracking

Régis Hanol85a7572b7a37
diff --git a/frontend/discourse/app/lib/click-track.js b/frontend/discourse/app/lib/click-track.js
index a3ec07c81b2..27a40e472a9 100644
--- a/frontend/discourse/app/lib/click-track.js
+++ b/frontend/discourse/app/lib/click-track.js
@@ -60,7 +60,11 @@ export function shouldOpenInNewTab(href) {
}
export function openLinkInNewTab(event, link) {
- let href = (link.href || link.dataset.href || "").trim();
+ let href = (
+ (link.href?.baseVal ?? link.href) ||
+ link.dataset.href ||
+ ""
+ ).trim();
if (href === "") {
return;
}
diff --git a/frontend/discourse/app/lib/intercept-click.js b/frontend/discourse/app/lib/intercept-click.js
index 0a93bdfd644..20f056ad282 100644
--- a/frontend/discourse/app/lib/intercept-click.js
+++ b/frontend/discourse/app/lib/intercept-click.js
@@ -10,7 +10,7 @@ export function wantsNewWindow(e, link = e.currentTarget) {
e.metaKey ||
e.ctrlKey ||
(e.button && e.button !== MOUSE_EVENT_PRIMARY_BUTTON_ID) ||
- link?.target === "_blank"
+ link?.getAttribute?.("target") === "_blank"
);
}
diff --git a/frontend/discourse/tests/unit/lib/click-track-test.js b/frontend/discourse/tests/unit/lib/click-track-test.js
index 8163c9d2379..1febedc7d2a 100644
--- a/frontend/discourse/tests/unit/lib/click-track-test.js
+++ b/frontend/discourse/tests/unit/lib/click-track-test.js
@@ -75,6 +75,10 @@ module("Unit | Utility | click-track", function (hooks) {
<a class="prefix-url" href="/forum/thing">prefix link</a>
<a class="abs-prefix-url" href="${window.location.origin}/forum/thing">prefix link</a>
<a class="diff-prefix-url" href="/thing">diff prefix link</a>
+ <svg>
+ <a id="svg-link" href="http://www.google.ch"><text>google.ch</text></a>
+ <a id="svg-link-blank" href="http://www.google.ch" target="_blank"><text>google.ch</text></a>
+ </svg>
</article>
</div>`;
});
@@ -212,6 +216,17 @@ module("Unit | Utility | click-track", function (hooks) {
assert.true(window.open.calledWith("http://www.google.com/", "_blank"));
});
+ test("opens SVG links in a new tab when external links open in new tab", async function (assert) {
+ User.currentProp("user_option.external_links_in_new_tab", true);
+
+ assert.false(track(generateClickEventOn("#svg-link")));
+ assert.true(window.open.calledWith("http://www.google.ch", "_blank"));
+ });
+
+ test("lets the browser open SVG links with target=_blank", async function (assert) {
+ assert.true(track(generateClickEventOn("#svg-link-blank")));
+ });
+
test("does not track clicks on lightboxes", async function (assert) {
assert.false(track(generateClickEventOn(".lightbox")));
});