Skip to content

Commit

Permalink
fix: 修复了对iframe src属性的错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Oct 20, 2021
1 parent 5a02596 commit 86f2d94
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
3 changes: 3 additions & 0 deletions examples/children/vite/index.html
Expand Up @@ -5,6 +5,9 @@
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<script>
console.log('common script in vite')
</script>
</head>
<body>
<div id="vite-app"></div>
Expand Down
6 changes: 5 additions & 1 deletion src/libs/utils.ts
Expand Up @@ -99,7 +99,11 @@ export function getEffectivePath (url: string): string {
* @param baseURI base url(app.url)
*/
export function CompletionPath (path: string, baseURI: string): string {
if (/^((((ht|f)tps?)|file):)?\/\//.test(path) || /^(data|blob):/.test(path)) return path
if (
!path ||
/^((((ht|f)tps?)|file):)?\/\//.test(path) ||
/^(data|blob):/.test(path)
) return path

return new URL(path, getEffectivePath(addProtocol(baseURI))).toString()
}
Expand Down
6 changes: 2 additions & 4 deletions src/source/index.ts
Expand Up @@ -53,10 +53,8 @@ function flatChildren (
extractScriptElement(dom, parent, app)
} else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
parent.removeChild(dom)
} else {
if (/^(img|iframe)$/i.test(dom.tagName) && dom.hasAttribute('src')) {
dom.setAttribute('src', CompletionPath(dom.getAttribute('src')!, app.url))
}
} else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
dom.setAttribute('src', CompletionPath(dom.getAttribute('src')!, app.url))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/source/patch.ts
Expand Up @@ -219,7 +219,7 @@ export function patchElementPrototypeMethods (): void {
}
} else if (
(
(key === 'src' && /^(img|iframe|script)$/i.test(this.tagName)) ||
(key === 'src' && /^(img|script)$/i.test(this.tagName)) ||
(key === 'href' && /^link$/i.test(this.tagName))
) &&
this.__MICRO_APP_NAME__ &&
Expand Down
14 changes: 8 additions & 6 deletions src/source/scripts.ts
Expand Up @@ -217,8 +217,8 @@ export function runScript (
callback?: Func,
): any {
try {
code = bindScope(url, code, app)
if (app.inline) {
code = bindScope(url, code, app, module)
if (app.inline || module) {
const scriptElement = pureCreateElement('script')
setInlinScriptContent(url, code, module, scriptElement, callback)
if (isDynamic) return scriptElement
Expand Down Expand Up @@ -260,7 +260,7 @@ export function runDynamicRemoteScript (
}

let replaceElement: Comment | HTMLScriptElement
if (app.inline) {
if (app.inline || info.module) {
replaceElement = pureCreateElement('script')
} else {
replaceElement = document.createComment(`dynamic script with src='${url}' extract by micro-app`)
Expand All @@ -271,8 +271,8 @@ export function runDynamicRemoteScript (
app.source.scripts.set(url, info)
if (info.isGlobal) globalScripts.set(url, code)
try {
code = bindScope(url, code, app)
if (app.inline) {
code = bindScope(url, code, app, info.module)
if (app.inline || info.module) {
setInlinScriptContent(url, code, info.module, replaceElement as HTMLScriptElement)
} else {
Function(code)()
Expand Down Expand Up @@ -321,16 +321,18 @@ function setInlinScriptContent (
* @param url script address
* @param code code
* @param app app
* @param module type='module' of script
*/
function bindScope (
url: string,
code: string,
app: AppInterface,
module: boolean,
): string {
if (typeof microApp.plugins === 'object') {
code = usePlugins(url, code, app.name, microApp.plugins)
}
if (app.sandBox) {
if (app.sandBox && !module) {
globalEnv.rawWindow.__MICRO_APP_PROXY_WINDOW__ = app.sandBox.proxyWindow
return `;(function(window, self){with(window){;${code}\n}}).call(window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__);`
}
Expand Down

0 comments on commit 86f2d94

Please sign in to comment.