From 88f5a32c318161cd76105919a027116f6131ec87 Mon Sep 17 00:00:00 2001 From: Rodney Rehm Date: Thu, 30 Mar 2017 10:52:50 +0200 Subject: [PATCH] fix(SLD): adding CentralNic SLDs - #333 --- CHANGELOG.md | 4 +++ src/SecondLevelDomains.js | 7 ++++- test/test.js | 62 ++++++++++++++++++++++++--------------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b545990..f8cfd7be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ The release notes tracked in this document are also made available on the [releases page](https://github.com/medialize/URI.js/releases) +### master ### + +* adding support for [CentralNic](https://en.wikipedia.org/wiki/CentralNic#Second-level_domains) Second Level Domains - [Issue #333](https://github.com/medialize/URI.js/issues/333) + ### 1.18.9 (March 6th 2017) ### * adding option `strict` to [`URITemplate()`](http://medialize.github.io/URI.js/uri-template.html) in order to throw an exception in case a placeholder could not be replaced - [PR #330](https://github.com/medialize/URI.js/issues/330) diff --git a/src/SecondLevelDomains.js b/src/SecondLevelDomains.js index ac9e3e62..aaea484c 100644 --- a/src/SecondLevelDomains.js +++ b/src/SecondLevelDomains.js @@ -173,7 +173,12 @@ 'ye':' co com gov ltd me net org plc ', 'yu':' ac co edu gov org ', 'za':' ac agric alt bourse city co cybernet db edu gov grondar iaccess imt inca landesign law mil net ngo nis nom olivetti org pix school tm web ', - 'zm':' ac co com edu gov net org sch ' + 'zm':' ac co com edu gov net org sch ', + // https://en.wikipedia.org/wiki/CentralNic#Second-level_domains + 'com': 'ar br cn de eu gb gr hu jpn kr no qc ru sa se uk us uy za ', + 'net': 'gb jp se uk ', + 'org': 'ae', + 'de': 'com ' }, // gorhill 2013-10-25: Using indexOf() instead Regexp(). Significant boost // in both performance and memory footprint. No initialization required. diff --git a/test/test.js b/test/test.js index dc07acf0..1cc2ce5d 100644 --- a/test/test.js +++ b/test/test.js @@ -560,29 +560,20 @@ }); test('sld', function() { - // Lets just test them all.. - // Calling URI.is(), URI.domain(), URI.subdomain() allows us to indirectly - // test SLD.has(), SLD.is() and SLD.get() - var u = new URI('http://www.example.org/foo.html'); - equal(u.is('sld'), false, 'is not sld'); - var list = SecondLevelDomains.list; - var tlds = Object.keys(list); - var iTld = tlds.length; - var tld, slds, sld, iSld; - while ( iTld-- ) { - tld = tlds[iTld]; - slds = list[tld].trim().split(/\s+/); - iSld = slds.length; - while ( iSld-- ) { - sld = slds[iSld].trim() + '.' + tld; - u.hostname('www.example.' + sld); - equal(u.is('sld'), true, 'is sld'); - equal(u.domain(), 'example.' + sld, 'domain is example.' + sld); - equal(u.subdomain(), 'www', 'subdomain is www'); - u.hostname('www.example.' + tld); - equal(u.is('sld'), false, 'is not sld'); - } - } + var u = new URI('http://www.example.ch/foo.html') + equal(u.is('sld'), false, 'is() www.example.ch'); + equal(u.domain(), 'example.ch', 'domain() www.example.ch'); + equal(u.subdomain(), 'www', 'subdomain() www.example.ch'); + + u = new URI('http://www.example.com/foo.html') + equal(u.is('sld'), false, 'is() www.example.com'); + equal(u.domain(), 'example.com', 'domain() www.example.com'); + equal(u.subdomain(), 'www', 'subdomain() www.example.com'); + + u = new URI('http://www.example.eu.com/foo.html') + equal(u.is('sld'), true, 'is() www.example.eu.com'); + equal(u.domain(), 'example.eu.com', 'domain() www.example.eu.com'); + equal(u.subdomain(), 'www', 'subdomain() www.example.eu.com'); }); test('directory', function() { var u = new URI('http://www.example.org/some/directory/foo.html'); @@ -1855,4 +1846,29 @@ equal(URI.encodeReserved('รค:/?#[]@!$&\'()*+,;='), '%C3%A4:/?#[]@!$&\'()*+,;='); }); + module('SecondLevelDomains'); + test('SecondLevelDomains.get()', function() { + equal(SecondLevelDomains.get('www.example.ch'), null, 'www.example.ch') + equal(SecondLevelDomains.get('www.example.com'), null, 'www.example.com') + equal(SecondLevelDomains.get('www.example.eu.com'), 'eu.com', 'www.example.eu.com') + equal(SecondLevelDomains.get('www.example.co.uk'), 'co.uk', 'www.example.co.uk') + }); + test('SecondLevelDomains.has()', function() { + equal(SecondLevelDomains.has('www.example.ch'), false, 'www.example.ch') + equal(SecondLevelDomains.has('www.example.com'), false, 'www.example.com') + equal(SecondLevelDomains.has('www.example.eu.com'), true, 'www.example.eu.com') + equal(SecondLevelDomains.has('www.example.co.uk'), true, 'www.example.co.uk') + }); + test('SecondLevelDomains.is()', function() { + equal(SecondLevelDomains.is('ch'), false, 'ch') + equal(SecondLevelDomains.is('example.ch'), false, 'example.ch') + + equal(SecondLevelDomains.is('com'), false, 'com') + equal(SecondLevelDomains.is('eu.com'), true, 'eu.com') + equal(SecondLevelDomains.is('example.com'), false, 'example.com') + + equal(SecondLevelDomains.is('uk'), false, 'uk') + equal(SecondLevelDomains.is('co.uk'), true, 'co.uk') + }); + })();