Skip to content

Commit fbc770c

Browse files
authored
cherry-pick(#33793): fix(aria): escape even more yaml (#33795)
1 parent 1046fe0 commit fbc770c

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

packages/playwright-core/src/server/injected/yaml.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ function yamlStringNeedsQuotes(str: string): boolean {
6262
if (/^-\s/.test(str))
6363
return true;
6464

65-
// Strings that start with a special indicator character need quotes
66-
if (/^[&*\],].*/.test(str))
67-
return true;
68-
69-
// Strings containing ':' followed by a space or at the end need quotes
70-
if (/:(\s|$)/.test(str))
65+
// Strings containing ':' or '\n' followed by a space or at the end need quotes
66+
if (/[\n:](\s|$)/.test(str))
7167
return true;
7268

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

81-
// Strings starting with '?' or '!' (directives) need quotes
82-
if (/^[?!]/.test(str))
83-
return true;
84-
85-
// Strings starting with '>' or '|' (block scalar indicators) need quotes
86-
if (/^[>|]/.test(str))
87-
return true;
88-
89-
// Strings starting with quotes need quotes
90-
if (/^["']/.test(str))
77+
// Strings starting with indicator characters or quotes need quotes
78+
if (/^[&*\],?!>|@"'#%]/.test(str))
9179
return true;
9280

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

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

tests/page/page-aria-snapshot.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,57 @@ it('should handle long strings', async ({ page }) => {
509509
- region: ${s}
510510
`);
511511
});
512+
513+
it('should escape special yaml characters', async ({ page }) => {
514+
await page.setContent(`
515+
<a href="#">@hello</a>@hello
516+
<a href="#">]hello</a>]hello
517+
<a href="#">hello\n</a>
518+
hello\n<a href="#">\n hello</a>\n hello
519+
<a href="#">#hello</a>#hello
520+
`);
521+
522+
await checkAndMatchSnapshot(page.locator('body'), `
523+
- link "@hello"
524+
- text: "@hello"
525+
- link "]hello"
526+
- text: "]hello"
527+
- link "hello"
528+
- text: hello
529+
- link "hello"
530+
- text: hello
531+
- link "#hello"
532+
- text: "#hello"
533+
`);
534+
});
535+
536+
it('should escape special yaml values', async ({ page }) => {
537+
await page.setContent(`
538+
<a href="#">true</a>False
539+
<a href="#">NO</a>yes
540+
<a href="#">y</a>N
541+
<a href="#">on</a>Off
542+
<a href="#">null</a>NULL
543+
<a href="#">123</a>123
544+
<a href="#">-1.2</a>-1.2
545+
<input type=text value="555">
546+
`);
547+
548+
await checkAndMatchSnapshot(page.locator('body'), `
549+
- link "true"
550+
- text: "False"
551+
- link "NO"
552+
- text: "yes"
553+
- link "y"
554+
- text: "N"
555+
- link "on"
556+
- text: "Off"
557+
- link "null"
558+
- text: "NULL"
559+
- link "123"
560+
- text: "123"
561+
- link "-1.2"
562+
- text: "-1.2"
563+
- textbox: "555"
564+
`);
565+
});

0 commit comments

Comments
 (0)