Skip to content

Commit

Permalink
fix(addressparser): Correctly detect if user local part is attached t…
Browse files Browse the repository at this point in the history
…o domain part
  • Loading branch information
andris9 committed Oct 28, 2024
1 parent 81de9eb commit f2096c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/addressparser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @return {Object} Address object
*/
function _handleAddress(tokens) {
let token;
let isGroup = false;
let state = 'text';
let address;
Expand All @@ -23,7 +22,8 @@ function _handleAddress(tokens) {

// Filter out <addresses>, (comments) and regular text
for (i = 0, len = tokens.length; i < len; i++) {
token = tokens[i];
let token = tokens[i];
let prevToken = i ? tokens[i - 1] : null;
if (token.type === 'operator') {
switch (token.value) {
case '<':
Expand All @@ -38,6 +38,7 @@ function _handleAddress(tokens) {
break;
default:
state = 'text';
break;
}
} else if (token.value) {
if (state === 'address') {
Expand All @@ -46,7 +47,13 @@ function _handleAddress(tokens) {
// and so will we
token.value = token.value.replace(/^[^<]*<\s*/, '');
}
data[state].push(token.value);

if (prevToken && prevToken.noBreak && data[state].length) {
// join values
data[state][data[state].length - 1] += token.value;
} else {
data[state].push(token.value);
}
}
}

Expand Down Expand Up @@ -172,11 +179,12 @@ class Tokenizer {
* @return {Array} An array of operator|text tokens
*/
tokenize() {
let chr,
list = [];
let list = [];

for (let i = 0, len = this.str.length; i < len; i++) {
chr = this.str.charAt(i);
this.checkChar(chr);
let chr = this.str.charAt(i);
let nextChr = i < len - 1 ? this.str.charAt(i + 1) : null;
this.checkChar(chr, nextChr);
}

this.list.forEach(node => {
Expand All @@ -194,18 +202,24 @@ class Tokenizer {
*
* @param {String} chr Character from the address field
*/
checkChar(chr) {
checkChar(chr, nextChr) {
if (this.escaped) {
// ignore next condition blocks
} else if (chr === this.operatorExpecting) {
this.node = {
type: 'operator',
value: chr
};

if (nextChr && ![' ', '\t', '\r', '\n', ',', ';'].includes(nextChr)) {
this.node.noBreak = true;
}

this.list.push(this.node);
this.node = null;
this.operatorExpecting = '';
this.escaped = false;

return;
} else if (!this.operatorExpecting && chr in this.operators) {
this.node = {
Expand Down
11 changes: 11 additions & 0 deletions test/addressparser/addressparser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,15 @@ describe('#addressparser', () => {
let expected = [{ address: 'test@example.com', name: 'Firstname " \\, Lastname (Test)' }];
assert.deepStrictEqual(addressparser(input), expected);
});

it('should handle quoted usernames', () => {
let input = '"test@subdomain.com"@example.com';
let expected = [
{
address: 'test@subdomain.com@example.com',
name: ''
}
];
assert.deepStrictEqual(addressparser(input), expected);
});
});

0 comments on commit f2096c5

Please sign in to comment.