From 2984608cf54ee1e4cbaf7130dcd5643bb0bc003c Mon Sep 17 00:00:00 2001 From: Smit Shah Date: Mon, 5 Feb 2018 17:37:29 -0800 Subject: [PATCH] Issue 452 - fixed an issue where parser does not parse the file properly for 2 below conditions. 1. When there is a quote at the end of the line and there is some extra spaces after closing quote and newline 2. When there is a quote before delimiter and there is some extra spaces after closing quote and next delimiter --- papaparse.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/papaparse.js b/papaparse.js index 5bef3cc..1410942 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1285,21 +1285,25 @@ continue; } - // Closing quote followed by delimiter - if (input[quoteSearch+1] === delim) + var spacesBetweenQuoteAndDelimiter = extraSpacesLengthAfterQuote(nextDelim); + + // Closing quote followed by delimiter or 'unnecessary steps + delimiter' + if (input[quoteSearch+1] === delim || spacesBetweenQuoteAndDelimiter > 0) { row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); - cursor = quoteSearch + 1 + delimLen; + cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen; nextDelim = input.indexOf(delim, cursor); nextNewline = input.indexOf(newline, cursor); break; } - // Closing quote followed by newline - if (input.substr(quoteSearch+1, newlineLen) === newline) + var spacesBetweenQuoteAndNewLine = extraSpacesLengthAfterQuote(nextNewline); + + // Closing quote followed by newline or 'unnecessary spaces + newLine' + if (input.substr(quoteSearch+1, newlineLen) === newline || spacesBetweenQuoteAndNewLine > 0) { row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); - saveRow(quoteSearch + 1 + newlineLen); + saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen); nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field if (stepIsFunction) @@ -1385,6 +1389,21 @@ lastCursor = cursor; } + /** + * checks if there are extra spaces after closing quote and newLine without any text + * if Yes, returns the number of spaces + */ + function extraSpacesLengthAfterQuote(index) { + var spaceLength = 0; + if (index !== -1) { + var textBetweenQuoteAndNextNewLine = input.substring(quoteSearch + 1, index); + if (textBetweenQuoteAndNextNewLine && textBetweenQuoteAndNextNewLine.trim() == '') { + spaceLength = textBetweenQuoteAndNextNewLine.length; + } + } + return spaceLength; + } + /** * Appends the remaining input from cursor to the end into * row, saves the row, calls step, and returns the results.