diff --git a/core/test/lib/url-utils-test.js b/core/test/lib/url-utils-test.js index be75ebaabbc3..943abb8abcc1 100644 --- a/core/test/lib/url-utils-test.js +++ b/core/test/lib/url-utils-test.js @@ -396,4 +396,32 @@ describe('UrlUtils', () => { }).toThrow('INVALID_URL'); }); }); + + describe('getRootDomain', () => { + it('returns the correct rootDomain from a string from PSL', () => { + assert.equal(UrlUtils.getRootDomain('https://www.example.com/index.html'), 'example.com'); + assert.equal(UrlUtils.getRootDomain('https://example.com'), 'example.com'); + assert.equal(UrlUtils.getRootDomain('https://www.example.co.uk'), 'example.co.uk'); + assert.equal(UrlUtils.getRootDomain('https://example.com.br/app/'), 'example.com.br'); + assert.equal(UrlUtils.getRootDomain('https://example.tokyo.jp'), 'example.tokyo.jp'); + assert.equal(UrlUtils.getRootDomain('https://sub.example.com'), 'example.com'); + assert.equal(UrlUtils.getRootDomain('https://sub.example.tokyo.jp'), 'example.tokyo.jp'); + assert.equal(UrlUtils.getRootDomain('http://localhost'), 'localhost'); + assert.equal(UrlUtils.getRootDomain('http://localhost:8080'), 'localhost'); + assert.equal(UrlUtils.getRootDomain('https://www.hydro.mb.ca'), 'hydro.mb.ca'); + }); + + it('returns the correct rootDomain from an URL object', () => { + assert.equal(UrlUtils.getRootDomain(new URL('https://www.example.com/index.html')), 'example.com'); + assert.equal(UrlUtils.getRootDomain(new URL('https://example.com')), 'example.com'); + assert.equal(UrlUtils.getRootDomain(new URL('https://www.example.co.uk')), 'example.co.uk'); + assert.equal(UrlUtils.getRootDomain(new URL('https://example.com.br/app/')), 'example.com.br'); + assert.equal(UrlUtils.getRootDomain(new URL('https://example.tokyo.jp')), 'example.tokyo.jp'); + assert.equal(UrlUtils.getRootDomain(new URL('https://sub.example.com')), 'example.com'); + assert.equal(UrlUtils.getRootDomain(new URL('https://sub.example.tokyo.jp')), 'example.tokyo.jp'); + assert.equal(UrlUtils.getRootDomain(new URL('http://localhost')), 'localhost'); + assert.equal(UrlUtils.getRootDomain(new URL('http://localhost:8080')), 'localhost'); + assert.equal(UrlUtils.getRootDomain(new URL('https://www.hydro.mb.ca')), 'hydro.mb.ca'); + }); + }); }); diff --git a/shared/test/util-test.js b/shared/test/util-test.js index 200a10320156..35629e7d5d23 100644 --- a/shared/test/util-test.js +++ b/shared/test/util-test.js @@ -9,38 +9,38 @@ import assert from 'assert/strict'; import {Util} from '../util.js'; describe('util helpers', () => { - describe('getTld', () => { + describe('getPseudoTld', () => { it('returns the correct tld', () => { - assert.equal(Util.getTld('example.com'), '.com'); - assert.equal(Util.getTld('example.co.uk'), '.co.uk'); - assert.equal(Util.getTld('example.com.br'), '.com.br'); - assert.equal(Util.getTld('example.tokyo.jp'), '.jp'); + assert.equal(Util.getPseudoTld('example.com'), '.com'); + assert.equal(Util.getPseudoTld('example.co.uk'), '.co.uk'); + assert.equal(Util.getPseudoTld('example.com.br'), '.com.br'); + assert.equal(Util.getPseudoTld('example.tokyo.jp'), '.jp'); }); }); - describe('getLegacyRootDomain', () => { + describe('getPseudoRootDomain', () => { it('returns the correct rootDomain from a string', () => { - assert.equal(Util.getLegacyRootDomain('https://www.example.com/index.html'), 'example.com'); - assert.equal(Util.getLegacyRootDomain('https://example.com'), 'example.com'); - assert.equal(Util.getLegacyRootDomain('https://www.example.co.uk'), 'example.co.uk'); - assert.equal(Util.getLegacyRootDomain('https://example.com.br/app/'), 'example.com.br'); - assert.equal(Util.getLegacyRootDomain('https://example.tokyo.jp'), 'tokyo.jp'); - assert.equal(Util.getLegacyRootDomain('https://sub.example.com'), 'example.com'); - assert.equal(Util.getLegacyRootDomain('https://sub.example.tokyo.jp'), 'tokyo.jp'); - assert.equal(Util.getLegacyRootDomain('http://localhost'), 'localhost'); - assert.equal(Util.getLegacyRootDomain('http://localhost:8080'), 'localhost'); + assert.equal(Util.getPseudoRootDomain('https://www.example.com/index.html'), 'example.com'); + assert.equal(Util.getPseudoRootDomain('https://example.com'), 'example.com'); + assert.equal(Util.getPseudoRootDomain('https://www.example.co.uk'), 'example.co.uk'); + assert.equal(Util.getPseudoRootDomain('https://example.com.br/app/'), 'example.com.br'); + assert.equal(Util.getPseudoRootDomain('https://example.tokyo.jp'), 'tokyo.jp'); + assert.equal(Util.getPseudoRootDomain('https://sub.example.com'), 'example.com'); + assert.equal(Util.getPseudoRootDomain('https://sub.example.tokyo.jp'), 'tokyo.jp'); + assert.equal(Util.getPseudoRootDomain('http://localhost'), 'localhost'); + assert.equal(Util.getPseudoRootDomain('http://localhost:8080'), 'localhost'); }); it('returns the correct rootDomain from an URL object', () => { - assert.equal(Util.getLegacyRootDomain(new URL('https://www.example.com/index.html')), 'example.com'); - assert.equal(Util.getLegacyRootDomain(new URL('https://example.com')), 'example.com'); - assert.equal(Util.getLegacyRootDomain(new URL('https://www.example.co.uk')), 'example.co.uk'); - assert.equal(Util.getLegacyRootDomain(new URL('https://example.com.br/app/')), 'example.com.br'); - assert.equal(Util.getLegacyRootDomain(new URL('https://example.tokyo.jp')), 'tokyo.jp'); - assert.equal(Util.getLegacyRootDomain(new URL('https://sub.example.com')), 'example.com'); - assert.equal(Util.getLegacyRootDomain(new URL('https://sub.example.tokyo.jp')), 'tokyo.jp'); - assert.equal(Util.getLegacyRootDomain(new URL('http://localhost')), 'localhost'); - assert.equal(Util.getLegacyRootDomain(new URL('http://localhost:8080')), 'localhost'); + assert.equal(Util.getPseudoRootDomain(new URL('https://www.example.com/index.html')), 'example.com'); + assert.equal(Util.getPseudoRootDomain(new URL('https://example.com')), 'example.com'); + assert.equal(Util.getPseudoRootDomain(new URL('https://www.example.co.uk')), 'example.co.uk'); + assert.equal(Util.getPseudoRootDomain(new URL('https://example.com.br/app/')), 'example.com.br'); + assert.equal(Util.getPseudoRootDomain(new URL('https://example.tokyo.jp')), 'tokyo.jp'); + assert.equal(Util.getPseudoRootDomain(new URL('https://sub.example.com')), 'example.com'); + assert.equal(Util.getPseudoRootDomain(new URL('https://sub.example.tokyo.jp')), 'tokyo.jp'); + assert.equal(Util.getPseudoRootDomain(new URL('http://localhost')), 'localhost'); + assert.equal(Util.getPseudoRootDomain(new URL('http://localhost:8080')), 'localhost'); }); }); diff --git a/shared/util.js b/shared/util.js index 21038af9a39c..fcc51eb13dce 100644 --- a/shared/util.js +++ b/shared/util.js @@ -86,12 +86,12 @@ class Util { static getEntityFromUrl(url, entities) { // If it's a pre-v10 LHR, we don't have entities, so match against the root-ish domain if (!entities) { - return Util.getLegacyRootDomain(url); + return Util.getPseudoRootDomain(url); } const entity = entities.find(e => e.origins.find(origin => url.startsWith(origin))); - // This fallback case would be unexpected, but leaving for safety. - return entity || Util.getLegacyRootDomain(url); + // This fallback case would be unexpected, but leaving for safety. + return entity || Util.getPseudoRootDomain(url); } /** @@ -314,7 +314,7 @@ class Util { * @param {string} hostname * @return {string} tld */ - static getLegacyTld(hostname) { + static getPseudoTld(hostname) { const tlds = hostname.split('.').slice(-2); if (!listOfTlds.includes(tlds[0])) { @@ -328,13 +328,14 @@ class Util { * Returns a primary domain for provided hostname (e.g. www.example.com -> example.com). * As it doesn't consult the Public Suffix List, it can sometimes lose detail. * See the `listOfTlds` comment above for more. - * This function is used only while rendering pre-10.0 LHRs. + * This function is used only while rendering pre-10.0 LHRs. See UrlUtils.getRootDomain + * for the current method that makes use of PSL. * @param {string|URL} url hostname or URL object * @return {string} */ - static getLegacyRootDomain(url) { + static getPseudoRootDomain(url) { const hostname = Util.createOrReturnURL(url).hostname; - const tld = Util.getLegacyTld(hostname); + const tld = Util.getPseudoTld(hostname); // tld is .com or .co.uk which means we means that length is 1 to big // .com => 2 & .co.uk => 3