@@ -1169,6 +1169,73 @@ describe('EscapeSequenceParser', function (): void {
1169
1169
parser2 . parse ( INPUT ) ;
1170
1170
chai . expect ( csi ) . eql ( [ ] ) ;
1171
1171
} ) ;
1172
+ describe ( 'CSI custom handlers' , ( ) => {
1173
+ it ( 'Prevent fallback' , ( ) => {
1174
+ const csiCustom : [ string , number [ ] , string ] [ ] = [ ] ;
1175
+ parser2 . setCsiHandler ( 'm' , ( params , collect ) => csi . push ( [ 'm' , params , collect ] ) ) ;
1176
+ parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom . push ( [ 'm' , params , collect ] ) ; return true ; } ) ;
1177
+ parser2 . parse ( INPUT ) ;
1178
+ chai . expect ( csi ) . eql ( [ ] , 'Should not fallback to original handler' ) ;
1179
+ chai . expect ( csiCustom ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] ) ;
1180
+ } ) ;
1181
+ it ( 'Allow fallback' , ( ) => {
1182
+ const csiCustom : [ string , number [ ] , string ] [ ] = [ ] ;
1183
+ parser2 . setCsiHandler ( 'm' , ( params , collect ) => csi . push ( [ 'm' , params , collect ] ) ) ;
1184
+ parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom . push ( [ 'm' , params , collect ] ) ; return false ; } ) ;
1185
+ parser2 . parse ( INPUT ) ;
1186
+ chai . expect ( csi ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] , 'Should fallback to original handler' ) ;
1187
+ chai . expect ( csiCustom ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] ) ;
1188
+ } ) ;
1189
+ it ( 'Multiple custom handlers fallback once' , ( ) => {
1190
+ const csiCustom : [ string , number [ ] , string ] [ ] = [ ] ;
1191
+ const csiCustom2 : [ string , number [ ] , string ] [ ] = [ ] ;
1192
+ parser2 . setCsiHandler ( 'm' , ( params , collect ) => csi . push ( [ 'm' , params , collect ] ) ) ;
1193
+ parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom . push ( [ 'm' , params , collect ] ) ; return true ; } ) ;
1194
+ parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom2 . push ( [ 'm' , params , collect ] ) ; return false ; } ) ;
1195
+ parser2 . parse ( INPUT ) ;
1196
+ chai . expect ( csi ) . eql ( [ ] , 'Should not fallback to original handler' ) ;
1197
+ chai . expect ( csiCustom ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] ) ;
1198
+ chai . expect ( csiCustom2 ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] ) ;
1199
+ } ) ;
1200
+ it ( 'Multiple custom handlers no fallback' , ( ) => {
1201
+ const csiCustom : [ string , number [ ] , string ] [ ] = [ ] ;
1202
+ const csiCustom2 : [ string , number [ ] , string ] [ ] = [ ] ;
1203
+ parser2 . setCsiHandler ( 'm' , ( params , collect ) => csi . push ( [ 'm' , params , collect ] ) ) ;
1204
+ parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom . push ( [ 'm' , params , collect ] ) ; return true ; } ) ;
1205
+ parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom2 . push ( [ 'm' , params , collect ] ) ; return true ; } ) ;
1206
+ parser2 . parse ( INPUT ) ;
1207
+ chai . expect ( csi ) . eql ( [ ] , 'Should not fallback to original handler' ) ;
1208
+ chai . expect ( csiCustom ) . eql ( [ ] , 'Should not fallback once' ) ;
1209
+ chai . expect ( csiCustom2 ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] ) ;
1210
+ } ) ;
1211
+ it ( 'Execution order should go from latest handler down to the original' , ( ) => {
1212
+ const order : number [ ] = [ ] ;
1213
+ parser2 . setCsiHandler ( 'm' , ( ) => order . push ( 1 ) ) ;
1214
+ parser2 . addCsiHandler ( 'm' , ( ) => { order . push ( 2 ) ; return false ; } ) ;
1215
+ parser2 . addCsiHandler ( 'm' , ( ) => { order . push ( 3 ) ; return false ; } ) ;
1216
+ parser2 . parse ( '\x1b[0m' ) ;
1217
+ chai . expect ( order ) . eql ( [ 3 , 2 , 1 ] ) ;
1218
+ } ) ;
1219
+ it ( 'Dispose should work' , ( ) => {
1220
+ const csiCustom : [ string , number [ ] , string ] [ ] = [ ] ;
1221
+ parser2 . setCsiHandler ( 'm' , ( params , collect ) => csi . push ( [ 'm' , params , collect ] ) ) ;
1222
+ const customHandler = parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom . push ( [ 'm' , params , collect ] ) ; return true ; } ) ;
1223
+ customHandler . dispose ( ) ;
1224
+ parser2 . parse ( INPUT ) ;
1225
+ chai . expect ( csi ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] ) ;
1226
+ chai . expect ( csiCustom ) . eql ( [ ] , 'Should not use custom handler as it was disposed' ) ;
1227
+ } ) ;
1228
+ it ( 'Should not corrupt the parser when dispose is called twice' , ( ) => {
1229
+ const csiCustom : [ string , number [ ] , string ] [ ] = [ ] ;
1230
+ parser2 . setCsiHandler ( 'm' , ( params , collect ) => csi . push ( [ 'm' , params , collect ] ) ) ;
1231
+ const customHandler = parser2 . addCsiHandler ( 'm' , ( params , collect ) => { csiCustom . push ( [ 'm' , params , collect ] ) ; return true ; } ) ;
1232
+ customHandler . dispose ( ) ;
1233
+ customHandler . dispose ( ) ;
1234
+ parser2 . parse ( INPUT ) ;
1235
+ chai . expect ( csi ) . eql ( [ [ 'm' , [ 1 , 31 ] , '' ] , [ 'm' , [ 0 ] , '' ] ] ) ;
1236
+ chai . expect ( csiCustom ) . eql ( [ ] , 'Should not use custom handler as it was disposed' ) ;
1237
+ } ) ;
1238
+ } ) ;
1172
1239
it ( 'EXECUTE handler' , function ( ) : void {
1173
1240
parser2 . setExecuteHandler ( '\n' , function ( ) : void {
1174
1241
exe . push ( '\n' ) ;
@@ -1196,6 +1263,73 @@ describe('EscapeSequenceParser', function (): void {
1196
1263
parser2 . parse ( INPUT ) ;
1197
1264
chai . expect ( osc ) . eql ( [ ] ) ;
1198
1265
} ) ;
1266
+ describe ( 'OSC custom handlers' , ( ) => {
1267
+ it ( 'Prevent fallback' , ( ) => {
1268
+ const oscCustom : [ number , string ] [ ] = [ ] ;
1269
+ parser2 . setOscHandler ( 1 , data => osc . push ( [ 1 , data ] ) ) ;
1270
+ parser2 . addOscHandler ( 1 , data => { oscCustom . push ( [ 1 , data ] ) ; return true ; } ) ;
1271
+ parser2 . parse ( INPUT ) ;
1272
+ chai . expect ( osc ) . eql ( [ ] , 'Should not fallback to original handler' ) ;
1273
+ chai . expect ( oscCustom ) . eql ( [ [ 1 , 'foo=bar' ] ] ) ;
1274
+ } ) ;
1275
+ it ( 'Allow fallback' , ( ) => {
1276
+ const oscCustom : [ number , string ] [ ] = [ ] ;
1277
+ parser2 . setOscHandler ( 1 , data => osc . push ( [ 1 , data ] ) ) ;
1278
+ parser2 . addOscHandler ( 1 , data => { oscCustom . push ( [ 1 , data ] ) ; return false ; } ) ;
1279
+ parser2 . parse ( INPUT ) ;
1280
+ chai . expect ( osc ) . eql ( [ [ 1 , 'foo=bar' ] ] , 'Should fallback to original handler' ) ;
1281
+ chai . expect ( oscCustom ) . eql ( [ [ 1 , 'foo=bar' ] ] ) ;
1282
+ } ) ;
1283
+ it ( 'Multiple custom handlers fallback once' , ( ) => {
1284
+ const oscCustom : [ number , string ] [ ] = [ ] ;
1285
+ const oscCustom2 : [ number , string ] [ ] = [ ] ;
1286
+ parser2 . setOscHandler ( 1 , data => osc . push ( [ 1 , data ] ) ) ;
1287
+ parser2 . addOscHandler ( 1 , data => { oscCustom . push ( [ 1 , data ] ) ; return true ; } ) ;
1288
+ parser2 . addOscHandler ( 1 , data => { oscCustom2 . push ( [ 1 , data ] ) ; return false ; } ) ;
1289
+ parser2 . parse ( INPUT ) ;
1290
+ chai . expect ( osc ) . eql ( [ ] , 'Should not fallback to original handler' ) ;
1291
+ chai . expect ( oscCustom ) . eql ( [ [ 1 , 'foo=bar' ] ] ) ;
1292
+ chai . expect ( oscCustom2 ) . eql ( [ [ 1 , 'foo=bar' ] ] ) ;
1293
+ } ) ;
1294
+ it ( 'Multiple custom handlers no fallback' , ( ) => {
1295
+ const oscCustom : [ number , string ] [ ] = [ ] ;
1296
+ const oscCustom2 : [ number , string ] [ ] = [ ] ;
1297
+ parser2 . setOscHandler ( 1 , data => osc . push ( [ 1 , data ] ) ) ;
1298
+ parser2 . addOscHandler ( 1 , data => { oscCustom . push ( [ 1 , data ] ) ; return true ; } ) ;
1299
+ parser2 . addOscHandler ( 1 , data => { oscCustom2 . push ( [ 1 , data ] ) ; return true ; } ) ;
1300
+ parser2 . parse ( INPUT ) ;
1301
+ chai . expect ( osc ) . eql ( [ ] , 'Should not fallback to original handler' ) ;
1302
+ chai . expect ( oscCustom ) . eql ( [ ] , 'Should not fallback once' ) ;
1303
+ chai . expect ( oscCustom2 ) . eql ( [ [ 1 , 'foo=bar' ] ] ) ;
1304
+ } ) ;
1305
+ it ( 'Execution order should go from latest handler down to the original' , ( ) => {
1306
+ const order : number [ ] = [ ] ;
1307
+ parser2 . setOscHandler ( 1 , ( ) => order . push ( 1 ) ) ;
1308
+ parser2 . addOscHandler ( 1 , ( ) => { order . push ( 2 ) ; return false ; } ) ;
1309
+ parser2 . addOscHandler ( 1 , ( ) => { order . push ( 3 ) ; return false ; } ) ;
1310
+ parser2 . parse ( '\x1b]1;foo=bar\x1b\\' ) ;
1311
+ chai . expect ( order ) . eql ( [ 3 , 2 , 1 ] ) ;
1312
+ } ) ;
1313
+ it ( 'Dispose should work' , ( ) => {
1314
+ const oscCustom : [ number , string ] [ ] = [ ] ;
1315
+ parser2 . setOscHandler ( 1 , data => osc . push ( [ 1 , data ] ) ) ;
1316
+ const customHandler = parser2 . addOscHandler ( 1 , data => { oscCustom . push ( [ 1 , data ] ) ; return true ; } ) ;
1317
+ customHandler . dispose ( ) ;
1318
+ parser2 . parse ( INPUT ) ;
1319
+ chai . expect ( osc ) . eql ( [ [ 1 , 'foo=bar' ] ] ) ;
1320
+ chai . expect ( oscCustom ) . eql ( [ ] , 'Should not use custom handler as it was disposed' ) ;
1321
+ } ) ;
1322
+ it ( 'Should not corrupt the parser when dispose is called twice' , ( ) => {
1323
+ const oscCustom : [ number , string ] [ ] = [ ] ;
1324
+ parser2 . setOscHandler ( 1 , data => osc . push ( [ 1 , data ] ) ) ;
1325
+ const customHandler = parser2 . addOscHandler ( 1 , data => { oscCustom . push ( [ 1 , data ] ) ; return true ; } ) ;
1326
+ customHandler . dispose ( ) ;
1327
+ customHandler . dispose ( ) ;
1328
+ parser2 . parse ( INPUT ) ;
1329
+ chai . expect ( osc ) . eql ( [ [ 1 , 'foo=bar' ] ] ) ;
1330
+ chai . expect ( oscCustom ) . eql ( [ ] , 'Should not use custom handler as it was disposed' ) ;
1331
+ } ) ;
1332
+ } ) ;
1199
1333
it ( 'DCS handler' , function ( ) : void {
1200
1334
parser2 . setDcsHandler ( '+p' , {
1201
1335
hook : function ( collect : string , params : number [ ] , flag : number ) : void {
0 commit comments