Skip to content

Commit

Permalink
fix(parse): do not remove backslashes followed by b
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Feb 7, 2021
1 parent cee1200 commit ca764ef
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ module.exports = function (input, callback) {
// so remove that specific one before continuing.
// SB2 JSONs and SB3 JSONs have different versions of the
// character serialized (e.g. \u0008 and \b), strip out both versions
result = JSON.parse(input.replace(/\\b|\\u0008/g, ''));
result = JSON.parse(input.replace(
/(\\+)(b|u0008)/g,
(match, backslash, code) => {
// If the number is odd, there is an actual backspace.
if (backslash.length % 2) {
// The match contains an actual backspace, instead of backslashes followed by b.
// Remove backspace and keep backslashes that are not part of
// the control character representation.
return match.replace('\\' + code, '');
}
// They are just backslashes followed by b or u0008. (e.g. "\\b")
// Don't replace in this case. (LLK/scratch-parser#56)
return match;
}
));
} catch (e) {
return callback(e.toString());
}
Expand Down
24 changes: 24 additions & 0 deletions test/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,27 @@ test('backspace control characters get stripped out in sb3', function (t) {
t.end();
});
});

test('backslashes are kept when stripping backspace control characters', function (t) {
var json = JSON.stringify({
test: '\\\\\baaa\ba'
});
parse(json, function (err, res) {
t.equal(err, null);
t.type(res, 'object');
t.equal(res.test, '\\\\aaaa');
t.end();
});
});

test('backslashes followed by b are not stripped at all', function (t) {
var json = JSON.stringify({
test: '\\b\b\\\\b'
});
parse(json, function (err, res) {
t.equal(err, null);
t.type(res, 'object');
t.equal(res.test, '\\b\\\\b');
t.end();
});
});

0 comments on commit ca764ef

Please sign in to comment.