Skip to content

Commit

Permalink
getSelectedElement - V2
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Fedotov authored and Denys Fedotov committed Aug 3, 2024
1 parent fe4eb9b commit 0e9373a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ function normalizeKeys(obj) {

const querySelector = (el, selector, all = false) => {
try {
console.log(el);
return all ? el.querySelectorAll(selector) : el.querySelector(selector);
} catch (e) {
/* eslint-disable-next-line no-console */
Expand Down Expand Up @@ -335,9 +336,9 @@ function registerInBlockActions(cmd, manifestId, targetManifestId) {
config.mep.inBlock[blockName].commands.push(command);
}

function getSelectedElement({ selector, rootEl }) {
function getSelectedElement({ selector }) {
if (!selector) return null;
let originalSelector = selector;
const originalSelector = selector;
if (checkSelectorType(selector) === 'fragment') {
// handle fragment selector
try {
Expand All @@ -356,7 +357,7 @@ function getSelectedElement({ selector, rootEl }) {
const simplifiedSelector = match[0].replace(/\s+/g, '');
const n = simplifiedSelector.match(/\d+/g) || '1';
const cleanClassSelector = match[2]; // this one has no digits and no spaces
const cssOptimizedSelector = `>.${cleanClassSelector}:nth-child(${n} of .${cleanClassSelector})`;
const cssOptimizedSelector = `> .${cleanClassSelector}:nth-child(${n} of .${cleanClassSelector})`;
// eslint-disable-next-line no-param-reassign
selector = selector.replace(simplifiedSelector, cssOptimizedSelector);
}
Expand All @@ -366,7 +367,7 @@ function getSelectedElement({ selector, rootEl }) {
const simplifiedSelectors = selector.match(new RegExp(`${sel}\\.?\\d?`, 'g'));
simplifiedSelectors?.forEach((simplifiedSelector) => {
const n = simplifiedSelector.match(/\d+/g) || '1';
const cssOptimizedSelector = `>div:nth-of-type(${n})`;
const cssOptimizedSelector = `> div:nth-of-type(${n})`;
// eslint-disable-next-line no-param-reassign
selector = selector.replace(simplifiedSelector, cssOptimizedSelector);
});
Expand All @@ -393,14 +394,14 @@ function getSelectedElement({ selector, rootEl }) {
});
});
// eslint-disable-next-line no-param-reassign
selector = selector.charAt(0) === '>' ? selector.slice(1) : selector;
selector = `body ${selector}`;
// TODO: for testing purposes only. Remove when done
console.log('=====================================');
console.log('selector: ', originalSelector, ' ==> ', selector);
console.log('element: ', querySelector(rootEl ?? document, selector));
console.log('element: ', querySelector(document, selector));

// slice(1) removes trailing >
return querySelector(rootEl ?? document, selector);
return querySelector(document, selector);
}
}
const addHash = (url, newHash) => {
Expand Down

0 comments on commit 0e9373a

Please sign in to comment.