forked from tools/josh
1
0
Fork 0

Properly escape characters in paths

Fixes #97.
This commit is contained in:
Florine W. Dekker 2019-11-23 13:14:32 +01:00
parent aa9a466956
commit 09588f4bb5
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
4 changed files with 20 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fwdekker.com",
"version": "0.24.1",
"version": "0.24.2",
"description": "The source code of [my personal website](https://fwdekker.com/).",
"author": "Felix W. Dekker",
"repository": {

View File

@ -345,10 +345,22 @@ export class Path {
if (!escape)
return this.path;
return this.path
.replace(/'/g, "\\\'")
.replace(/"/g, "\\\"")
.replace(/\s/g, "\\ ");
const escapes = [
["\\;", "\\&#59;"],
["\\'", "\\\'"],
['\\"', "\\\""],
[" ", "\\ "],
["\\\\", "\\\\"],
["\\~", "\\~"],
["\\$", "\\$"],
["\\>", "\\>"],
["\\?", "\\?"],
["\\*", "\\*"],
["\\{", "\\{"],
["\\}", "\\}"],
];
return escapes.reduce((path, instr) => path.replace(new RegExp(instr[0], "g"), instr[1]), this.path);
}
}

View File

@ -338,6 +338,7 @@ export class Expander {
switch (nextChar) {
case "\\":
case " ":
case ";":
case "~":
case "$":
case ">":
@ -349,9 +350,6 @@ export class Expander {
case "}":
expandedToken += nextChar;
break;
case "n":
expandedToken += "\n";
break;
default:
expandedToken += char + nextChar;
break;

View File

@ -521,8 +521,9 @@ describe("expander", () => {
expect(expander.expand("\\\\")).to.have.deep.members(["\\"]);
});
it("escapes whitespace", () => {
it("escapes separators", () => {
expect(expander.expand("\\ ")).to.have.deep.members([" "]);
expect(expander.expand("\\;")).to.have.deep.members([";"]);
});
it("escapes the home directory character", () => {
@ -549,10 +550,6 @@ describe("expander", () => {
expect(expander.expand("\\}")).to.have.deep.members(["}"]);
});
it("escapes differently-represented characters", () => {
expect(expander.expand("\\n")).to.have.deep.members(["\n"]);
});
it("does not escape other characters", () => {
expect(expander.expand("\\a\\b\\_")).to.have.deep.members(["\\a\\b\\_"]);
});