cherry-pick(#33793): fix(aria): escape even more yaml (#33795)

This commit is contained in:
Dmitry Gozman 2024-11-28 12:09:28 +00:00 committed by GitHub
parent 1046fe0455
commit fbc770c804
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 16 deletions

View file

@ -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)
@ -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;
}

View file

@ -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"
`);
});