mirror of
https://github.com/microsoft/playwright-cli.git
synced 2026-09-14 19:59:39 +08:00
fix(codegen): escape slashes correctly in JavaScript (#75)
This commit is contained in:
@@ -167,7 +167,7 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator {
|
||||
const browser = await ${browserName}.launch(${formatObjectOrVoid(launchOptions)});
|
||||
const context = await browser.newContext(${formatContextOptions(contextOptions, deviceName)});
|
||||
})();`);
|
||||
this._output.write(formatter.format() + '\n');
|
||||
this._output.write(formatter.format() + '\n');
|
||||
}
|
||||
|
||||
writeFooter(): void {
|
||||
@@ -266,12 +266,8 @@ class JavaScriptFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
function quote(text: string, char: string = '\'') {
|
||||
if (char === '\'')
|
||||
return char + text.replace(/[']/g, '\\\'') + char;
|
||||
if (char === '"')
|
||||
return char + text.replace(/["]/g, '\\"') + char;
|
||||
if (char === '`')
|
||||
return char + text.replace(/[`]/g, '\\`') + char;
|
||||
throw new Error('Invalid escape char');
|
||||
const quoteChar = '\'';
|
||||
function quote(text: string): string {
|
||||
return quoteChar + text.replace(/[']/g, '\\\'')
|
||||
.replace(/\\/g, '\\\\') + quoteChar;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,17 @@ it('should click', async ({ page, recorder }) => {
|
||||
expect(message.text()).toBe('click');
|
||||
});
|
||||
|
||||
it('should escape slashes correctly for JavaScript', async ({ recorder, page }) => {
|
||||
await recorder.setContentAndWait(`<button>username (first last) / Repositories</button>`);
|
||||
const selector = await recorder.focusElement('button');
|
||||
expect(selector).toBe('text=/.*username \\(first last\\) / Reposi.*/');
|
||||
await page.click('text=username')
|
||||
await recorder.waitForOutput('username')
|
||||
expect(recorder.output()).toContain(`
|
||||
// Click text=/.*username \\(first last\\) / Reposi.*/
|
||||
await page.click('text=/.*username \\\\(first last\\\\) / Reposi.*/');`)
|
||||
});
|
||||
|
||||
it('should not target selector preview by text regexp', async ({ page, recorder }) => {
|
||||
await recorder.setContentAndWait(`<span>dummy</span>`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user