mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 14:12:10 +08:00
FIX: Avoid redirection when not leaving forum. (#6419)
* FIX: Avoid redirection when not leaving forum. * DEV: Updated click-track's tests.
This commit is contained in:

committed by
Régis Hanol

parent
b8a1196b6b
commit
6ad13e5ae9
@ -128,16 +128,18 @@ export default {
|
|||||||
const isInternal = DiscourseURL.isInternal(href);
|
const isInternal = DiscourseURL.isInternal(href);
|
||||||
|
|
||||||
// If we're on the same site, use the router and track via AJAX
|
// If we're on the same site, use the router and track via AJAX
|
||||||
if (tracking && isInternal && !$link.hasClass("attachment")) {
|
if (isInternal && !$link.hasClass("attachment")) {
|
||||||
ajax("/clicks/track", {
|
if (tracking) {
|
||||||
data: {
|
ajax("/clicks/track", {
|
||||||
url: href,
|
data: {
|
||||||
post_id: postId,
|
url: href,
|
||||||
topic_id: topicId,
|
post_id: postId,
|
||||||
redirect: false
|
topic_id: topicId,
|
||||||
},
|
redirect: false
|
||||||
dataType: "html"
|
},
|
||||||
});
|
dataType: "html"
|
||||||
|
});
|
||||||
|
}
|
||||||
DiscourseURL.routeTo(href);
|
DiscourseURL.routeTo(href);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -187,13 +187,7 @@ const DiscourseURL = Ember.Object.extend({
|
|||||||
const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, "");
|
const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, "");
|
||||||
const baseUri = Discourse.BaseUri;
|
const baseUri = Discourse.BaseUri;
|
||||||
|
|
||||||
// If we have a baseUri and an absolute URL, make sure the baseUri
|
if (!DiscourseURL.isInternal(path)) {
|
||||||
// is the same. Otherwise we could be switching forums.
|
|
||||||
if (
|
|
||||||
baseUri &&
|
|
||||||
path.indexOf("http") === 0 &&
|
|
||||||
pathname.indexOf(baseUri) !== 0
|
|
||||||
) {
|
|
||||||
return redirectTo(path);
|
return redirectTo(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,11 +201,6 @@ const DiscourseURL = Ember.Object.extend({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Protocol relative URLs
|
|
||||||
if (path.indexOf("//") === 0) {
|
|
||||||
return redirectTo(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scroll to the same page, different anchor
|
// Scroll to the same page, different anchor
|
||||||
const m = /^#(.+)$/.exec(path);
|
const m = /^#(.+)$/.exec(path);
|
||||||
if (m) {
|
if (m) {
|
||||||
|
@ -30,7 +30,8 @@ QUnit.module("lib:click-track", {
|
|||||||
<a class="hashtag" href="http://discuss.domain.com">#hashtag</a>
|
<a class="hashtag" href="http://discuss.domain.com">#hashtag</a>
|
||||||
<a class="mailto" href="mailto:foo@bar.com">email-me</a>
|
<a class="mailto" href="mailto:foo@bar.com">email-me</a>
|
||||||
<aside class="quote">
|
<aside class="quote">
|
||||||
<a class="inside-quote" href="http://discuss.domain.com">foobar</a>
|
<a href="https://discuss.domain.com/t/welcome-to-meta-discourse-org/1/30">foo</a>
|
||||||
|
<a href="https://google.com">bar</a>
|
||||||
</aside>
|
</aside>
|
||||||
</article>
|
</article>
|
||||||
</div>`
|
</div>`
|
||||||
@ -45,14 +46,14 @@ var generateClickEventOn = function(selector) {
|
|||||||
return $.Event("click", { currentTarget: fixture(selector)[0] });
|
return $.Event("click", { currentTarget: fixture(selector)[0] });
|
||||||
};
|
};
|
||||||
|
|
||||||
QUnit.test("does not track clicks on lightboxes", function(assert) {
|
QUnit.test("does not track clicks on lightboxes", assert => {
|
||||||
var clickEvent = generateClickEventOn(".lightbox");
|
var clickEvent = generateClickEventOn(".lightbox");
|
||||||
sandbox.stub(clickEvent, "preventDefault");
|
sandbox.stub(clickEvent, "preventDefault");
|
||||||
assert.ok(track(clickEvent));
|
assert.ok(track(clickEvent));
|
||||||
assert.ok(!clickEvent.preventDefault.calledOnce);
|
assert.ok(!clickEvent.preventDefault.calledOnce);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("it calls preventDefault when clicking on an a", function(assert) {
|
QUnit.test("it calls preventDefault when clicking on an a", assert => {
|
||||||
var clickEvent = generateClickEventOn("a");
|
var clickEvent = generateClickEventOn("a");
|
||||||
sandbox.stub(clickEvent, "preventDefault");
|
sandbox.stub(clickEvent, "preventDefault");
|
||||||
track(clickEvent);
|
track(clickEvent);
|
||||||
@ -60,28 +61,40 @@ QUnit.test("it calls preventDefault when clicking on an a", function(assert) {
|
|||||||
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("does not track clicks when forcibly disabled", function(assert) {
|
QUnit.test("does not track clicks when forcibly disabled", assert => {
|
||||||
assert.ok(track(generateClickEventOn(".no-track-link")));
|
assert.ok(track(generateClickEventOn(".no-track-link")));
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("does not track clicks on back buttons", function(assert) {
|
QUnit.test("does not track clicks on back buttons", assert => {
|
||||||
assert.ok(track(generateClickEventOn(".back")));
|
assert.ok(track(generateClickEventOn(".back")));
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("does not track clicks in quotes", function(assert) {
|
QUnit.test("does not track clicks to internal links in quotes", assert => {
|
||||||
track(generateClickEventOn(".inside-quote"));
|
sandbox.stub(DiscourseURL, "routeTo");
|
||||||
assert.ok(DiscourseURL.redirectTo.calledWith("http://discuss.domain.com"));
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
||||||
|
|
||||||
|
track(generateClickEventOn(".quote a:first-child"));
|
||||||
|
assert.ok(
|
||||||
|
DiscourseURL.routeTo.calledWith(
|
||||||
|
"https://discuss.domain.com/t/welcome-to-meta-discourse-org/1/30"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
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"));
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("does not track clicks on category badges", assert => {
|
QUnit.test("does not track clicks on category badges", assert => {
|
||||||
assert.ok(track(generateClickEventOn(".hashtag")));
|
assert.ok(track(generateClickEventOn(".hashtag")));
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("does not track clicks on mailto", function(assert) {
|
QUnit.test("does not track clicks on mailto", assert => {
|
||||||
assert.ok(track(generateClickEventOn(".mailto")));
|
assert.ok(track(generateClickEventOn(".mailto")));
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("removes the href and put it as a data attribute", function(assert) {
|
QUnit.test("removes the href and put it as a data attribute", assert => {
|
||||||
track(generateClickEventOn("a"));
|
track(generateClickEventOn("a"));
|
||||||
|
|
||||||
var $link = fixture("a").first();
|
var $link = fixture("a").first();
|
||||||
@ -92,7 +105,7 @@ QUnit.test("removes the href and put it as a data attribute", function(assert) {
|
|||||||
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
assert.ok(DiscourseURL.redirectTo.calledOnce);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTestDiscourse("restores the href after a while", function(assert) {
|
asyncTestDiscourse("restores the href after a while", assert => {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
track(generateClickEventOn("a"));
|
track(generateClickEventOn("a"));
|
||||||
@ -110,7 +123,7 @@ var badgeClickCount = function(assert, id, expected) {
|
|||||||
assert.equal(parseInt($badge.html(), 10), expected);
|
assert.equal(parseInt($badge.html(), 10), expected);
|
||||||
};
|
};
|
||||||
|
|
||||||
QUnit.test("does not update badge clicks on my own link", function(assert) {
|
QUnit.test("does not update badge clicks on my own link", assert => {
|
||||||
sandbox
|
sandbox
|
||||||
.stub(Discourse.User, "currentProp")
|
.stub(Discourse.User, "currentProp")
|
||||||
.withArgs("id")
|
.withArgs("id")
|
||||||
@ -118,7 +131,7 @@ QUnit.test("does not update badge clicks on my own link", function(assert) {
|
|||||||
badgeClickCount(assert, "with-badge", 1);
|
badgeClickCount(assert, "with-badge", 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("does not update badge clicks in my own post", function(assert) {
|
QUnit.test("does not update badge clicks in my own post", assert => {
|
||||||
sandbox
|
sandbox
|
||||||
.stub(Discourse.User, "currentProp")
|
.stub(Discourse.User, "currentProp")
|
||||||
.withArgs("id")
|
.withArgs("id")
|
||||||
@ -126,14 +139,14 @@ QUnit.test("does not update badge clicks in my own post", function(assert) {
|
|||||||
badgeClickCount(assert, "with-badge-but-not-mine", 1);
|
badgeClickCount(assert, "with-badge-but-not-mine", 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("updates badge counts correctly", function(assert) {
|
QUnit.test("updates badge counts correctly", assert => {
|
||||||
badgeClickCount(assert, "inside-onebox", 1);
|
badgeClickCount(assert, "inside-onebox", 1);
|
||||||
badgeClickCount(assert, "inside-onebox-forced", 2);
|
badgeClickCount(assert, "inside-onebox-forced", 2);
|
||||||
badgeClickCount(assert, "with-badge", 2);
|
badgeClickCount(assert, "with-badge", 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
var testOpenInANewTab = function(description, clickEventModifier) {
|
var testOpenInANewTab = function(description, clickEventModifier) {
|
||||||
test(description, function(assert) {
|
test(description, assert => {
|
||||||
var clickEvent = generateClickEventOn("a");
|
var clickEvent = generateClickEventOn("a");
|
||||||
clickEventModifier(clickEvent);
|
clickEventModifier(clickEvent);
|
||||||
sandbox.stub(clickEvent, "preventDefault");
|
sandbox.stub(clickEvent, "preventDefault");
|
||||||
@ -166,7 +179,7 @@ testOpenInANewTab("it opens in a new tab on middle click", function(
|
|||||||
clickEvent.button = 2;
|
clickEvent.button = 2;
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("tracks via AJAX if we're on the same site", function(assert) {
|
QUnit.test("tracks via AJAX if we're on the same site", assert => {
|
||||||
sandbox.stub(DiscourseURL, "routeTo");
|
sandbox.stub(DiscourseURL, "routeTo");
|
||||||
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
||||||
|
|
||||||
@ -174,7 +187,7 @@ QUnit.test("tracks via AJAX if we're on the same site", function(assert) {
|
|||||||
assert.ok(DiscourseURL.routeTo.calledOnce);
|
assert.ok(DiscourseURL.routeTo.calledOnce);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("does not track via AJAX for attachments", function(assert) {
|
QUnit.test("does not track via AJAX for attachments", assert => {
|
||||||
sandbox.stub(DiscourseURL, "routeTo");
|
sandbox.stub(DiscourseURL, "routeTo");
|
||||||
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user