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

Make rules with no exceptions valid #52

Merged
merged 1 commit into from
Jan 15, 2015
Merged

Make rules with no exceptions valid #52

merged 1 commit into from
Jan 15, 2015

Conversation

GreyKn
Copy link

@GreyKn GreyKn commented Jan 8, 2015

Fixes a problem where suffixes that are valid (like .fm and .eu) but were falling to parse correctly:

tldjs.rules.eu // returns ""
tldjs.getPublicSuffix('microsoft.eu') // expect `microsoft', get null

@@ -141,7 +141,7 @@ tld.prototype.getRulesForTld = function getRulesForTld (tld, default_rule) {
}

// Nothing found, apply some default value
if (!rules) {
if (rules === void 0) {
Copy link
Owner

Choose a reason for hiding this comment

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

To properly reflect the intent, we should have then if (!rules || rules.length === 0) { right?

I am afraid === void 0 has no real signification for a user.

Copy link
Author

Choose a reason for hiding this comment

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

Rules having a length of 0 is a valid case.
Example: tldjs.rules.eu.length => 0

"Nothing found" would mean the tld is undefined, so the check against void 0 should be the correct one.
Example: tldjs.rules.imagination === void 0 => true

Copy link
Owner

Choose a reason for hiding this comment

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

I am not sure to get it right, as your test relates to .eu, and for this same case, the rules === void 0 syntax does not tell me what to expect in that case (going into the if statement or not).

To be fair, I do not see the difference between rules === void 0 and !rules (which is a shortcut for rules === null || rules === undefined).

Copy link
Collaborator

Choose a reason for hiding this comment

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

!rules captures '', null, undefined, 0, false - any falsey value.

Unfortunately, undefined is writable, so not completely safe, whereas void 0 ensures the undefined value is used. See http://stackoverflow.com/questions/4806286/difference-between-void-0-and-undefined.

Checking against undefined/void explicitly is the proper way to handle this, rather than a catch-all for a falsey value, then making sure it's not an empty string.

Copy link
Owner

Choose a reason for hiding this comment

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

OK gotcha :-)

This project code is enforced in strict mode so undefined cannot be rewritten. In addition, if rules === false, we do consider we have no rules, so we want to go into that if statement.

A more accurate condition in that case would be if (Array.isArray(rules)) as ES5 is our minimal requirement.

Copy link
Author

Choose a reason for hiding this comment

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

As far as I can tell, rules is a string (based on looking at https://github.com/oncletom/tld.js/blob/f1477fbae4d60c1c2eb3b5dfc91e969dd7eb00a0/rules.json). It can be "", "*", or a longer string that is | delimited.

If it's undefined, it means that the requested TLD does not exist in rules.json, thus is not a known TLD, so it should have a default rule applied.

In the existing code, trying to find a public suffix on any TLD that is defined to have a rule of "" will fail because the default rule is applied. But something like .eu is a valid domain, so we don't want default rules applied.

Or am I misunderstanding, and "" means we should apply a default rule? I'm currently reading it as "apply the default rule only if the TLD is unknown"

Copy link
Owner

Choose a reason for hiding this comment

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

You are right @GreyKn, it seems that I am not familiar anymore with my own codebase ;-)

I now understand why the if statement check is problematic, sorry if it has been a pain to explain!

thom4parisot pushed a commit that referenced this pull request Jan 15, 2015
Make rules with no exceptions valid
@thom4parisot thom4parisot merged commit 686a3e1 into thom4parisot:master Jan 15, 2015
@thom4parisot
Copy link
Owner

Thanks @GreyKn and @andreasheim, it's now on npm as of v1.5.2 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants