Change PowerShell double quotes escape
It changes the way privacy.sexy escape double quotes inside batch command when running PowerShell scripts as an argument to PowerShell.exe. It uses more robust and stable way offering support for wider use-cases.
This commit is contained in:
@@ -3,10 +3,25 @@ import { IPipe } from '../IPipe';
|
||||
export class EscapeDoubleQuotes implements IPipe {
|
||||
public readonly name: string = 'escapeDoubleQuotes';
|
||||
public apply(raw: string): string {
|
||||
return raw?.replaceAll('"', '\\"');
|
||||
return raw?.replaceAll('"', '"^""');
|
||||
/*
|
||||
In batch, it also works with 4 double quotes but looks bloated
|
||||
An easy test: PowerShell -Command "Write-Host '\"Hello World\"'"
|
||||
"^"" is the most robust and stable choice.
|
||||
Other options:
|
||||
""
|
||||
Breaks, because it is fundamentally unsupported
|
||||
""""
|
||||
Does not work with consecutive double quotes.
|
||||
E.g. PowerShell -Command "$name='aq'; Write-Host """"Disabled `""""$name`"""""""";"
|
||||
Works when using: PowerShell -Command "$name='aq'; Write-Host "^""Disabled `"^""$name`"^"" "^"";"
|
||||
\"
|
||||
May break as they are interpreted by cmd.exe as metacharacters breaking the command
|
||||
E.g. PowerShell -Command "Write-Host 'Hello \"w&orld\"'" does not work due to unescaped "&"
|
||||
Works when using: PowerShell -Command "Write-Host 'Hello "^""w&orld"^""'"
|
||||
\""
|
||||
Normalizes interior whitespace
|
||||
E.g. PowerShell -Command "\""a& c\"".length", outputs 4 and discards one of two whitespaces
|
||||
Works when using "^"": PowerShell -Command ""^""a& c"^"".length"
|
||||
A good explanation: https://stackoverflow.com/a/31413730
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ describe('EscapeDoubleQuotes', () => {
|
||||
{
|
||||
name: 'using "',
|
||||
input: 'hello "world"',
|
||||
expectedOutput: 'hello \\"world\\"',
|
||||
expectedOutput: 'hello "^""world"^""',
|
||||
},
|
||||
{
|
||||
name: 'not using any double quotes',
|
||||
@@ -20,7 +20,7 @@ describe('EscapeDoubleQuotes', () => {
|
||||
{
|
||||
name: 'consecutive double quotes',
|
||||
input: '""hello world""',
|
||||
expectedOutput: '\\"\\"hello world\\"\\"',
|
||||
expectedOutput: '"^"""^""hello world"^"""^""',
|
||||
},
|
||||
{
|
||||
name: 'returns undefined when if input is undefined',
|
||||
|
||||
Reference in New Issue
Block a user