Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});