Skip to content

Commit 9173a89

Browse files
Irene Ninathan-barrett
authored andcommitted
Bug 2004746 - Add URL normalization step for New Tab frecency based tile sorting. r=home-newtab-reviewers,nbarrett
Differential Revision: https://phabricator.services.mozilla.com/D275524
1 parent 2be29d8 commit 9173a89

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

browser/extensions/newtab/lib/TopSitesFeed.sys.mjs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,19 @@ export class TopSitesFeed {
14491449
);
14501450
}
14511451

1452+
normalizeUrl(url) {
1453+
let normalized = url;
1454+
if (normalized.startsWith("https://")) {
1455+
normalized = normalized.slice(8);
1456+
} else if (normalized.startsWith("http://")) {
1457+
normalized = normalized.slice(7);
1458+
}
1459+
if (normalized.startsWith("www.")) {
1460+
normalized = normalized.slice(4);
1461+
}
1462+
return normalized;
1463+
}
1464+
14521465
/**
14531466
* Build frecency-boosted spocs from a list of sponsor domains by checking Places history.
14541467
* Checks if domains exist in history, dedupes against organic topsites,
@@ -1481,9 +1494,13 @@ export class TopSitesFeed {
14811494

14821495
const candidates = [];
14831496
frecent.forEach(site => {
1497+
const normalizedSiteUrl = this.normalizeUrl(site.url);
1498+
14841499
for (const domainObj of sponsorsToCheck) {
1500+
const normalizedDomain = this.normalizeUrl(domainObj.domain);
1501+
14851502
if (
1486-
!site.url.startsWith(domainObj.domain) ||
1503+
!normalizedSiteUrl.startsWith(normalizedDomain) ||
14871504
lazy.NewTabUtils.blockedLinks.isBlocked({ url: domainObj.domain })
14881505
) {
14891506
continue;

browser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyRanking.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ function getTopSitesFeedForTest(
108108
return feed;
109109
}
110110

111+
// eslint-disable-next-line max-statements
111112
add_task(async function test_frecency_sponsored_topsites() {
112113
let sandbox = sinon.createSandbox();
113114
{
@@ -197,7 +198,7 @@ add_task(async function test_frecency_sponsored_topsites() {
197198

198199
sandbox.restore();
199200
}
200-
/*{
201+
{
201202
info(
202203
"TopSitesFeed.fetchFrecencyBoostedSpocs - " +
203204
"Should return a single match with a subdomain"
@@ -216,7 +217,7 @@ add_task(async function test_frecency_sponsored_topsites() {
216217
Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname1");
217218

218219
sandbox.restore();
219-
}*/
220+
}
220221
{
221222
info(
222223
"TopSitesFeed.fetchFrecencyBoostedSpocs - " +
@@ -264,6 +265,45 @@ add_task(async function test_frecency_sponsored_topsites() {
264265
Assert.equal(frecencyBoostedSpocs.length, 1);
265266
Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname4");
266267

268+
sandbox.restore();
269+
}
270+
{
271+
info(
272+
"TopSitesFeed.fetchFrecencyBoostedSpocs - " +
273+
"Should not return a match with a different subdomain"
274+
);
275+
const feed = getTopSitesFeedForTest(sandbox, {
276+
frecent: [
277+
{
278+
url: "https://bus.domain1.com",
279+
frecency: 1234,
280+
},
281+
],
282+
});
283+
284+
const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
285+
Assert.equal(frecencyBoostedSpocs.length, 0);
286+
287+
sandbox.restore();
288+
}
289+
{
290+
info(
291+
"TopSitesFeed.fetchFrecencyBoostedSpocs - " +
292+
"Should return a match with the same subdomain"
293+
);
294+
const feed = getTopSitesFeedForTest(sandbox, {
295+
frecent: [
296+
{
297+
url: "https://sub.domain1.com",
298+
frecency: 1234,
299+
},
300+
],
301+
});
302+
303+
const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
304+
Assert.equal(frecencyBoostedSpocs.length, 1);
305+
Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname1sub");
306+
267307
sandbox.restore();
268308
}
269309
});

0 commit comments

Comments
 (0)