Skip to content

Commit

Permalink
Convert to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWr committed May 23, 2024
1 parent a47011e commit 628e64f
Showing 1 changed file with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ import {
* @added v1.7.3.
*/
/* eslint-enable max-len */
export function trustedClickElement(source, selectors, extraMatch = '', delay = NaN) {
export function trustedClickElement(
source: Source,
selectors: string,
extraMatch = '',
delay = NaN,
) {
if (!selectors) {
return;
}
Expand All @@ -129,11 +134,11 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
// Regex to split match pairs by commas, avoiding the ones included in regexes
const EXTRA_MATCH_DELIMITER = /(,\s*){1}(?=!?cookie:|!?localStorage:|containsText:)/;

const sleep = (delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs));
const sleep = (delayMs: number) => new Promise((resolve) => setTimeout(resolve, delayMs));

let parsedDelay;
if (delay) {
parsedDelay = parseInt(delay, 10);
parsedDelay = parseInt(String(delay), 10);
const isValidDelay = !Number.isNaN(parsedDelay) || parsedDelay < OBSERVER_TIMEOUT_MS;
if (!isValidDelay) {
// eslint-disable-next-line max-len
Expand All @@ -145,8 +150,8 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =

let canClick = !parsedDelay;

const cookieMatches = [];
const localStorageMatches = [];
const cookieMatches: string[] = [];
const localStorageMatches: string[] = [];
let textMatches = '';
let isInvertedMatchCookie = false;
let isInvertedMatchLocalStorage = false;
Expand Down Expand Up @@ -191,11 +196,11 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
// Avoid getting /.?/ result from toRegExp on undefined
// as cookie may be set without value,
// on which cookie parsing will return cookieKey:undefined pair
const valueMatch = parsedCookieMatches[key] ? toRegExp(parsedCookieMatches[key]) : null;
const valueMatch = parsedCookieMatches[key] ? toRegExp(parsedCookieMatches[key] || '') : null;
const keyMatch = toRegExp(key);

return cookieKeys.some((key) => {
const keysMatched = keyMatch.test(key);
return cookieKeys.some((cookieKey) => {
const keysMatched = keyMatch.test(cookieKey);
if (!keysMatched) {
return false;
}
Expand All @@ -205,7 +210,7 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
return true;
}

return valueMatch.test(parsedCookies[key]);
return valueMatch.test(parsedCookies[cookieKey] || '');
});
});

Expand Down Expand Up @@ -233,11 +238,14 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
/**
* Checks if an element contains the specified text.
*
* @param {Element} element - The element to check.
* @param {string} matchRegexp - The text to match.
* @returns {boolean} Returns true if the element contains the specified text, otherwise false.
* @param element - The element to check.
* @param matchRegexp - The text to match.
* @returns True if the element contains the specified text, otherwise false.
*/
const doesElementContainText = (element, matchRegexp) => {
const doesElementContainText = (
element: Element,
matchRegexp: RegExp,
): boolean => {
const { textContent } = element;
if (!textContent) {
return false;
Expand All @@ -257,13 +265,13 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
.split(SELECTORS_DELIMITER)
.map((selector) => selector.trim());

const createElementObj = (element) => {
const createElementObj = (element: any): Object => {
return {
element: element || null,
clicked: false,
};
};
const elementsSequence = Array(selectorsSequence.length).fill(createElementObj());
const elementsSequence = Array(selectorsSequence.length).fill(createElementObj(null));

/**
* Go through elementsSequence from left to right, clicking on found elements
Expand Down Expand Up @@ -301,7 +309,7 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
}
};

const handleElement = (element, i) => {
const handleElement = (element: Element, i: number) => {
const elementObj = createElementObj(element);
elementsSequence[i] = elementObj;

Expand All @@ -318,8 +326,8 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
* when delay is getting off after the last mutation took place.
*
*/
const findElements = (mutations, observer) => {
const fulfilledSelectors = [];
const findElements = (mutations: MutationRecord[], observer: MutationObserver) => {
const fulfilledSelectors: string[] = [];
selectorsSequence.forEach((selector, i) => {
if (!selector) {
return;
Expand All @@ -335,11 +343,11 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =

// selectorsSequence should be modified after the loop to not break loop indexation
selectorsSequence = selectorsSequence.map((selector) => {
return fulfilledSelectors.includes(selector) ? null : selector;
return fulfilledSelectors.includes(selector) ? '' : selector;
});

// Disconnect observer after finding all elements
const allSelectorsFulfilled = selectorsSequence.every((selector) => selector === null);
const allSelectorsFulfilled = selectorsSequence.every((selector) => selector === '');
if (allSelectorsFulfilled) {
observer.disconnect();
}
Expand Down

0 comments on commit 628e64f

Please sign in to comment.