Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fully qualified domains (trailing dot in domain name) #96

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/clean-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ var URL = require('url');
var hasPrefixRE = /^(([a-z][a-z0-9+.-]*)?:)?\/\//;
var invalidHostnameChars = /[^A-Za-z0-9.-]/;

function trim(value) {
return String(value).replace(/(^\s+|\s+$)/g, '');
function ltrim(value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, we could also just use the plain old String.prototype.trim as it is supported since ECMAScript 5.0.

I'll make the change prior to publish the new release 👍

return String(value).replace(/^\s+/g, '');
}

function rtrim(value) {
return String(value).replace(/[.]+$/g, '');
}

module.exports = function cleanHostValue(value){
value = trim(value).toLowerCase();
value = ltrim(value).toLowerCase();

var parts = URL.parse(hasPrefixRE.test(value) ? value : '//' + value, null, true);

if (parts.hostname && !invalidHostnameChars.test(parts.hostname)) { return parts.hostname; }
if (!invalidHostnameChars.test(value)) { return value; }
if (parts.hostname && !invalidHostnameChars.test(parts.hostname)) { return rtrim(parts.hostname); }
if (!invalidHostnameChars.test(value)) { return rtrim(value); }
return '';
};
28 changes: 28 additions & 0 deletions test/tld.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ describe('tld.js', function () {
expect(tld.isValid('google.com')).to.be(true);
expect(tld.isValid('miam.google.com')).to.be(true);
expect(tld.isValid('miam.miam.google.com')).to.be(true);

//@see https://github.com/oncletom/tld.js/issues/95
expect(tld.isValid('miam.miam.google.com.')).to.be(true);
});

it('should detect invalid hostname', function () {
Expand Down Expand Up @@ -112,6 +115,11 @@ describe('tld.js', function () {
it('should return nytimes.com even in a whole valid', function(){
expect(tld.getDomain('http://www.nytimes.com/')).to.be('nytimes.com');
});

//@see https://github.com/oncletom/tld.js/issues/95
it('should ignore the trailing dot in a domain', function () {
expect(tld.getDomain('https://www.google.co.uk./maps')).to.equal('google.co.uk');
});
});

describe('tldExists method', function () {
Expand All @@ -134,6 +142,11 @@ describe('tld.js', function () {
it('should be truthy on complex TLD which cannot be verified as long as the gTLD exists', function(){
expect(tld.tldExists('uk.com')).to.be(true);
});

//@see https://github.com/oncletom/tld.js/issues/95
it('should ignore the trailing dot in a domain', function () {
expect(tld.tldExists('https://www.google.co.uk./maps')).to.be(true);
});
});

describe('#getPublicSuffix', function () {
Expand Down Expand Up @@ -165,6 +178,11 @@ describe('tld.js', function () {
it('should return null if the publicsuffix does not exist', function(){
expect(tld.getPublicSuffix('www.freedom.nsa')).to.be(null);
});

//@see https://github.com/oncletom/tld.js/issues/95
it('should ignore the trailing dot in a domain', function () {
expect(tld.getPublicSuffix('https://www.google.co.uk./maps')).to.equal('co.uk');
});
});

describe('cleanHostValue', function(){
Expand Down Expand Up @@ -223,6 +241,11 @@ describe('tld.js', function () {
it('should return punycode for international hostnames', function() {
expect(tld.cleanHostValue('台灣')).to.equal('xn--kpry57d');
});

//@see https://github.com/oncletom/tld.js/issues/95
it('should ignore the trailing dot in a domain', function () {
expect(tld.cleanHostValue('http://example.co.uk./some/path?and&query#hash')).to.equal('example.co.uk');
});
});

describe('getSubdomain method', function(){
Expand Down Expand Up @@ -288,6 +311,11 @@ describe('tld.js', function () {
expect(tld.getSubdomain('www.bl.uk')).to.equal('www');
expect(tld.getSubdomain('www.majestic12.co.uk')).to.equal('www');
});

//@see https://github.com/oncletom/tld.js/issues/95
it('should ignore the trailing dot in a domain', function () {
expect(tld.getSubdomain('random.fr.google.co.uk.')).to.equal('random.fr');
});
});

describe('validHosts', function(){
Expand Down