-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in-house the file-uri implementation
fixes #1941
- Loading branch information
Showing
7 changed files
with
73 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { test, expect, describe } from 'vitest'; | ||
|
||
import fileUriRaw from './fileUri.js'; | ||
|
||
|
||
describe('file uri windows only', () => { | ||
test('converts path to file url', () => { | ||
expect(fileUriRaw('C:\\Users\\sindresorhus\\dev\\te^st.jpg', true)).toEqual('file:///C:/Users/sindresorhus/dev/te%5Est.jpg'); | ||
}); | ||
}); | ||
|
||
describe('file uri non-windows', () => { | ||
// https://github.com/mifi/lossless-cut/issues/1941 | ||
test('file with backslash', () => { | ||
expect(fileUriRaw('/has/back\\slash', false)).toEqual('file:///has/back%5Cslash'); | ||
}); | ||
}); | ||
|
||
// taken from https://github.com/sindresorhus/file-url | ||
describe.each([{ isWindows: false }, { isWindows: true }])('file uri both platforms isWindows=$isWindows', ({ isWindows }) => { | ||
const fileUri = (path) => fileUriRaw(path, isWindows); | ||
|
||
test('converts path to file url', () => { | ||
expect(fileUri('/test.jpg')).toMatch(/file:\/{3}test\.jpg/); | ||
|
||
expect(fileUri('/Users/sindresorhus/dev/te^st.jpg')).toEqual('file:///Users/sindresorhus/dev/te%5Est.jpg'); | ||
}); | ||
|
||
test('escapes more special characters in path', () => { | ||
expect(fileUri('/a?!@#$%^&\'";<>')).toEqual('file:///a%3F!@%23$%25%5E&\'%22;%3C%3E'); | ||
}); | ||
|
||
test('escapes whitespace characters in path', () => { | ||
expect(fileUri('/file with\r\nnewline')).toEqual('file:///file%20with%0D%0Anewline'); | ||
}); | ||
|
||
test('relative path', () => { | ||
expect(fileUri('relative/test.jpg')).toEqual('file:///relative/test.jpg'); | ||
}); | ||
|
||
test('empty', () => { | ||
expect(fileUri('')).toEqual('file:///'); | ||
}); | ||
|
||
test('slash', () => { | ||
expect(fileUri('/')).toEqual('file:///'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export default function fileUri(filePath: string, isWindows: boolean) { | ||
let pathName = filePath; | ||
|
||
if (isWindows) { | ||
pathName = pathName.replaceAll('\\', '/'); | ||
} | ||
|
||
// Windows drive letter must be prefixed with a slash. | ||
// also relative paths will be converted to absolute | ||
if (pathName[0] !== '/') { | ||
pathName = `/${pathName}`; | ||
} | ||
|
||
// Escape required characters for path components. | ||
// See: https://tools.ietf.org/html/rfc3986#section-3.3 | ||
return encodeURI(`file://${pathName}`).replaceAll(/[#?]/g, encodeURIComponent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters