Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复绝对路径对hash路由的影响 #140

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/wujie-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function fixElementCtrSrcOrHref(
const rawElementSetAttribute = iframeWindow.Element.prototype.setAttribute;
elementCtr.prototype.setAttribute = function (name: string, value: string): void {
let targetValue = value;
if (name === attr) targetValue = getAbsolutePath(value, this.baseURI || "");
if (name === attr) targetValue = getAbsolutePath(value, this.baseURI || "", true);
rawElementSetAttribute.call(this, name, targetValue);
};
// patch href get and set
Expand All @@ -177,7 +177,7 @@ export function fixElementCtrSrcOrHref(
return get.call(this);
},
set: function (href) {
set.call(this, getAbsolutePath(href, this.baseURI));
set.call(this, getAbsolutePath(href, this.baseURI, true));
},
});
// TODO: innerHTML的处理
Expand All @@ -188,10 +188,12 @@ export function getCurUrl(proxyLocation: Object): string {
return location.protocol + "//" + location.host + location.pathname;
}

export function getAbsolutePath(url: string, base: string): string {
export function getAbsolutePath(url: string, base: string, hash): string {
try {
// 为空值或者hash值无需处理
if (url && !url.startsWith("#")) {
// 为空值无需处理
if (url) {
// 需要处理hash的场景
if (hash && url.startsWith("#")) return url;
return new URL(url, base).href;
} else return url;
} catch {
Expand Down