@@ -4,11 +4,6 @@ import { BaseKernel, type IKernel } from '@jupyterlite/services';
44
55import { PromiseDelegate } from '@lumino/coreutils' ;
66
7- /**
8- * The mimetype for mime bundle results
9- */
10- const MIME_TYPE = 'text/html-sandboxed' ;
11-
127/**
138 * A kernel for making p5 sketches in the browser
149 */
@@ -158,7 +153,6 @@ export class P5Kernel extends BaseKernel {
158153 const magics = await this . _magics ( ) ;
159154 const { data, metadata } = magics ;
160155 this . _parentHeaders . forEach ( h => {
161- this . clearOutput ( { wait : false } ) ;
162156 this . updateDisplayData (
163157 {
164158 data,
@@ -290,23 +284,27 @@ export class P5Kernel extends BaseKernel {
290284 // add metadata
291285 const re = / ^ % s h o w (?: ( .+ ) \s + ( .+ ) ) ? \s * $ / ;
292286 const matches = code . match ( re ) ;
293- const width = matches ?. [ 1 ] ?? undefined ;
294- const height = matches ?. [ 2 ] ?? undefined ;
287+ const width = matches ?. [ 1 ] ?? '100%' ;
288+ const height = matches ?. [ 2 ] ?? '400px' ;
289+ // Properly escape the srcdoc content
290+ const srcdocContent = [
291+ '<body style="overflow: hidden; margin: 0; padding: 0;">' ,
292+ `<script>${ script } </script>` ,
293+ '</body>'
294+ ] . join ( '' ) ;
295+
296+ // Escape the srcdoc attribute value
297+ const escapedSrcdoc = srcdocContent
298+ . replace ( / & / g, '&' )
299+ . replace ( / ' / g, ''' )
300+ . replace ( / " / g, '"' ) ;
301+
295302 return {
296303 execution_count : this . executionCount ,
297304 data : {
298- [ MIME_TYPE ] : [
299- '<body style="overflow: hidden;">' ,
300- `<script>${ script } </script>` ,
301- '</body>'
302- ] . join ( '\n' )
305+ 'text/html' : `<iframe width="${ width } " height="${ height } " frameborder="0" srcdoc="${ escapedSrcdoc } "></iframe>`
303306 } ,
304- metadata : {
305- [ MIME_TYPE ] : {
306- width,
307- height
308- }
309- }
307+ metadata : { }
310308 } ;
311309 }
312310
0 commit comments