Skip to content

Commit

Permalink
update function to validate selector
Browse files Browse the repository at this point in the history
  • Loading branch information
tedw87 committed Mar 25, 2024
1 parent e61425a commit 64c283b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export const serializeText = (text) => {
return isArray(text) ? serializeNodes(text) : text;
};

function removeSpecialCharacters(str) {
return str.replace(/[^a-zA-Z0-9]/g, '');
}

export const toSlug = (url) => Slugger.slug(url);

export const waitForElm = (selector) => {
Expand Down Expand Up @@ -54,8 +50,21 @@ export const scrollToTarget = (target, offsetHeight = 0) => {
return;
};

const isValidSelector = (selector) => {
try {
document.createElement('div').querySelector(selector);
return true;
} catch (e) {
return false;
}
};

export const openAccordionIfContainsAnchors = (anchor) => {
waitForElm(removeSpecialCharacters(anchor)).then((elm) => {
if (!isValidSelector(anchor)) {
console.error('Invalid selector:', anchor);
return;
}
waitForElm(anchor).then((elm) => {
if (elm.closest('.accordion')) {
const comp = elm.closest('.accordion')?.querySelector('.title');
if (!comp?.className?.includes('active')) {
Expand Down

0 comments on commit 64c283b

Please sign in to comment.