mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 11:36:23 +00:00
fix: include anchor when normalizing urls for spa/popovers
This commit is contained in:
parent
a26eb59392
commit
95b1141b9d
1 changed files with 10 additions and 6 deletions
|
@ -2,14 +2,18 @@ import { computePosition, flip, inline, shift } from "@floating-ui/dom"
|
|||
|
||||
// from micromorph/src/utils.ts
|
||||
// https://github.com/natemoo-re/micromorph/blob/main/src/utils.ts#L5
|
||||
export function normalizeRelativeURLs(el: Element | Document, base: string | URL) {
|
||||
const update = (el: Element, attr: string, base: string | URL) => {
|
||||
el.setAttribute(attr, new URL(el.getAttribute(attr)!, base).pathname)
|
||||
export function normalizeRelativeURLs(el: Element | Document, destination: string | URL) {
|
||||
const rebase = (el: Element, attr: string, newBase: string | URL) => {
|
||||
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('[src^="./"], [src^="../"]').forEach((item) => update(item, "src", base))
|
||||
el.querySelectorAll('[href^="./"], [href^="../"]').forEach((item) =>
|
||||
rebase(item, "href", destination),
|
||||
)
|
||||
el.querySelectorAll('[src^="./"], [src^="../"]').forEach((item) =>
|
||||
rebase(item, "src", destination),
|
||||
)
|
||||
}
|
||||
|
||||
const p = new DOMParser()
|
||||
|
|
Loading…
Reference in a new issue