@@ -35,7 +35,7 @@ export function Element({ model }) {
35
35
return null ;
36
36
}
37
37
} else if ( model . tagName == "script" ) {
38
- return html `< ${ ScriptElement } script =${ model . children [ 0 ] } /> ` ;
38
+ return html `< ${ ScriptElement } model =${ model } /> ` ;
39
39
} else if ( model . importSource ) {
40
40
return html `< ${ ImportedElement } model =${ model } /> ` ;
41
41
} else {
@@ -58,10 +58,31 @@ function StandardElement({ model }) {
58
58
) ;
59
59
}
60
60
61
- function ScriptElement ( { script } ) {
62
- const el = React . useRef ( ) ;
63
- React . useEffect ( eval ( script ) , [ script ] ) ;
64
- return null ;
61
+ function ScriptElement ( { model } ) {
62
+ const ref = React . useRef ( ) ;
63
+ React . useEffect ( ( ) => {
64
+ if ( model ?. children ?. length > 1 ) {
65
+ console . error ( "Too many children for 'script' element." ) ;
66
+ }
67
+
68
+ let scriptContent = model ?. children ?. [ 0 ] ;
69
+
70
+ let scriptElement ;
71
+ if ( model . attributes ) {
72
+ scriptElement = document . createElement ( "script" ) ;
73
+ for ( const [ k , v ] of Object . entries ( model . attributes ) ) {
74
+ scriptElement . setAttribute ( k , v ) ;
75
+ }
76
+ scriptElement . appendChild ( document . createTextNode ( scriptContent ) ) ;
77
+ ref . current . appendChild ( scriptElement ) ;
78
+ } else {
79
+ let scriptResult = eval ( scriptContent ) ;
80
+ if ( typeof scriptResult == "function" ) {
81
+ return scriptResult ( ) ;
82
+ }
83
+ }
84
+ } , [ model . key ] ) ;
85
+ return html `< div ref =${ ref } / > ` ;
65
86
}
66
87
67
88
function ImportedElement ( { model } ) {
0 commit comments