@@ -54,6 +54,12 @@ export function patchDocument (
5454
5555 return patchDocumentEffect ( appName , microAppWindow )
5656}
57+ function getElementDocument ( microDocument : Document , rawDocument : Document ) : Document {
58+ if ( microApp ?. options ?. disableIframeRootDocument ) {
59+ return rawDocument
60+ }
61+ return microDocument
62+ }
5763
5864function patchDocumentPrototype ( appName : string , microAppWindow : microAppWindowType ) : void {
5965 const rawDocument = globalEnv . rawDocument
@@ -88,7 +94,7 @@ function patchDocumentPrototype (appName: string, microAppWindow: microAppWindow
8894 tagName : string ,
8995 options ?: ElementCreationOptions ,
9096 ) : HTMLElement {
91- let element = rawMicroCreateElement . call ( this , tagName , options )
97+ let element = rawMicroCreateElement . call ( getElementDocument ( this , rawDocument ) , tagName , options )
9298 if ( isWebComponentElement ( element ) ) {
9399 element = rawMicroCreateElement . call ( rawDocument , tagName , options )
94100 }
@@ -100,22 +106,22 @@ function patchDocumentPrototype (appName: string, microAppWindow: microAppWindow
100106 name : string ,
101107 options ?: string | ElementCreationOptions ,
102108 ) : HTMLElement {
103- const element = rawMicroCreateElementNS . call ( this , namespaceURI , name , options )
109+ const element = rawMicroCreateElementNS . call ( getElementDocument ( this , rawDocument ) , namespaceURI , name , options )
104110 return updateElementInfo ( element , appName )
105111 }
106112
107113 microRootDocument . prototype . createTextNode = function createTextNode ( data : string ) : Text {
108- const element = rawMicroCreateTextNode . call ( this , data )
114+ const element = rawMicroCreateTextNode . call ( getElementDocument ( this , rawDocument ) , data )
109115 return updateElementInfo < Text > ( element , appName )
110116 }
111117
112118 microRootDocument . prototype . createDocumentFragment = function createDocumentFragment ( ) : DocumentFragment {
113- const element = rawMicroCreateDocumentFragment . call ( this )
119+ const element = rawMicroCreateDocumentFragment . call ( getElementDocument ( this , rawDocument ) )
114120 return updateElementInfo ( element , appName )
115121 }
116122
117123 microRootDocument . prototype . createComment = function createComment ( data : string ) : Comment {
118- const element = rawMicroCreateComment . call ( this , data )
124+ const element = rawMicroCreateComment . call ( getElementDocument ( this , rawDocument ) , data )
119125 return updateElementInfo < Comment > ( element , appName )
120126 }
121127
@@ -184,7 +190,7 @@ function patchDocumentPrototype (appName: string, microAppWindow: microAppWindow
184190 }
185191
186192 try {
187- return querySelector . call ( this , `#${ key } ` )
193+ return querySelector . call ( getElementDocument ( this , rawDocument ) , `#${ key } ` )
188194 } catch {
189195 return rawMicroGetElementById . call ( _this , key )
190196 }
@@ -197,14 +203,14 @@ function patchDocumentPrototype (appName: string, microAppWindow: microAppWindow
197203 }
198204
199205 try {
200- return querySelectorAll . call ( this , `.${ key } ` )
206+ return querySelectorAll . call ( getElementDocument ( this , rawDocument ) , `.${ key } ` )
201207 } catch {
202208 return rawMicroGetElementsByClassName . call ( _this , key )
203209 }
204210 }
205211
206212 microRootDocument . prototype . getElementsByTagName = function getElementsByTagName ( key : string ) : HTMLCollectionOf < Element > {
207- const _this = getBindTarget ( this )
213+ const _this = getBindTarget ( getElementDocument ( this , rawDocument ) )
208214 if (
209215 isUniqueElement ( key ) ||
210216 isInvalidQuerySelectorKey ( key )
@@ -216,20 +222,20 @@ function patchDocumentPrototype (appName: string, microAppWindow: microAppWindow
216222 }
217223
218224 try {
219- return querySelectorAll . call ( this , key )
225+ return querySelectorAll . call ( getElementDocument ( this , rawDocument ) , key )
220226 } catch {
221227 return rawMicroGetElementsByTagName . call ( _this , key )
222228 }
223229 }
224230
225231 microRootDocument . prototype . getElementsByName = function getElementsByName ( key : string ) : NodeListOf < HTMLElement > {
226- const _this = getBindTarget ( this )
232+ const _this = getBindTarget ( getElementDocument ( this , rawDocument ) )
227233 if ( isInvalidQuerySelectorKey ( key ) ) {
228234 return rawMicroGetElementsByName . call ( _this , key )
229235 }
230236
231237 try {
232- return querySelectorAll . call ( this , `[name=${ key } ]` )
238+ return querySelectorAll . call ( getElementDocument ( this , rawDocument ) , `[name=${ key } ]` )
233239 } catch {
234240 return rawMicroGetElementsByName . call ( _this , key )
235241 }
0 commit comments