@@ -5,7 +5,7 @@ import { Compartment, StateCommand } from '@codemirror/state';
5
5
import { keymap } from '@codemirror/view' ;
6
6
import { defaultKeymap } from '@codemirror/commands' ;
7
7
import { oneDarkTheme } from '@codemirror/theme-one-dark' ;
8
- import { addClasses , htmlDecode } from '../utils' ;
8
+ import { getAttribute , addClasses , htmlDecode } from '../utils' ;
9
9
import { BaseEvalElement } from './base' ;
10
10
import type { Runtime } from '../runtime' ;
11
11
import { getLogger } from '../logger' ;
@@ -25,7 +25,11 @@ export function make_PyRepl(runtime: Runtime) {
25
25
26
26
let initialTheme : string ;
27
27
function getEditorTheme ( el : BaseEvalElement ) : string {
28
- return initialTheme || ( initialTheme = el . getAttribute ( 'theme' ) ) ;
28
+ const theme = getAttribute ( el , 'theme' ) ;
29
+ if ( ! initialTheme && theme ) {
30
+ initialTheme = theme ;
31
+ }
32
+ return initialTheme ;
29
33
}
30
34
31
35
class PyRepl extends BaseEvalElement {
@@ -120,26 +124,44 @@ export function make_PyRepl(runtime: Runtime) {
120
124
this . setAttribute ( 'root' , this . id ) ;
121
125
}
122
126
123
- if ( this . hasAttribute ( 'output' ) ) {
124
- this . errorElement = this . outputElement = document . getElementById ( this . getAttribute ( 'output' ) ) ;
127
+ const output = getAttribute ( this , "output" )
128
+ if ( output ) {
129
+ const el = document . getElementById ( output ) ;
130
+ if ( el ) {
131
+ this . errorElement = el ;
132
+ this . outputElement = el
133
+ }
125
134
} else {
126
- if ( this . hasAttribute ( 'std-out' ) ) {
127
- this . outputElement = document . getElementById ( this . getAttribute ( 'std-out' ) ) ;
135
+ const stdOut = getAttribute ( this , "std-out" ) ;
136
+ if ( stdOut ) {
137
+ const el = document . getElementById ( stdOut ) ;
138
+ if ( el ) {
139
+ this . outputElement = el
140
+ }
128
141
} else {
129
142
// In this case neither output or std-out have been provided so we need
130
143
// to create a new output div to output to
131
144
this . outputElement = document . createElement ( 'div' ) ;
132
145
this . outputElement . classList . add ( 'output' ) ;
133
146
this . outputElement . hidden = true ;
134
- this . outputElement . id = this . id + '-' + this . getAttribute ( 'exec-id' ) ;
147
+ const stdOut = getAttribute ( this , "exec-id" ) || "" ;
148
+ this . outputElement . id = this . id + '-' + stdOut ;
135
149
136
150
// add the output div id if there's not output pre-defined
137
151
mainDiv . appendChild ( this . outputElement ) ;
138
152
}
139
153
140
- this . errorElement = this . hasAttribute ( 'std-err' )
141
- ? document . getElementById ( this . getAttribute ( 'std-err' ) )
142
- : this . outputElement ;
154
+ const stdErr = getAttribute ( this , "std-err" ) ;
155
+ if ( stdErr ) {
156
+ const el = document . getElementById ( stdErr ) ;
157
+ if ( el ) {
158
+ this . errorElement = el ;
159
+ } else {
160
+ this . errorElement = this . outputElement
161
+ }
162
+ } else {
163
+ this . errorElement = this . outputElement
164
+ }
143
165
}
144
166
145
167
this . appendChild ( mainDiv ) ;
@@ -178,13 +200,15 @@ export function make_PyRepl(runtime: Runtime) {
178
200
this . removeAttribute ( 'auto-generate' ) ;
179
201
}
180
202
181
- if ( this . hasAttribute ( 'output-mode' ) ) {
182
- newPyRepl . setAttribute ( 'output-mode' , this . getAttribute ( 'output-mode' ) ) ;
203
+ const outputMode = getAttribute ( this , 'output-mode' )
204
+ if ( outputMode ) {
205
+ newPyRepl . setAttribute ( 'output-mode' , outputMode ) ;
183
206
}
184
207
185
208
const addReplAttribute = ( attribute : string ) => {
186
- if ( this . hasAttribute ( attribute ) ) {
187
- newPyRepl . setAttribute ( attribute , this . getAttribute ( attribute ) ) ;
209
+ const attr = getAttribute ( this , attribute )
210
+ if ( attr ) {
211
+ newPyRepl . setAttribute ( attribute , attr ) ;
188
212
}
189
213
} ;
190
214
@@ -193,7 +217,9 @@ export function make_PyRepl(runtime: Runtime) {
193
217
addReplAttribute ( 'std-err' ) ;
194
218
195
219
newPyRepl . setAttribute ( 'exec-id' , nextExecId . toString ( ) ) ;
196
- this . parentElement . appendChild ( newPyRepl ) ;
220
+ if ( this . parentElement ) {
221
+ this . parentElement . appendChild ( newPyRepl ) ;
222
+ }
197
223
}
198
224
}
199
225
0 commit comments