11import { getAttributes } from '@cocreate/utils' ;
22import { storage } from './getValue' ;
33
4- HTMLElement . prototype . setValue = function ( value ) {
5- setValue ( this , value )
4+ HTMLElement . prototype . setValue = function ( value , dispatch ) {
5+ setValue ( this , value , dispatch )
66} ;
77
8- HTMLInputElement . prototype . setValue = function ( value ) {
9- setValue ( this , value )
8+ HTMLInputElement . prototype . setValue = function ( value , dispatch ) {
9+ setValue ( this , value , dispatch )
1010} ;
1111
12- HTMLHeadingElement . prototype . setValue = function ( value ) {
13- setValue ( this , value )
12+ HTMLHeadingElement . prototype . setValue = function ( value , dispatch ) {
13+ setValue ( this , value , dispatch )
1414} ;
1515
1616// TODO: check if using a a switch case will provide better performance
17- const setValue = ( el , value ) => {
17+ const setValue = ( el , value , dispatch ) => {
1818 if ( value === null || value === undefined ) return ;
1919 if ( el . hasAttribute ( 'component' ) || el . hasAttribute ( 'plugin' ) )
2020 return storage . set ( el , value )
@@ -78,7 +78,7 @@ const setValue = (el, value) => {
7878
7979 el . value = value ;
8080 }
81- dispatchEvents ( el )
81+ dispatchEvents ( el , dispatch )
8282 } else if ( el . tagName === 'IMG' || el . tagName === 'SOURCE' ) {
8383 el . src = value ;
8484 } else if ( el . tagName === 'IFRAME' ) {
@@ -119,6 +119,12 @@ const setValue = (el, value) => {
119119 }
120120 } else
121121 el . innerHTML = newElement . innerHTML ;
122+
123+ let scripts = el . querySelectorAll ( 'script' ) ;
124+ for ( let script of scripts ) {
125+ setScript ( script )
126+ }
127+
122128 }
123129
124130 if ( el . hasAttribute ( "value" ) ) {
@@ -127,7 +133,7 @@ const setValue = (el, value) => {
127133 }
128134
129135 if ( el . getAttribute ( 'contenteditable' ) )
130- dispatchEvents ( el ) ;
136+ dispatchEvents ( el , dispatch ) ;
131137
132138 if ( el . tagName == 'HEAD' || el . tagName == 'BODY' ) {
133139 el . removeAttribute ( 'array' ) ;
@@ -150,8 +156,26 @@ function setState(el) {
150156}
151157
152158function setScript ( script , value ) {
159+ let srcAttribute = script . src
160+ if ( srcAttribute ) {
161+ let pageOrigin = window . location . origin ;
162+ let srcOrigin ;
163+
164+ try {
165+ srcOrigin = new URL ( srcAttribute , document . baseURI ) . origin ;
166+ } catch ( e ) {
167+ // Handle invalid URLs
168+ console . error ( "Invalid URL in src attribute:" , srcAttribute ) ;
169+ return ;
170+ }
171+ if ( pageOrigin !== srcOrigin )
172+ return
173+ }
174+
153175 let newScript = document . createElement ( 'script' ) ;
154- newScript . attributes = script . attributes ;
176+ for ( let attr of script . attributes ) {
177+ newScript . setAttribute ( attr . name , attr . value ) ;
178+ }
155179 newScript . innerHTML = script . innerHTML ;
156180 if ( value ) {
157181 if ( script . hasAttribute ( "src" ) )
@@ -168,11 +192,11 @@ function __decryptPassword(str) {
168192 return decode_str ;
169193}
170194
171- function dispatchEvents ( el ) {
195+ function dispatchEvents ( el , skip = true ) {
172196 let inputEvent = new CustomEvent ( 'input' , {
173197 bubbles : true ,
174198 detail : {
175- skip : true
199+ skip
176200 } ,
177201 } ) ;
178202
@@ -185,7 +209,7 @@ function dispatchEvents(el) {
185209 let changeEvent = new CustomEvent ( 'change' , {
186210 bubbles : true ,
187211 detail : {
188- skip : true
212+ skip
189213 } ,
190214 } ) ;
191215
0 commit comments