7
7
* @return {Object } Address object
8
8
*/
9
9
function _handleAddress ( tokens ) {
10
- let token ;
11
10
let isGroup = false ;
12
11
let state = 'text' ;
13
12
let address ;
@@ -23,7 +22,8 @@ function _handleAddress(tokens) {
23
22
24
23
// Filter out <addresses>, (comments) and regular text
25
24
for ( i = 0 , len = tokens . length ; i < len ; i ++ ) {
26
- token = tokens [ i ] ;
25
+ let token = tokens [ i ] ;
26
+ let prevToken = i ? tokens [ i - 1 ] : null ;
27
27
if ( token . type === 'operator' ) {
28
28
switch ( token . value ) {
29
29
case '<' :
@@ -38,6 +38,7 @@ function _handleAddress(tokens) {
38
38
break ;
39
39
default :
40
40
state = 'text' ;
41
+ break ;
41
42
}
42
43
} else if ( token . value ) {
43
44
if ( state === 'address' ) {
@@ -46,7 +47,13 @@ function _handleAddress(tokens) {
46
47
// and so will we
47
48
token . value = token . value . replace ( / ^ [ ^ < ] * < \s * / , '' ) ;
48
49
}
49
- data [ state ] . push ( token . value ) ;
50
+
51
+ if ( prevToken && prevToken . noBreak && data [ state ] . length ) {
52
+ // join values
53
+ data [ state ] [ data [ state ] . length - 1 ] += token . value ;
54
+ } else {
55
+ data [ state ] . push ( token . value ) ;
56
+ }
50
57
}
51
58
}
52
59
@@ -172,11 +179,12 @@ class Tokenizer {
172
179
* @return {Array } An array of operator|text tokens
173
180
*/
174
181
tokenize ( ) {
175
- let chr ,
176
- list = [ ] ;
182
+ let list = [ ] ;
183
+
177
184
for ( let i = 0 , len = this . str . length ; i < len ; i ++ ) {
178
- chr = this . str . charAt ( i ) ;
179
- this . checkChar ( chr ) ;
185
+ let chr = this . str . charAt ( i ) ;
186
+ let nextChr = i < len - 1 ? this . str . charAt ( i + 1 ) : null ;
187
+ this . checkChar ( chr , nextChr ) ;
180
188
}
181
189
182
190
this . list . forEach ( node => {
@@ -194,18 +202,24 @@ class Tokenizer {
194
202
*
195
203
* @param {String } chr Character from the address field
196
204
*/
197
- checkChar ( chr ) {
205
+ checkChar ( chr , nextChr ) {
198
206
if ( this . escaped ) {
199
207
// ignore next condition blocks
200
208
} else if ( chr === this . operatorExpecting ) {
201
209
this . node = {
202
210
type : 'operator' ,
203
211
value : chr
204
212
} ;
213
+
214
+ if ( nextChr && ! [ ' ' , '\t' , '\r' , '\n' , ',' , ';' ] . includes ( nextChr ) ) {
215
+ this . node . noBreak = true ;
216
+ }
217
+
205
218
this . list . push ( this . node ) ;
206
219
this . node = null ;
207
220
this . operatorExpecting = '' ;
208
221
this . escaped = false ;
222
+
209
223
return ;
210
224
} else if ( ! this . operatorExpecting && chr in this . operators ) {
211
225
this . node = {
0 commit comments