fix: include anchor when normalizing urls for spa/popovers

This commit is contained in:
Jacky Zhao 2023-11-15 20:35:45 -08:00
parent a26eb59392
commit 95b1141b9d

View file

@ -2,14 +2,18 @@ import { computePosition, flip, inline, shift } from "@floating-ui/dom"
// from micromorph/src/utils.ts // from micromorph/src/utils.ts
// https://github.com/natemoo-re/micromorph/blob/main/src/utils.ts#L5 // https://github.com/natemoo-re/micromorph/blob/main/src/utils.ts#L5
export function normalizeRelativeURLs(el: Element | Document, base: string | URL) { export function normalizeRelativeURLs(el: Element | Document, destination: string | URL) {
const update = (el: Element, attr: string, base: string | URL) => { const rebase = (el: Element, attr: string, newBase: string | URL) => {
el.setAttribute(attr, new URL(el.getAttribute(attr)!, base).pathname) const rebased = new URL(el.getAttribute(attr)!, newBase)
el.setAttribute(attr, rebased.pathname + rebased.hash)
} }
el.querySelectorAll('[href^="./"], [href^="../"]').forEach((item) => update(item, "href", base)) el.querySelectorAll('[href^="./"], [href^="../"]').forEach((item) =>
rebase(item, "href", destination),
el.querySelectorAll('[src^="./"], [src^="../"]').forEach((item) => update(item, "src", base)) )
el.querySelectorAll('[src^="./"], [src^="../"]').forEach((item) =>
rebase(item, "src", destination),
)
} }
const p = new DOMParser() const p = new DOMParser()