@@ -4,10 +4,7 @@ import {
4
4
CompletionPath ,
5
5
injectFiberTask ,
6
6
serialExecFiberTasks ,
7
- isLinkElement ,
8
- isScriptElement ,
9
- isStyleElement ,
10
- isImageElement ,
7
+ isBodyElement ,
11
8
} from '../libs/utils'
12
9
import {
13
10
extractLinkFromHtml ,
@@ -24,62 +21,64 @@ import globalEnv from '../libs/global_env'
24
21
25
22
/**
26
23
* Recursively process each child element
27
- * @param parent parent element
24
+ * @param body body element
28
25
* @param app app
29
26
* @param microAppHead micro-app-head element
30
27
*/
31
- function flatChildren (
32
- parent : HTMLElement ,
28
+ function flatBodyChildren (
29
+ body : HTMLElement ,
33
30
app : AppInterface ,
34
- microAppHead : Element ,
35
31
fiberStyleTasks : fiberTasks ,
36
32
) : void {
37
- const children = Array . from ( parent . children )
33
+ if ( ! body || ! isBodyElement ( body ) ) {
34
+ return
35
+ }
36
+ const links = Array . from ( body . getElementsByTagName ( 'link' ) )
37
+
38
+ links . map ( ( dom ) => {
39
+ if ( dom . hasAttribute ( 'exclude' ) || checkExcludeUrl ( dom . getAttribute ( 'href' ) , app . name ) ) {
40
+ dom . parentElement ! . replaceChild ( document . createComment ( 'link element with exclude attribute ignored by micro-app' ) , dom )
41
+ } else if ( ! ( dom . hasAttribute ( 'ignore' ) || checkIgnoreUrl ( dom . getAttribute ( 'href' ) , app . name ) ) ) {
42
+ extractLinkFromHtml ( dom , dom . parentElement , app )
43
+ } else if ( dom . hasAttribute ( 'href' ) ) {
44
+ globalEnv . rawSetAttribute . call ( dom , 'href' , CompletionPath ( dom . getAttribute ( 'href' ) ! , app . url ) )
45
+ }
46
+ return dom
47
+ } )
48
+
49
+ const styles = Array . from ( body . getElementsByTagName ( 'style' ) )
38
50
39
- children . length && children . forEach ( ( child ) => {
40
- flatChildren ( child as HTMLElement , app , microAppHead , fiberStyleTasks )
51
+ styles . map ( ( dom ) => {
52
+ if ( dom . hasAttribute ( 'exclude' ) ) {
53
+ dom . parentElement ! . replaceChild ( document . createComment ( 'style element with exclude attribute ignored by micro-app' ) , dom )
54
+ } else if ( app . scopecss && ! dom . hasAttribute ( 'ignore' ) ) {
55
+ injectFiberTask ( fiberStyleTasks , ( ) => scopedCSS ( dom , app ) )
56
+ }
57
+ return dom
41
58
} )
42
59
43
- for ( const dom of children ) {
44
- if ( isLinkElement ( dom ) ) {
45
- if ( dom . hasAttribute ( 'exclude' ) || checkExcludeUrl ( dom . getAttribute ( 'href' ) , app . name ) ) {
46
- parent . replaceChild ( document . createComment ( 'link element with exclude attribute ignored by micro-app' ) , dom )
47
- } else if ( ! ( dom . hasAttribute ( 'ignore' ) || checkIgnoreUrl ( dom . getAttribute ( 'href' ) , app . name ) ) ) {
48
- extractLinkFromHtml ( dom , parent , app )
49
- } else if ( dom . hasAttribute ( 'href' ) ) {
50
- globalEnv . rawSetAttribute . call ( dom , 'href' , CompletionPath ( dom . getAttribute ( 'href' ) ! , app . url ) )
51
- }
52
- } else if ( isStyleElement ( dom ) ) {
53
- if ( dom . hasAttribute ( 'exclude' ) ) {
54
- parent . replaceChild ( document . createComment ( 'style element with exclude attribute ignored by micro-app' ) , dom )
55
- } else if ( app . scopecss && ! dom . hasAttribute ( 'ignore' ) ) {
56
- injectFiberTask ( fiberStyleTasks , ( ) => scopedCSS ( dom , app ) )
57
- }
58
- } else if ( isScriptElement ( dom ) ) {
59
- extractScriptElement ( dom , parent , app )
60
- } else if ( isImageElement ( dom ) && dom . hasAttribute ( 'src' ) ) {
60
+ const scripts = Array . from ( body . getElementsByTagName ( 'script' ) )
61
+
62
+ scripts . map ( ( dom ) => {
63
+ extractScriptElement ( dom , dom . parentElement , app )
64
+ return dom
65
+ } )
66
+ const images = Array . from ( body . getElementsByTagName ( 'img' ) )
67
+
68
+ images . map ( ( dom ) => {
69
+ if ( dom . hasAttribute ( 'src' ) ) {
61
70
globalEnv . rawSetAttribute . call ( dom , 'src' , CompletionPath ( dom . getAttribute ( 'src' ) ! , app . url ) )
62
71
}
63
- /**
64
- * Don't remove meta and title, they have some special scenes
65
- * e.g.
66
- * document.querySelector('meta[name="viewport"]') // for flexible
67
- * document.querySelector('meta[name="baseurl"]').baseurl // for api request
68
- *
69
- * Title point to main app title, child app title used to be compatible with some special scenes
70
- */
71
- // else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
72
- // parent.removeChild(dom)
73
- // }
74
- }
72
+ return dom
73
+ } )
75
74
}
76
75
77
76
/**
78
77
* Extract link and script, bind style scope
79
78
* @param htmlStr html string
80
79
* @param app app
81
80
*/
82
- export function extractSourceDom ( htmlStr : string , app : AppInterface ) : void {
81
+ export function extractSourceDom ( htmlStr : string , app : AppInterface ) : void {
83
82
const wrapElement = app . parseHtmlString ( htmlStr )
84
83
const microAppHead = globalEnv . rawElementQuerySelector . call ( wrapElement , 'micro-app-head' )
85
84
const microAppBody = globalEnv . rawElementQuerySelector . call ( wrapElement , 'micro-app-body' )
@@ -92,7 +91,7 @@ export function extractSourceDom (htmlStr: string, app: AppInterface): void {
92
91
93
92
const fiberStyleTasks : fiberTasks = app . isPrefetch || app . fiber ? [ ] : null
94
93
95
- flatChildren ( wrapElement , app , microAppHead , fiberStyleTasks )
94
+ flatBodyChildren ( wrapElement , app , fiberStyleTasks )
96
95
97
96
/**
98
97
* Style and link are parallel, as it takes a lot of time for link to request resources. During this period, style processing can be performed to improve efficiency.
0 commit comments