mirror of
https://github.com/scratchfoundation/scratch-parser.git
synced 2025-08-28 22:18:45 -04:00
fix(parse): do not remove backslashes followed by b
This commit is contained in:
parent
cee1200c85
commit
ca764efb0f
2 changed files with 39 additions and 1 deletions
16
lib/parse.js
16
lib/parse.js
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue