diff --git a/src/imap-parser.js b/src/imap-parser.js index bf14bd5..60df5f9 100644 --- a/src/imap-parser.js +++ b/src/imap-parser.js @@ -356,9 +356,6 @@ // space finishes an atom if (chr === " ") { - if ([")", "]"].indexOf(this.str.charAt(i + 1)) >= 0) { - throw new Error("Unexpected whitespace at position " + (this.pos + i + 1)); - } this.currentNode.endPos = this.pos + i - 1; this.currentNode = this.currentNode.parentNode; this.state = "NORMAL"; diff --git a/test/imap-parser-unit.js b/test/imap-parser-unit.js index 36eae64..d362ade 100644 --- a/test/imap-parser-unit.js +++ b/test/imap-parser-unit.js @@ -211,6 +211,14 @@ value: "1234" }] ]); + // Trailing whitespace in a BODYSTRUCTURE atom list has been + // observed on yahoo.co.jp's + expect(imapHandler.parser("TAG1 CMD (1234 )").attributes).to.deep.equal([ + [{ + type: "ATOM", + value: "1234" + }] + ]); expect(imapHandler.parser("TAG1 CMD (1234) ").attributes).to.deep.equal([ [{ type: "ATOM", @@ -218,12 +226,6 @@ }] ]); }); - it("should fail", function() { - expect(function() { - imapHandler.parser("TAG1 CMD (1234 )"); - }).to.throw(Error); - }); - }); describe('nested list', function() { @@ -254,6 +256,19 @@ } ] ]); + expect(imapHandler.parser("TAG1 CMD (((TERE) ) VANA)").attributes).to.deep.equal([ + [ + [ + [{ + type: "ATOM", + value: "TERE" + }] + ], { + type: "ATOM", + value: "VANA" + } + ] + ]); }); }); @@ -318,6 +333,30 @@ ] }]); }); + it("will not fail due to trailing whitespace", function() { + // We intentionally have trailing whitespace in the section here + // because we altered the parser to handle this when we made it + // legal for lists and it makes sense to accordingly test it. + // However, we have no recorded incidences of this happening in + // reality (unlike for lists). + expect(imapHandler.parser("TAG1 CMD BODY[HEADER.FIELDS (Subject From) ]").attributes).to.deep.equal([{ + type: "ATOM", + value: "BODY", + section: [ + { + type: 'ATOM', + value: 'HEADER.FIELDS' + }, + [{ + type: "ATOM", + value: "Subject" + }, { + type: "ATOM", + value: "From" + }] + ] + }]); + }); it("should fail where default BODY and BODY.PEEK are allowed to have sections", function() {}); expect(function() { imapHandler.parser("TAG1 CMD KODY[]");