Skip to content

Commit cf63907

Browse files
committed
feat: 支持 aHrefResolver: 自定义处理所有子应用 a 标签的 href 拼接方式
1 parent a44c654 commit cf63907

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/sandbox/iframe/element.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
isDocumentFragment,
1414
isFunction,
1515
isBrowser,
16-
isArray,
1716
} from '../../libs/utils'
1817
import {
1918
updateElementInfo,
@@ -242,11 +241,7 @@ function patchIframeAttribute (url: string, microAppWindow: microAppWindowType,
242241
) {
243242
this.setAttribute(key, value)
244243
} else {
245-
let appPlugins = microApp?.options?.plugins?.modules?.[appName]
246-
if (!isArray(appPlugins)) {
247-
appPlugins = []
248-
}
249-
const aHrefResolver = appPlugins[0]?.aHrefResolver
244+
const aHrefResolver = microApp?.options?.aHrefResolver
250245
if (key === 'href' && /^a$/i.test(this.tagName) && typeof aHrefResolver === 'function') {
251246
// 试验性质:a 标签开放自定义补齐功能
252247
value = aHrefResolver(value, appName, url)

src/source/patch.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,15 @@ function completePathDynamic(app: AppInterface, newChild: Node): void {
317317
// If it is the anchor tag,eg. <a href="#xxx"/>, the path will not be completed
318318
(/^(a)$/i.test(newChild.tagName) && newChild.hasAttribute('href') && !/^#/.test(newChild.getAttribute('href') || ''))
319319
) {
320-
globalEnv.rawSetAttribute.call(newChild, 'href', CompletionPath(newChild.getAttribute('href')!, app.url))
320+
const aHrefResolver = microApp?.options?.aHrefResolver
321+
const hrefValue = newChild.getAttribute('href')!
322+
let nextHrefValue
323+
if ((/^(a)$/i.test(newChild.tagName) && typeof aHrefResolver === 'function')) {
324+
nextHrefValue = aHrefResolver(hrefValue, app.name, app.url)
325+
} else {
326+
nextHrefValue = CompletionPath(hrefValue, app.url)
327+
}
328+
globalEnv.rawSetAttribute.call(newChild, 'href', nextHrefValue)
321329
}
322330
}
323331
}
@@ -560,7 +568,12 @@ export function patchElementAndDocument(): void {
560568

561569
) {
562570
const app = appInstanceMap.get(appName)
563-
value = CompletionPath(value, app!.url)
571+
const aHrefResolver = microApp?.options?.aHrefResolver
572+
if (key === 'href' && /^a$/i.test(this.tagName) && typeof aHrefResolver === 'function') {
573+
value = aHrefResolver(value, appName, app!.url)
574+
} else {
575+
value = CompletionPath(value, app!.url)
576+
}
564577
}
565578
globalEnv.rawSetAttribute.call(this, key, value)
566579
if (isImageElement(this) || isVideoElement(this) || isAudioElement(this)) {

typings/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ declare module '@micro-app/types' {
368368
includeCrossOrigin?: (assetUrl: string) => boolean
369369
getRootElementParentNode?: (node: Node, appName: AppName) => void
370370
customProxyDocumentProps?: Map<string | number | symbol, (value: unknown) => void>
371+
aHrefResolver?: (hrefValue: string, appName: string, appUrl: string) => string
371372
inheritBaseBody?:boolean
372373
}
373374

0 commit comments

Comments
 (0)