Skip to content

Commit c3bb466

Browse files
David VogelDavidVogel
authored andcommitted
added fix for paths starting with single period
1 parent dfd469a commit c3bb466

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/core/utils/url.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,28 @@ export function sanitizeUrl(url) {
6262
return `${urlObject.pathname}${urlObject.search}${urlObject.hash}`
6363
}
6464

65-
if (urlTrimmed.startsWith("./")) {
66-
return `.${urlObject.pathname}${urlObject.search}${urlObject.hash}`
67-
}
68-
69-
// Handle multiple levels of relative paths (../, ../../, ../../../, etc.)
70-
if (urlTrimmed.startsWith("../")) {
71-
// Count the number of ../ segments
65+
// Handle relative paths (./path, ../path, ./../../path, etc.)
66+
if (urlTrimmed.startsWith("./") || urlTrimmed.startsWith("../")) {
7267
const segments = urlTrimmed.split("/")
73-
let relativeLevels = 0
74-
75-
for (const segment of segments) {
76-
if (segment === "..") {
77-
relativeLevels++
68+
let relativePath = ""
69+
let pathStartIndex = 0
70+
71+
// Process initial relative segments
72+
for (let i = 0; i < segments.length; i++) {
73+
if (segments[i] === ".") {
74+
relativePath += "./"
75+
pathStartIndex = i + 1
76+
} else if (segments[i] === "..") {
77+
relativePath += "../"
78+
pathStartIndex = i + 1
7879
} else {
7980
break
8081
}
8182
}
8283

83-
// Reconstruct the relative path with correct number of ../
84-
const relativePath = "../".repeat(relativeLevels)
85-
return `${relativePath}${urlObject.pathname.substring(1)}${urlObject.search}${urlObject.hash}`
84+
// Get the remaining path from the URL object
85+
const remainingPath = urlObject.pathname.substring(1)
86+
return `${relativePath}${remainingPath}${urlObject.search}${urlObject.hash}`
8687
}
8788

8889
return `${urlObject.pathname.substring(1)}${urlObject.search}${urlObject.hash}`

test/unit/core/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,7 @@ describe("utils", () => {
14891489
expect(sanitizeUrl("../../openapi.json")).toEqual("../../openapi.json")
14901490
expect(sanitizeUrl("../../../openapi.json")).toEqual("../../../openapi.json")
14911491
expect(sanitizeUrl("../../../../openapi.json")).toEqual("../../../../openapi.json")
1492+
expect(sanitizeUrl("./../../../openapi.json")).toEqual("./../../../openapi.json")
14921493
})
14931494

14941495
it("should gracefully handle empty strings", () => {

0 commit comments

Comments
 (0)