diff --git a/Contact.md b/Contact.md index f1a409f0..8733a527 100644 --- a/Contact.md +++ b/Contact.md @@ -1,7 +1,7 @@ 欢迎小伙伴们加入micro-app微信群交流^ ^ -![IMG_0607](https://github.com/user-attachments/assets/a976245d-2d5e-460e-8bcb-421dbc31e494) +![IMG_0722](https://github.com/user-attachments/assets/4ca7569d-328d-4f28-aabe-f946b23bff3b) diff --git a/docs/zh-cn/changelog.md b/docs/zh-cn/changelog.md index 3757275c..540990f8 100644 --- a/docs/zh-cn/changelog.md +++ b/docs/zh-cn/changelog.md @@ -8,6 +8,14 @@ --- +### 1.0.0-rc.10 + +`2024-9-14` + +- **Bug Fix** + - 🐞 修复 子应用a标签href属性丢失,[issue 1358](https://github.com/micro-zoe/micro-app/issues/1358)。 + + ### 1.0.0-rc.9 `2024-9-10` diff --git a/src/libs/global_env.ts b/src/libs/global_env.ts index e9790e4c..3409e32a 100644 --- a/src/libs/global_env.ts +++ b/src/libs/global_env.ts @@ -14,7 +14,6 @@ import { import { updateElementInfo, } from '../sandbox/adapter' -import '../proxies/worker' declare global { interface Node { diff --git a/src/libs/utils.ts b/src/libs/utils.ts index 65d7fb64..b489d71c 100644 --- a/src/libs/utils.ts +++ b/src/libs/utils.ts @@ -125,7 +125,7 @@ export function isNode(target: unknown): target is Node { return target instanceof Node || isNumber((target as Node)?.nodeType) } -export function isCanvasElement(target: unknown): target is HTMLAnchorElement { +export function isCanvasElement(target: unknown): target is HTMLCanvasElement { return toTypeString(target) === '[object HTMLCanvasElement]' } @@ -133,11 +133,11 @@ export function isAnchorElement(target: unknown): target is HTMLAnchorElement { return toTypeString(target) === '[object HTMLAnchorElement]' } -export function isAudioElement(target: unknown): target is HTMLAnchorElement { +export function isAudioElement(target: unknown): target is HTMLAudioElement { return toTypeString(target) === '[object HTMLAudioElement]' } -export function isVideoElement(target: unknown): target is HTMLAnchorElement { +export function isVideoElement(target: unknown): target is HTMLVideoElement { return toTypeString(target) === '[object HTMLVideoElement]' } @@ -777,10 +777,3 @@ export function formatEventType(type: string, appName: string): string { export function isEmptyObject(target: unknown): boolean { return isPlainObject(target) ? !Object.keys(target).length : true } - -/** - * - * @param {string} url input url - * @returns {boolean} is relative path - */ -export const isRelativePath = (url: string) => !/^https?:\/\//i.test(url) && !/^\/\//i.test(url) diff --git a/src/proxies/worker.ts b/src/proxies/worker.ts index 957c2b44..a270887e 100644 --- a/src/proxies/worker.ts +++ b/src/proxies/worker.ts @@ -79,3 +79,5 @@ const WorkerProxy = new Proxy(originalWorker, { // @ts-ignore window.Worker = WorkerProxy + +export default WorkerProxy diff --git a/src/sandbox/adapter.ts b/src/sandbox/adapter.ts index 6d002fb7..39f51bec 100644 --- a/src/sandbox/adapter.ts +++ b/src/sandbox/adapter.ts @@ -11,7 +11,6 @@ import { getPreventSetState, throttleDeferForIframeAppName, isAnchorElement, - isRelativePath, isImageElement, isVideoElement, isAudioElement, @@ -161,17 +160,13 @@ export function updateElementInfo (node: T, appName: string | null): T { // a 标签 const microApp = AppManager.getInstance().get(appName) if (microApp) { - let originalHref = node.href props.href = { get() { - if (isRelativePath(originalHref)) { - return `${microApp.url}${originalHref}` - } - return originalHref + return this.getAttribute('href') }, set(value: string) { - originalHref = value - } + this.setAttribute('href', value) + }, } } } diff --git a/src/sandbox/iframe/element.ts b/src/sandbox/iframe/element.ts index 0be7c8e9..1614f8ff 100644 --- a/src/sandbox/iframe/element.ts +++ b/src/sandbox/iframe/element.ts @@ -240,7 +240,7 @@ function patchIframeAttribute (url: string, microAppWindow: microAppWindowType): } else { if ( ((key === 'src' || key === 'srcset') && /^(img|script|video|audio|source|embed)$/i.test(this.tagName)) || - (key === 'href' && /^(link|image)$/i.test(this.tagName)) + (key === 'href' && /^(a|link|image)$/i.test(this.tagName)) ) { value = CompletionPath(value, url) } diff --git a/src/sandbox/iframe/window.ts b/src/sandbox/iframe/window.ts index 8eba5601..145059c9 100644 --- a/src/sandbox/iframe/window.ts +++ b/src/sandbox/iframe/window.ts @@ -23,6 +23,7 @@ import { escape2RawWindowKeys, escape2RawWindowRegExpKeys, } from './special_key' +import WorkerProxy from '../../proxies/worker' /** * patch window of child app @@ -153,6 +154,12 @@ function createProxyWindow ( const rawWindow = globalEnv.rawWindow const customProperties = new Set() + Object.defineProperty(microAppWindow, 'Worker', { + value: WorkerProxy, + configurable: true, + writable: true, + }) + /** * proxyWindow will only take effect in certain scenes, such as window.key * e.g: @@ -162,6 +169,9 @@ function createProxyWindow ( */ const proxyWindow = new Proxy(microAppWindow, { get: (target: microAppWindowType, key: PropertyKey): unknown => { + if (key === 'Worker') { + return WorkerProxy + } if (key === 'location') { return sandbox.proxyLocation } diff --git a/src/source/patch.ts b/src/source/patch.ts index 2014d6cd..a7f46253 100644 --- a/src/source/patch.ts +++ b/src/source/patch.ts @@ -309,7 +309,7 @@ function completePathDynamic (app: AppInterface, newChild: Node): void { if (newChild.hasAttribute('srcset')) { globalEnv.rawSetAttribute.call(newChild, 'srcset', CompletionPath(newChild.getAttribute('srcset')!, app.url)) } - } else if (/^(link|image)$/i.test(newChild.tagName) && newChild.hasAttribute('href')) { + } else if (/^(a|link|image)$/i.test(newChild.tagName) && newChild.hasAttribute('href')) { globalEnv.rawSetAttribute.call(newChild, 'href', CompletionPath(newChild.getAttribute('href')!, app.url)) } } @@ -541,7 +541,7 @@ export function patchElementAndDocument (): void { appInstanceMap.has(appName) && ( ((key === 'src' || key === 'srcset') && /^(img|script|video|audio|source|embed)$/i.test(this.tagName)) || - (key === 'href' && /^(link|image)$/i.test(this.tagName)) + (key === 'href' && /^(a|link|image)$/i.test(this.tagName)) ) ) { const app = appInstanceMap.get(appName)