Skip to content

Commit eafdb21

Browse files
committed
feat: escape ids that start with digit
1 parent bf51873 commit eafdb21

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

scripts/target.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
const SESSION_COOKIE_NAME = 'sessionId';
22
const SESSION_COOKIE_EXPIRATION_DAYS = 7;
33

4+
/**
5+
* Escape a selector.
6+
* @param selector
7+
* @returns {string}
8+
*/
9+
function escapeSelector(selector) {
10+
return selector.replaceAll(/#(\d)/g, '#\\3$1 ');
11+
}
12+
413
/**
514
* Generate a UUID.
615
* @returns {string}
@@ -178,7 +187,7 @@ function getLoadedSections(main) {
178187
function displayOffers(section, offers) {
179188
offers.forEach((offer) => {
180189
const { type, cssSelector, content } = offer;
181-
const targetElement = section.querySelector(cssSelector);
190+
const targetElement = section.querySelector(escapeSelector(cssSelector));
182191
if (targetElement) {
183192
switch (type) {
184193
case 'insertAfter':
@@ -214,7 +223,7 @@ function displayOffers(section, offers) {
214223
* @param selector The element selector.
215224
*/
216225
function getSectionByElementSelector(selector) {
217-
let section = document.querySelector(selector);
226+
let section = document.querySelector(escapeSelector(selector));
218227
while (section && !section.classList.contains('section')) {
219228
section = section.parentNode;
220229
}
@@ -249,11 +258,12 @@ export default function loadOffers(client, useProxy) {
249258
window?.measurePerformance('targeting:loading-offers');
250259

251260
offers.forEach((offer) => {
261+
const { cssSelector } = offer;
252262
console.debug('processing offer', offer); // eslint-disable-line no-console
253-
const section = getSectionByElementSelector(offer.selector);
263+
const section = getSectionByElementSelector(cssSelector);
254264
if (section) {
255265
// eslint-disable-next-line no-console
256-
console.debug(`hiding section for selector ${offer.selector}`, section);
266+
console.debug(`hiding section for selector ${cssSelector}`, section);
257267
section.style.visibility = 'hidden';
258268
window?.createPerformanceMark(
259269
`targeting:rendering-section:${Array.from(section.classList).join('_')}`,

0 commit comments

Comments
 (0)