fix(aria snapshots): normalize whitespace (#34285)
This commit is contained in:
parent
4f3a5e2133
commit
1f2eb499d2
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
import * as roleUtils from './roleUtils';
|
import * as roleUtils from './roleUtils';
|
||||||
import { getElementComputedStyle } from './domUtils';
|
import { getElementComputedStyle } from './domUtils';
|
||||||
import { escapeRegExp, longestCommonSubstring } from '@isomorphic/stringUtils';
|
import { escapeRegExp, longestCommonSubstring, normalizeWhiteSpace } from '@isomorphic/stringUtils';
|
||||||
import { yamlEscapeKeyIfNeeded, yamlEscapeValueIfNeeded } from './yaml';
|
import { yamlEscapeKeyIfNeeded, yamlEscapeValueIfNeeded } from './yaml';
|
||||||
import type { AriaProps, AriaRole, AriaTemplateNode, AriaTemplateRoleNode, AriaTemplateTextNode } from '@isomorphic/ariaSnapshot';
|
import type { AriaProps, AriaRole, AriaTemplateNode, AriaTemplateRoleNode, AriaTemplateTextNode } from '@isomorphic/ariaSnapshot';
|
||||||
|
|
||||||
|
|
@ -137,7 +137,7 @@ function toAriaNode(element: Element): AriaNode | null {
|
||||||
if (!role || role === 'presentation' || role === 'none')
|
if (!role || role === 'presentation' || role === 'none')
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
const name = roleUtils.getElementAccessibleName(element, false) || '';
|
const name = normalizeWhiteSpace(roleUtils.getElementAccessibleName(element, false) || '');
|
||||||
const result: AriaNode = { role, name, children: [], element };
|
const result: AriaNode = { role, name, children: [], element };
|
||||||
|
|
||||||
if (roleUtils.kAriaCheckedRoles.includes(role))
|
if (roleUtils.kAriaCheckedRoles.includes(role))
|
||||||
|
|
@ -170,7 +170,7 @@ function normalizeStringChildren(rootA11yNode: AriaNode) {
|
||||||
const flushChildren = (buffer: string[], normalizedChildren: (AriaNode | string)[]) => {
|
const flushChildren = (buffer: string[], normalizedChildren: (AriaNode | string)[]) => {
|
||||||
if (!buffer.length)
|
if (!buffer.length)
|
||||||
return;
|
return;
|
||||||
const text = normalizeWhitespaceWithin(buffer.join('')).trim();
|
const text = normalizeWhiteSpace(buffer.join(''));
|
||||||
if (text)
|
if (text)
|
||||||
normalizedChildren.push(text);
|
normalizedChildren.push(text);
|
||||||
buffer.length = 0;
|
buffer.length = 0;
|
||||||
|
|
@ -196,8 +196,6 @@ function normalizeStringChildren(rootA11yNode: AriaNode) {
|
||||||
visit(rootA11yNode);
|
visit(rootA11yNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizeWhitespaceWithin = (text: string) => text.replace(/[\u200b\s\t\r\n]+/g, ' ');
|
|
||||||
|
|
||||||
function matchesText(text: string, template: RegExp | string | undefined): boolean {
|
function matchesText(text: string, template: RegExp | string | undefined): boolean {
|
||||||
if (!template)
|
if (!template)
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -212,7 +212,7 @@ class KeyParser {
|
||||||
const ch = this._peek();
|
const ch = this._peek();
|
||||||
if (ch === '"') {
|
if (ch === '"') {
|
||||||
this._next();
|
this._next();
|
||||||
return this._readString();
|
return normalizeWhitespace(this._readString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch === '/') {
|
if (ch === '/') {
|
||||||
|
|
|
||||||
|
|
@ -509,6 +509,32 @@ it('should escape yaml text in text nodes', async ({ page }) => {
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should normalize whitespace', async ({ page }) => {
|
||||||
|
await page.setContent(`
|
||||||
|
<details>
|
||||||
|
<summary> one \n two <a href="#"> link \n 1 </a> </summary>
|
||||||
|
</details>
|
||||||
|
<input value=' hello world '>
|
||||||
|
`);
|
||||||
|
|
||||||
|
await checkAndMatchSnapshot(page.locator('body'), `
|
||||||
|
- group:
|
||||||
|
- text: one two
|
||||||
|
- link "link 1"
|
||||||
|
- textbox: hello world
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Weird whitespace in the template should be normalized.
|
||||||
|
await expect(page.locator('body')).toMatchAriaSnapshot(`
|
||||||
|
- group:
|
||||||
|
- text: |
|
||||||
|
one
|
||||||
|
two
|
||||||
|
- link " link 1 "
|
||||||
|
- textbox: hello world
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle long strings', async ({ page }) => {
|
it('should handle long strings', async ({ page }) => {
|
||||||
const s = 'a'.repeat(10000);
|
const s = 'a'.repeat(10000);
|
||||||
await page.setContent(`
|
await page.setContent(`
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue