From a006c4fe7bf9d3b4ee810eaf24bcf7520f42c56e Mon Sep 17 00:00:00 2001 From: tomaskukk Date: Mon, 10 Jun 2024 12:42:33 +0300 Subject: [PATCH] fix multiple emojis in a row causing additional empty tokens being added --- lib/rfc2047.js | 3 ++- test/rfc2047.js | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/rfc2047.js b/lib/rfc2047.js index 59a01fb..95ed190 100644 --- a/lib/rfc2047.js +++ b/lib/rfc2047.js @@ -246,7 +246,8 @@ rfc2047.encode = (text) => { } // Word contains at least one header unsafe char, an encoded word must be created. - if (token.length > maxNumCharsPerEncodedWord) { + // Don't check string length directly as emojis can be longer than 1 char. + if ([...token].length > maxNumCharsPerEncodedWord) { nextTokenMustBeEncoded = true; const chars = [...token]; tokens.splice( diff --git a/test/rfc2047.js b/test/rfc2047.js index 3389f34..0f26240 100644 --- a/test/rfc2047.js +++ b/test/rfc2047.js @@ -50,9 +50,9 @@ describe('rfc2047', () => { // https://github.com/One-com/rfc2047/issues/11 it('should handle a string with multiple emojis in a row', () => { expect( - 'Seven hills😍🦌', + 'Seven hills 😍🦌😍🦌😍 no extra spaces', 'to encode back and forth to', - 'Seven =?utf-8?Q?hills=F0=9F=98=8D=F0=9F=A6=8C?=' + 'Seven hills =?utf-8?Q?=F0=9F=98=8D=F0=9F=A6=8C=F0=9F=98=8D=F0=9F=A6=8C=F0=9F=98=8D?= no extra spaces' ); });