Skip to content

Commit

Permalink
fixed handling of special characters in ""
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Nov 13, 2014
1 parent 1a24d63 commit c410e9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] === "'") {
Expand Down Expand Up @@ -358,7 +359,7 @@ function decoder() {
}

decoder.patterns = [
"'[^'\\n]*'|\"(?:\\.|[^\"\\\\\\n])*\"",
"'[^'\\n]*'|\"(?:\\\\.|[^\"\\\\\\n])*\"",
"(?:[^\\x00-\\x20#\"',:=[\\]{}()!`-]|[:-][^\"',\\]})\\s])(?:[^\\x00-\\x20,:=\\]})(]+|:(?![\\s,\\]})]|$)|[\\ \\t]+[^\\x00-\\x20#,:=\\]})(])*",
"[,:=[\\]{}()-]",
"?:\\#.*",
Expand Down
7 changes: 5 additions & 2 deletions test/Decoder.scalar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<literal> <literal>", neon.decode("<literal> <literal>"));
Expand Down

0 comments on commit c410e9a

Please sign in to comment.