Skip to content

Commit 70e7854

Browse files
Fix escaping repeated " for CMD
Rework escaping for CMD so that escaping a `"` works as follows 1. Escape the `"` itself (as `\\^"`) and inject a marker as a unique anchor for the required additional escaping of backslashes. The marker is guaranteed to be unique because we previously removed it indiscriminately from the argument. 2. Use the marker for further replacements. If it's preceded by `\\` it means the `\\` proceeds a `"` in the original string. If it's not it should still be removed.
1 parent e12017f commit 70e7854

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/internal/win/cmd.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ import RegExp from "@ericcornelissen/lregexp";
1313
export function getEscapeFunction() {
1414
const controls = new RegExp("[\0\u0008\r\u001B\u009B]", "g");
1515
const newlines = new RegExp("\n", "g");
16-
const quotes = new RegExp('(^|[^\\\\])(\\\\*)"', "g");
17-
const specials = new RegExp('(["%&<>^|])', "g");
16+
const specials = new RegExp("([%&<>^|])", "g");
17+
const quotes = new RegExp('"', "g");
18+
const backslashes = new RegExp("(^|[^\\\\])(\\\\*)\0", "g");
1819
return (arg) =>
1920
arg
2021
.replace(controls, "")
2122
.replace(newlines, " ")
22-
.replace(quotes, '$1$2$2\\"')
23-
.replace(specials, "^$1");
23+
.replace(specials, "^$1")
24+
.replace(quotes, '\0\\^"')
25+
.replace(backslashes, "$1$2$2");
2426
}
2527

2628
/**

0 commit comments

Comments
 (0)