Skip to content

Commit

Permalink
fix(ariaqueryhandler): allow single quotes in aria attribute selector (
Browse files Browse the repository at this point in the history
…#7750)

This updates the regular expression used to parse aria attribute
selectors so that single quotes may be used as an alternative to double
quotes, e.g. `aria/Single button[role='button']`.

Issues: #7721

Co-authored-by: Andy Earnshaw <andy.earnshaw@gmail.com>
  • Loading branch information
andyearnshaw and Andy Earnshaw committed Nov 9, 2021
1 parent ad7f1de commit b0319ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/common/AriaQueryHandler.ts
Expand Up @@ -41,7 +41,7 @@ const normalizeValue = (value: string): string =>
value.replace(/ +/g, ' ').trim();
const knownAttributes = new Set(['name', 'role']);
const attributeRegexp =
/\[\s*(?<attribute>\w+)\s*=\s*"(?<value>\\.|[^"\\]*)"\s*\]/g;
/\[\s*(?<attribute>\w+)\s*=\s*(?<quote>"|')(?<value>\\.|.*?(?=\k<quote>))\k<quote>\s*\]/g;

/*
* The selectors consist of an accessible name to query for and optionally
Expand All @@ -58,7 +58,7 @@ function parseAriaSelector(selector: string): ariaQueryOption {
const queryOptions: ariaQueryOption = {};
const defaultName = selector.replace(
attributeRegexp,
(_, attribute: string, value: string) => {
(_, attribute: string, quote: string, value: string) => {
attribute = attribute.trim();
if (!knownAttributes.has(attribute))
throw new Error(`Unknown aria attribute "${attribute}" in selector`);
Expand Down
8 changes: 8 additions & 0 deletions test/ariaqueryhandler.spec.ts
Expand Up @@ -46,6 +46,10 @@ describeChromeOnly('AriaQueryHandler', () => {
'aria/Submit button and some spaces[role="button"]'
);
await expectFound(button);
button = await page.$(
"aria/Submit button and some spaces[role='button']"
);
await expectFound(button);
button = await page.$(
'aria/ Submit button and some spaces[role="button"]'
);
Expand All @@ -70,6 +74,10 @@ describeChromeOnly('AriaQueryHandler', () => {
'aria/[name=" Submit button and some spaces"][role="button"]'
);
await expectFound(button);
button = await page.$(
"aria/[name=' Submit button and some spaces'][role='button']"
);
await expectFound(button);
button = await page.$(
'aria/ignored[name="Submit button and some spaces"][role="button"]'
);
Expand Down

0 comments on commit b0319ec

Please sign in to comment.