refactor(common): move actual constants (#7512)
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.
This commit is contained in:
parent
2aec35553b
commit
f04ffdbe2c
@ -37,6 +37,12 @@ async function queryAXTree(
|
|||||||
return filteredNodes;
|
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
|
* The selectors consist of an accessible name to query for and optionally
|
||||||
* further aria attributes on the form `[<attribute>=<value>]`.
|
* further aria attributes on the form `[<attribute>=<value>]`.
|
||||||
@ -49,23 +55,19 @@ async function queryAXTree(
|
|||||||
*/
|
*/
|
||||||
type ariaQueryOption = { name?: string; role?: string };
|
type ariaQueryOption = { name?: string; role?: string };
|
||||||
function parseAriaSelector(selector: string): ariaQueryOption {
|
function parseAriaSelector(selector: string): ariaQueryOption {
|
||||||
const normalize = (value: string): string => value.replace(/ +/g, ' ').trim();
|
|
||||||
const knownAttributes = new Set(['name', 'role']);
|
|
||||||
const queryOptions: ariaQueryOption = {};
|
const queryOptions: ariaQueryOption = {};
|
||||||
const attributeRegexp =
|
|
||||||
/\[\s*(?<attribute>\w+)\s*=\s*"(?<value>\\.|[^"\\]*)"\s*\]/g;
|
|
||||||
const defaultName = selector.replace(
|
const defaultName = selector.replace(
|
||||||
attributeRegexp,
|
attributeRegexp,
|
||||||
(_, attribute: string, value: string) => {
|
(_, attribute: string, value: string) => {
|
||||||
attribute = attribute.trim();
|
attribute = attribute.trim();
|
||||||
if (!knownAttributes.has(attribute))
|
if (!knownAttributes.has(attribute))
|
||||||
throw new Error(`Unknown aria attribute "${attribute}" in selector`);
|
throw new Error(`Unknown aria attribute "${attribute}" in selector`);
|
||||||
queryOptions[attribute] = normalize(value);
|
queryOptions[attribute] = normalizeValue(value);
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (defaultName && !queryOptions.name)
|
if (defaultName && !queryOptions.name)
|
||||||
queryOptions.name = normalize(defaultName);
|
queryOptions.name = normalizeValue(defaultName);
|
||||||
return queryOptions;
|
return queryOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user