Skip to content

Commit ce759e3

Browse files
author
David Haeffner
committed
Refactored string escape preprocessing
1 parent f843369 commit ce759e3

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

packages/selianize/__tests__/__snapshots__/command.spec.js.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ exports[`command code emitter should emit \`mouse up at\` event 1`] = `"await dr
2222
2323
exports[`command code emitter should emit \`mouse up\` event 1`] = `"await driver.wait(until.elementLocated(By.id(\`button\`)), configuration.timeout);await driver.findElement(By.id(\`button\`)).then(element => {return driver.actions({bridge: true}).move({origin: element}).release().perform();});"`;
2424
25-
exports[`command code emitter should emit \`run script\` command 1`] = `"await driver.executeScript(\`alert('test');\\\\nalert('Im annoying');\`);"`;
25+
exports[`command code emitter should emit \`run script\` command 1`] = `
26+
"await driver.executeScript(\`alert('test');
27+
alert('Im annoying');\`);"
28+
`;
2629
2730
exports[`command code emitter should emit \`set window size\` command 1`] = `
2831
"try {

packages/selianize/src/command.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,16 @@ export function emit(command, options = config, snapshot) {
125125
if (options.skipStdLibEmitting && !emitters[command.command].isAdditional)
126126
return res({ skipped: true })
127127
try {
128+
const ignoreEscaping = command.command === 'storeJson'
128129
let result = await emitters[command.command](
129130
preprocessParameter(
130131
command.target,
131132
emitters[command.command].target,
132-
command
133+
{ ignoreEscaping }
133134
),
134-
preprocessParameter(
135-
command.value,
136-
emitters[command.command].value,
137-
command
138-
)
135+
preprocessParameter(command.value, emitters[command.command].value, {
136+
ignoreEscaping,
137+
})
139138
)
140139
if (command.opensWindow) result = emitNewWindowHandling(result, command)
141140
res(result)
@@ -160,21 +159,21 @@ export function canEmit(commandName) {
160159
return !!emitters[commandName]
161160
}
162161

163-
function preprocessParameter(param, preprocessor, command) {
162+
function preprocessParameter(param, preprocessor, { ignoreEscaping }) {
164163
const escapedParam = escapeString(param, {
165164
preprocessor,
166-
commandName: command ? command.command : undefined,
165+
ignoreEscaping,
167166
})
168167
if (preprocessor) {
169168
return preprocessor(escapedParam)
170169
}
171170
return defaultPreprocessor(escapedParam)
172171
}
173172

174-
function escapeString(string, { preprocessor, commandName }) {
175-
if (commandName && commandName === 'storeJson') return string
173+
function escapeString(string, { preprocessor, ignoreEscaping }) {
174+
if (ignoreEscaping) return string
176175
else if (preprocessor && preprocessor.name === 'scriptPreprocessor')
177-
return stringEscape(string.replace(/"/g, "'"), '"')
176+
return string.replace(/"/g, "'")
178177
else return stringEscape(string)
179178
}
180179

packages/side-utils/src/code-export/emit.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ export function emitCommand(
2727
{ variableLookup, emitNewWindowHandling }
2828
) {
2929
if (emitter) {
30+
const ignoreEscaping = command.command === 'storeJson'
3031
let result = emitter(
3132
preprocessParameter(
3233
command.target,
3334
emitter.targetPreprocessor,
3435
variableLookup,
35-
command.command
36+
{ ignoreEscaping }
3637
),
3738
preprocessParameter(
3839
command.value,
3940
emitter.valuePreprocessor,
4041
variableLookup,
41-
command.command
42+
{ ignoreEscaping }
4243
)
4344
)
4445
if (command.opensWindow) result = emitNewWindowHandling(command, result)

packages/side-utils/src/code-export/preprocessor.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717

1818
import stringEscape from '../string-escape'
1919

20-
function escapeString(string, { preprocessor, commandName }) {
21-
if (commandName && commandName === 'storeJson') return string
20+
function escapeString(string, { preprocessor, ignoreEscaping }) {
21+
if (ignoreEscaping) return string
2222
else if (preprocessor && preprocessor.name === 'scriptPreprocessor')
23-
return stringEscape(string.replace(/"/g, "'"), '"')
23+
return string.replace(/"/g, "'")
2424
else return stringEscape(string)
2525
}
2626

2727
export function preprocessParameter(
2828
param,
2929
preprocessor,
3030
variableLookup,
31-
commandName
31+
{ ignoreEscaping }
3232
) {
33-
const escapedParam = escapeString(param, { preprocessor, commandName })
33+
const escapedParam = escapeString(param, { preprocessor, ignoreEscaping })
3434
return preprocessor
3535
? preprocessor(escapedParam, variableLookup)
3636
: defaultPreprocessor(escapedParam, variableLookup)

0 commit comments

Comments
 (0)