Skip to content

Commit

Permalink
Do not encode local email parts
Browse files Browse the repository at this point in the history
Punycode does not apply to local parts of email addresses, only to the domain name labels in it. Everything before `@` should be preserved.

Fixes #20 (comment).
  • Loading branch information
mathiasbynens committed Aug 8, 2014
1 parent 40e15ef commit 92f8796
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -100,7 +100,7 @@ punycode.toUnicode('xn----dqo34k.com');
// → '☃-⌘.com'

// decode email addresses
punycode.toUnicode('xn--80ahgue5b@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
// → 'джумла@джpумлатест.bрфa'
```

Expand All @@ -117,7 +117,7 @@ punycode.toASCII('☃-⌘.com');

// encode email addresses
punycode.toASCII('джумла@джpумлатест.bрфa');
// → 'xn--80ahgue5b@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
// → 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
```

### `punycode.ucs2`
Expand Down
15 changes: 10 additions & 5 deletions punycode.js
Expand Up @@ -95,12 +95,17 @@
* function.
*/
function mapDomain(string, fn) {
var parts = string.split('@');
var result = '';
if (parts.length > 1) {
// In email addresses, only the domain name should be punycoded. Leave
// the local part (i.e. everything up to `@`) intact.
result = parts[0] + '@';
string = parts[1];
}
var labels = string.split(regexSeparators);
// Note: each label could still contain `@` in the case of an email address.
return map(labels, function(label) {
var parts = label.split('@');
return map(parts, fn).join('@');
}).join('.');
var encoded = map(labels, fn).join('.');
return result + encoded;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion punycode.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/tests.js
Expand Up @@ -236,7 +236,7 @@
{
'description': 'Email address',
'decoded': '\u0434\u0436\u0443\u043C\u043B\u0430@\u0434\u0436p\u0443\u043C\u043B\u0430\u0442\u0435\u0441\u0442.b\u0440\u0444a',
'encoded': 'xn--80ahgue5b@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
'encoded': '\u0434\u0436\u0443\u043C\u043B\u0430@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
}
],
'separators': [
Expand Down

0 comments on commit 92f8796

Please sign in to comment.