Skip to content

Commit

Permalink
cherry-pick(microsoft#33793): fix(aria): escape even more yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Nov 28, 2024
1 parent 1781bf3 commit a8f44f1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
24 changes: 8 additions & 16 deletions packages/playwright-core/src/server/injected/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ function yamlStringNeedsQuotes(str: string): boolean {
if (/^-\s/.test(str))
return true;

// Strings that start with a special indicator character need quotes
if (/^[&*\],].*/.test(str))
return true;

// Strings containing ':' followed by a space or at the end need quotes
if (/:(\s|$)/.test(str))
// Strings containing ':' or '\n' followed by a space or at the end need quotes
if (/[\n:](\s|$)/.test(str))
return true;

// Strings containing '#' preceded by a space need quotes (comment indicator)
Expand All @@ -78,21 +74,17 @@ function yamlStringNeedsQuotes(str: string): boolean {
if (/[\n\r]/.test(str))
return true;

// Strings starting with '?' or '!' (directives) need quotes
if (/^[?!]/.test(str))
return true;

// Strings starting with '>' or '|' (block scalar indicators) need quotes
if (/^[>|]/.test(str))
return true;

// Strings starting with quotes need quotes
if (/^["']/.test(str))
// Strings starting with indicator characters or quotes need quotes
if (/^[&*\],?!>|@"'#%]/.test(str))
return true;

// Strings containing special characters that could cause ambiguity
if (/[{}`]/.test(str))
return true;

// Non-string types recognized by YAML
if (!isNaN(Number(str)) || ['y', 'n', 'yes', 'no', 'true', 'false', 'on', 'off', 'null'].includes(str.toLowerCase()))
return true;

return false;
}
54 changes: 54 additions & 0 deletions tests/page/page-aria-snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,57 @@ it('should handle long strings', async ({ page }) => {
- region: ${s}
`);
});

it('should escape special yaml characters', async ({ page }) => {
await page.setContent(`
<a href="#">@hello</a>@hello
<a href="#">]hello</a>]hello
<a href="#">hello\n</a>
hello\n<a href="#">\n hello</a>\n hello
<a href="#">#hello</a>#hello
`);

await checkAndMatchSnapshot(page.locator('body'), `
- link "@hello"
- text: "@hello"
- link "]hello"
- text: "]hello"
- link "hello"
- text: hello
- link "hello"
- text: hello
- link "#hello"
- text: "#hello"
`);
});

it('should escape special yaml values', async ({ page }) => {
await page.setContent(`
<a href="#">true</a>False
<a href="#">NO</a>yes
<a href="#">y</a>N
<a href="#">on</a>Off
<a href="#">null</a>NULL
<a href="#">123</a>123
<a href="#">-1.2</a>-1.2
<input type=text value="555">
`);

await checkAndMatchSnapshot(page.locator('body'), `
- link "true"
- text: "False"
- link "NO"
- text: "yes"
- link "y"
- text: "N"
- link "on"
- text: "Off"
- link "null"
- text: "NULL"
- link "123"
- text: "123"
- link "-1.2"
- text: "-1.2"
- textbox: "555"
`);
});

0 comments on commit a8f44f1

Please sign in to comment.