OS.Path shim: Add Windows version of normalize() from Fx102
Some checks are pending
CI / Build, Upload, Test (push) Waiting to run
Some checks are pending
CI / Build, Upload, Test (push) Waiting to run
Should fix #4588
This commit is contained in:
parent
a80a9a1598
commit
f53ce16b8b
1 changed files with 58 additions and 0 deletions
|
@ -316,6 +316,59 @@ export let OS = {
|
|||
|
||||
// From Firefox 102
|
||||
normalize: function (path) {
|
||||
if (Services.appinfo.OS == "WINNT") {
|
||||
let stack = [];
|
||||
|
||||
if (!path.startsWith("\\\\")) {
|
||||
// Normalize "/" to "\\"
|
||||
path = path.replace(/\//g, "\\");
|
||||
}
|
||||
|
||||
// Remove the drive (we will put it back at the end)
|
||||
let root = winGetDrive(path);
|
||||
if (root) {
|
||||
path = path.slice(root.length);
|
||||
}
|
||||
|
||||
// Remember whether we need to restore a leading "\\" or drive name.
|
||||
let absolute = winIsAbsolute(path);
|
||||
|
||||
// And now, fill |stack| from the components,
|
||||
// popping whenever there is a ".."
|
||||
path.split("\\").forEach(function loop(v) {
|
||||
switch (v) {
|
||||
case "":
|
||||
case ".": // Ignore
|
||||
break;
|
||||
case "..":
|
||||
if (!stack.length) {
|
||||
if (absolute) {
|
||||
throw new Error("Path is ill-formed: attempting to go past root");
|
||||
} else {
|
||||
stack.push("..");
|
||||
}
|
||||
} else if (stack[stack.length - 1] == "..") {
|
||||
stack.push("..");
|
||||
} else {
|
||||
stack.pop();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
stack.push(v);
|
||||
}
|
||||
});
|
||||
|
||||
// Put everything back together
|
||||
let result = stack.join("\\");
|
||||
if (absolute || root) {
|
||||
result = "\\" + result;
|
||||
}
|
||||
if (root) {
|
||||
result = root + result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
let stack = [];
|
||||
let absolute;
|
||||
if (path.length >= 0 && path[0] == "/") {
|
||||
|
@ -401,6 +454,11 @@ var winGetDrive = function (path) {
|
|||
return path.slice(0, index + 1);
|
||||
};
|
||||
|
||||
var winIsAbsolute = function (path) {
|
||||
let index = path.indexOf(":");
|
||||
return path.length > index + 1 && path[index + 1] == "\\";
|
||||
};
|
||||
|
||||
function wrapWrite(func) {
|
||||
return async function () {
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue