Skip to content

Commit

Permalink
refactor(common): move actual constants (#7512)
Browse files Browse the repository at this point in the history
The values of these constant variables are always the exact same when the `parseAriaSelector()` function is called, so these can be moved out of the function.
  • Loading branch information
VoltrexKeyva committed Sep 10, 2021
1 parent 2aec355 commit f04ffdb
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/common/AriaQueryHandler.ts
Expand Up @@ -37,6 +37,12 @@ async function queryAXTree(
return filteredNodes;
}

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;

/*
* The selectors consist of an accessible name to query for and optionally
* further aria attributes on the form `[<attribute>=<value>]`.
Expand All @@ -49,23 +55,19 @@ async function queryAXTree(
*/
type ariaQueryOption = { name?: string; role?: string };
function parseAriaSelector(selector: string): ariaQueryOption {
const normalize = (value: string): string => value.replace(/ +/g, ' ').trim();
const knownAttributes = new Set(['name', 'role']);
const queryOptions: ariaQueryOption = {};
const attributeRegexp =
/\[\s*(?<attribute>\w+)\s*=\s*"(?<value>\\.|[^"\\]*)"\s*\]/g;
const defaultName = selector.replace(
attributeRegexp,
(_, attribute: string, value: string) => {
attribute = attribute.trim();
if (!knownAttributes.has(attribute))
throw new Error(`Unknown aria attribute "${attribute}" in selector`);
queryOptions[attribute] = normalize(value);
queryOptions[attribute] = normalizeValue(value);
return '';
}
);
if (defaultName && !queryOptions.name)
queryOptions.name = normalize(defaultName);
queryOptions.name = normalizeValue(defaultName);
return queryOptions;
}

Expand Down

0 comments on commit f04ffdb

Please sign in to comment.