File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,21 @@ module.exports = function (input, callback) {
1616 // so remove that specific one before continuing.
1717 // SB2 JSONs and SB3 JSONs have different versions of the
1818 // character serialized (e.g. \u0008 and \b), strip out both versions
19- result = JSON . parse ( input . replace ( / \\ b | \\ u 0 0 0 8 / g, '' ) ) ;
19+ result = JSON . parse ( input . replace (
20+ / ( \\ + ) ( b | u 0 0 0 8 ) / g,
21+ ( match , backslash , code ) => {
22+ // If the number is odd, there is an actual backspace.
23+ if ( backslash . length % 2 ) {
24+ // The match contains an actual backspace, instead of backslashes followed by b.
25+ // Remove backspace and keep backslashes that are not part of
26+ // the control character representation.
27+ return match . replace ( '\\' + code , '' ) ;
28+ }
29+ // They are just backslashes followed by b or u0008. (e.g. "\\b")
30+ // Don't replace in this case. (LLK/scratch-parser#56)
31+ return match ;
32+ }
33+ ) ) ;
2034 } catch ( e ) {
2135 return callback ( e . toString ( ) ) ;
2236 }
Original file line number Diff line number Diff line change @@ -61,3 +61,27 @@ test('backspace control characters get stripped out in sb3', function (t) {
6161 t . end ( ) ;
6262 } ) ;
6363} ) ;
64+
65+ test ( 'backslashes are kept when stripping backspace control characters' , function ( t ) {
66+ var json = JSON . stringify ( {
67+ test : '\\\\\baaa\ba'
68+ } ) ;
69+ parse ( json , function ( err , res ) {
70+ t . equal ( err , null ) ;
71+ t . type ( res , 'object' ) ;
72+ t . equal ( res . test , '\\\\aaaa' ) ;
73+ t . end ( ) ;
74+ } ) ;
75+ } ) ;
76+
77+ test ( 'backslashes followed by b are not stripped at all' , function ( t ) {
78+ var json = JSON . stringify ( {
79+ test : '\\b\b\\\\b'
80+ } ) ;
81+ parse ( json , function ( err , res ) {
82+ t . equal ( err , null ) ;
83+ t . type ( res , 'object' ) ;
84+ t . equal ( res . test , '\\b\\\\b' ) ;
85+ t . end ( ) ;
86+ } ) ;
87+ } ) ;
You can’t perform that action at this time.
0 commit comments