From c410e9a4540d0a497f724fcccae83e26b628695e Mon Sep 17 00:00:00 2001 From: David Matejka Date: Thu, 13 Nov 2014 01:31:46 +0100 Subject: [PATCH] fixed handling of special characters in "" --- src/decoder.js | 17 +++++++++-------- test/Decoder.scalar.js | 7 +++++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/decoder.js b/src/decoder.js index 5bc7eb2..0a60c22 100644 --- a/src/decoder.js +++ b/src/decoder.js @@ -253,16 +253,17 @@ function decoder() { 'null': 0, 'Null': 0, 'NULL': 0 }; if (t[0] === '"') { - value = t.substr(1, t.length - 2).replace(/#\\\\(?:u[0-9a-f]{4}|x[0-9a-f]{2}|.)/i, function (whole, sq) { + var self = this; + value = t.substr(1, t.length - 2).replace(/\\(?:u[0-9a-f]{4}|x[0-9a-f]{2}|.)/i, function (match) { var mapping = {'t': "\t", 'n': "\n", 'r': "\r", 'f': "\x0C", 'b': "\x08", '"': '"', '\\': '\\', '/': '/', '_': "\xc2\xa0"}; - if (mapping[sq[1]] !== undefined) { - return mapping[sq[1]]; - } else if (sq[1] === 'u' && sq.length === 6) { - return String.fromCharCode(parseInt(sq.substr(2), 16)); - } else if (sq[1] === 'x' && sq.length === 4) { + if (mapping[match[1]] !== undefined) { + return mapping[match[1]]; + } else if (match[1] === 'u' && match.length === 6) { return String.fromCharCode(parseInt(sq.substr(2), 16)); + } else if (match[1] === 'x' && match.length === 4) { + return String.fromCharCode(parseInt(match.substr(2), 16)); } else { - this.error("Invalid escaping sequence " + sq + ""); + self.error("Invalid escaping sequence " + match + ""); } }); } else if (t[0] === "'") { @@ -358,7 +359,7 @@ function decoder() { } decoder.patterns = [ - "'[^'\\n]*'|\"(?:\\.|[^\"\\\\\\n])*\"", + "'[^'\\n]*'|\"(?:\\\\.|[^\"\\\\\\n])*\"", "(?:[^\\x00-\\x20#\"',:=[\\]{}()!`-]|[:-][^\"',\\]})\\s])(?:[^\\x00-\\x20,:=\\]})(]+|:(?![\\s,\\]})]|$)|[\\ \\t]+[^\\x00-\\x20#,:=\\]})(])*", "[,:=[\\]{}()-]", "?:\\#.*", diff --git a/test/Decoder.scalar.js b/test/Decoder.scalar.js index 61eee9e..45d72dc 100644 --- a/test/Decoder.scalar.js +++ b/test/Decoder.scalar.js @@ -39,10 +39,13 @@ suite('Decoder.scalar', function () { assert.equal('the"string', neon.decode('the"string #literal')); }); test('literal 3', function () { - assert.equal("the'string#literal", neon.decode("the'string#literal")); + assert.equal("the'string #literal", neon.decode('"the\'string #literal"')); }); test('literal 4', function () { - assert.equal("the'string", neon.decode("the'string #literal")); + assert.equal('the"string #literal', neon.decode("'the\"string #literal'")); + }); + test('literal 5', function () { + assert.equal('the"string #literal', neon.decode('"the\\"string #literal"')); }); test('literal 5', function () { assert.equal(" ", neon.decode(" "));