From f04ffdbe2ca418a5f3c761fa72f9598ee9263a31 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Fri, 10 Sep 2021 23:58:30 +0430 Subject: [PATCH] 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. --- src/common/AriaQueryHandler.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/common/AriaQueryHandler.ts b/src/common/AriaQueryHandler.ts index 8b2e4c92..9e9c69a0 100644 --- a/src/common/AriaQueryHandler.ts +++ b/src/common/AriaQueryHandler.ts @@ -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*(?\w+)\s*=\s*"(?\\.|[^"\\]*)"\s*\]/g; + /* * The selectors consist of an accessible name to query for and optionally * further aria attributes on the form `[=]`. @@ -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*(?\w+)\s*=\s*"(?\\.|[^"\\]*)"\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; }