Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/mep-cell' into simplifiedselectors
Browse files Browse the repository at this point in the history
  • Loading branch information
vgoodric committed Mar 2, 2024
2 parents 4f1ee26 + dc7a628 commit 1089101
Show file tree
Hide file tree
Showing 7 changed files with 495 additions and 120 deletions.
79 changes: 78 additions & 1 deletion libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,84 @@ function normalizeKeys(obj) {
}, {});
}

const getDivInTargetedCell = (tableCell) => {
tableCell.replaceChildren();
const div = document.createElement('div');
tableCell.appendChild(div);
return div;
};

const querySelector = (el, selector, all = false) => {
try {
return all ? el.querySelectorAll(selector) : el.querySelector(selector);
} catch (e) {
/* eslint-disable-next-line no-console */
console.log('Invalid selector: ', selector);
return null;
}
};

function getTrailingNumber(s) {
const match = s.match(/\d+$/);
return match ? parseInt(match[0], 10) : null;
}

function getSection(rootEl, idx) {
return rootEl === document
? document.querySelector(`body > main > div:nth-child(${idx})`)
: rootEl.querySelector(`:scope > div:nth-child(${idx})`);
}

function getSelectedEl(rootEl, selector) {
if (!selector) return null;

let selectedEl;
if (selector.includes('.') || !['section', 'block', 'row'].some((s) => selector.includes(s))) {
selectedEl = querySelector(rootEl, selector);
if (selectedEl) return selectedEl;
}

const terms = selector.split(/\s+/);

let section = null;
if (terms[0]?.startsWith('section')) {
const sectionIdx = getTrailingNumber(terms[0]) || 1;
section = getSection(rootEl, sectionIdx);
terms.shift();
}

if (terms.length) {
const blockStr = terms.shift();
if (blockStr.includes('.')) {
selectedEl = querySelector(section || rootEl, blockStr);
} else {
const blockIndex = getTrailingNumber(blockStr) || 1;
const blockName = blockStr.replace(`${blockIndex}`, '');
if (blockName === 'block') {
if (!section) section = getSection(rootEl, 1);
selectedEl = querySelector(section, `:scope > div:nth-of-type(${blockIndex})`);
} else {
selectedEl = querySelector(section || rootEl, `.${blockName}`, true)?.[blockIndex - 1];
}
}
} else if (section) {
return section;
}

if (terms.length) {
// find targeted table cell in rowX colY format
const rowColMatch = /row(?<row>\d+)\s+col(?<col>\d+)/gm.exec(terms.join(' '));
if (rowColMatch) {
const { row, col } = rowColMatch.groups;
const tableCell = querySelector(selectedEl, `:nth-child(${row}) > :nth-child(${col})`);
if (!tableCell) return null;
return getDivInTargetedCell(tableCell);
}
}

return selectedEl;
}

function getSelectedElement(selector, action) {
try {
if ((action.includes('appendtosection') || action.includes('prependtosection')) && selector.includes('section')) {
Expand Down Expand Up @@ -396,7 +474,6 @@ function parsePlaceholders(placeholders, config, selectedVariantName = '') {
}
return config;
}
/* c8 ignore stop */

const checkForParamMatch = (paramStr) => {
const [name, val] = paramStr.split('param-')[1].split('=');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
"firefox": "",
"android": "",
"ios": ""
},
{
"action": "replaceContent",
"selector": ".z-pattern.small row3 col2",
"page filter (optional)": "",
"param-newoffer=123": "",
"chrome": "/fragments/milo-replace-r3c2",
"firefox": "",
"android": "",
"ios": ""
}
],
":type": "sheet"
Expand Down
22 changes: 22 additions & 0 deletions test/features/personalization/mocks/manifestBlockNumber.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"total": 5,
"offset": 0,
"limit": 5,
"data": [
{
"action": "replaceContent",
"selector": "marquee row2 col1",
"page filter (optional)": "",
"param-newoffer=123": "",
"all": "/fragments/replace/marquee/r2c1"
},
{
"action": "replaceContent",
"selector": "section5 marquee row2 col2",
"page filter (optional)": "",
"param-newoffer=123": "",
"all": "/fragments/replace/marquee-2/r2c2"
}
],
":type": "sheet"
}
15 changes: 15 additions & 0 deletions test/features/personalization/mocks/manifestInvalidSelector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"total": 5,
"offset": 0,
"limit": 5,
"data": [
{
"action": "replaceContent",
"selector": ".bad...selector",
"page filter (optional)": "",
"param-newoffer=123": "",
"all": "on"
}
],
":type": "sheet"
}
29 changes: 29 additions & 0 deletions test/features/personalization/mocks/manifestSectionBlock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"total": 5,
"offset": 0,
"limit": 5,
"data": [
{
"action": "removeContent",
"selector": "section2 block2",
"page filter (optional)": "",
"param-newoffer=123": "",
"all": "on"
},
{
"action": "removeContent",
"selector": "custom-block2",
"page filter (optional)": "",
"param-newoffer=123": "",
"all": "on"
},
{
"action": "removeContent",
"selector": "section5 custom-block",
"page filter (optional)": "",
"param-newoffer=123": "",
"all": "on"
}
],
":type": "sheet"
}
Loading

0 comments on commit 1089101

Please sign in to comment.