fix(parse): do not remove backslashes followed by b

This commit is contained in:
apple502j 2021-02-07 15:44:00 +09:00
parent cee1200c85
commit ca764efb0f
No known key found for this signature in database
GPG key ID: 13BE71607A63CDF2
2 changed files with 39 additions and 1 deletions

View file

@ -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());
}

View file

@ -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();
});
});