@@ -40,6 +40,10 @@ import { KernelConnectionMetadata } from './kernels/types';
4040
4141// tslint:disable-next-line: no-require-imports
4242import cloneDeep = require( 'lodash/cloneDeep' ) ;
43+ // tslint:disable-next-line: no-require-imports
44+ import escape = require( 'lodash/escape' ) ;
45+ // tslint:disable-next-line: no-require-imports
46+ import unescape = require( 'lodash/unescape' ) ;
4347import { concatMultilineString , formatStreamText } from '../../../datascience-ui/common' ;
4448import { RefBool } from '../../common/refBool' ;
4549import { PythonEnvironment } from '../../pythonEnvironments/info' ;
@@ -783,12 +787,12 @@ export class JupyterNotebookBase implements INotebook {
783787 outputs . forEach ( ( o ) => {
784788 if ( o . output_type === 'stream' ) {
785789 const stream = o as nbformat . IStream ;
786- result = result . concat ( formatStreamText ( concatMultilineString ( stream . text , true ) ) ) ;
790+ result = result . concat ( formatStreamText ( unescape ( concatMultilineString ( stream . text , true ) ) ) ) ;
787791 } else {
788792 const data = o . data ;
789793 if ( data && data . hasOwnProperty ( 'text/plain' ) ) {
790794 // tslint:disable-next-line:no-any
791- result = result . concat ( ( data as any ) [ 'text/plain' ] ) ;
795+ result = result . concat ( unescape ( ( data as any ) [ 'text/plain' ] ) ) ;
792796 }
793797 }
794798 } ) ;
@@ -1233,7 +1237,7 @@ export class JupyterNotebookBase implements INotebook {
12331237 ) {
12341238 // Check our length on text output
12351239 if ( msg . content . data && msg . content . data . hasOwnProperty ( 'text/plain' ) ) {
1236- msg . content . data [ 'text/plain' ] = trimFunc ( msg . content . data [ 'text/plain' ] as string ) ;
1240+ msg . content . data [ 'text/plain' ] = escape ( trimFunc ( msg . content . data [ 'text/plain' ] as string ) ) ;
12371241 }
12381242
12391243 this . addToCellData (
@@ -1262,7 +1266,7 @@ export class JupyterNotebookBase implements INotebook {
12621266 if ( o . data && o . data . hasOwnProperty ( 'text/plain' ) ) {
12631267 // tslint:disable-next-line: no-any
12641268 const str = ( o . data as any ) [ 'text/plain' ] . toString ( ) ;
1265- const data = trimFunc ( str ) as string ;
1269+ const data = escape ( trimFunc ( str ) ) as string ;
12661270 this . addToCellData (
12671271 cell ,
12681272 {
@@ -1310,13 +1314,13 @@ export class JupyterNotebookBase implements INotebook {
13101314 : undefined ;
13111315 if ( existing ) {
13121316 // tslint:disable-next-line:restrict-plus-operands
1313- existing . text = existing . text + msg . content . text ;
1317+ existing . text = existing . text + escape ( msg . content . text ) ;
13141318 const originalText = formatStreamText ( concatMultilineString ( existing . text ) ) ;
13151319 originalTextLength = originalText . length ;
13161320 existing . text = trimFunc ( originalText ) ;
13171321 trimmedTextLength = existing . text . length ;
13181322 } else {
1319- const originalText = formatStreamText ( concatMultilineString ( msg . content . text ) ) ;
1323+ const originalText = formatStreamText ( concatMultilineString ( escape ( msg . content . text ) ) ) ;
13201324 originalTextLength = originalText . length ;
13211325 // Create a new stream entry
13221326 const output : nbformat . IStream = {
@@ -1346,6 +1350,11 @@ export class JupyterNotebookBase implements INotebook {
13461350 }
13471351
13481352 private handleDisplayData ( msg : KernelMessage . IDisplayDataMsg , clearState : RefBool , cell : ICell ) {
1353+ // Escape text output
1354+ if ( msg . content . data && msg . content . data . hasOwnProperty ( 'text/plain' ) ) {
1355+ msg . content . data [ 'text/plain' ] = escape ( msg . content . data [ 'text/plain' ] as string ) ;
1356+ }
1357+
13491358 const output : nbformat . IDisplayData = {
13501359 output_type : 'display_data' ,
13511360 data : msg . content . data ,
@@ -1393,9 +1402,9 @@ export class JupyterNotebookBase implements INotebook {
13931402 private handleError ( msg : KernelMessage . IErrorMsg , clearState : RefBool , cell : ICell ) {
13941403 const output : nbformat . IError = {
13951404 output_type : 'error' ,
1396- ename : msg . content . ename ,
1397- evalue : msg . content . evalue ,
1398- traceback : msg . content . traceback
1405+ ename : escape ( msg . content . ename ) ,
1406+ evalue : escape ( msg . content . evalue ) ,
1407+ traceback : msg . content . traceback . map ( escape )
13991408 } ;
14001409 this . addToCellData ( cell , output , clearState ) ;
14011410 cell . state = CellState . error ;
0 commit comments