Skip to content

Commit 34b7e42

Browse files
fix: handle empty data-cslp attributes in getEntryIdentifiersInCurrentPage function
1 parent d9857f9 commit 34b7e42

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/visualBuilder/utils/__test__/getEntryIdentifiersInCurrentPage.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ describe("getEntryIdentifiersInCurrentPage", () => {
4747
expect(entriesInCurrentPage.length).toBe(0);
4848
expect(entriesInCurrentPage).toEqual([]);
4949
});
50+
51+
test("should filter out elements with empty data-cslp attribute and not break", () => {
52+
document.body.innerHTML = `
53+
<div>
54+
<h1 data-cslp="">Empty CSLP</h1>
55+
<h1 data-cslp="page.bltf5bb5f8fb088a332.en-us.page_components.0.hero_banner.banner_title">Valid CSLP</h1>
56+
</div>
57+
`;
58+
const { entriesInCurrentPage } = getEntryIdentifiersInCurrentPage();
59+
expect(entriesInCurrentPage.length).toBe(1);
60+
expect(entriesInCurrentPage[0].entryUid).toBe('bltf5bb5f8fb088a332');
61+
});
5062
});

src/visualBuilder/utils/getEntryIdentifiersInCurrentPage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export function getEntryIdentifiersInCurrentPage(): EntryIdentifiers {
1414
);
1515
const uniqueEntriesMap = new Map<string, { entryUid: string, contentTypeUid: string, locale: string}>();
1616
elementsWithCslp.forEach((element) => {
17-
const cslpData = extractDetailsFromCslp(
18-
element.getAttribute("data-cslp") as string
19-
);
17+
const cslpValue = element.getAttribute("data-cslp");
18+
if (!cslpValue) return;
19+
const cslpData = extractDetailsFromCslp(cslpValue);
2020
uniqueEntriesMap.set(cslpData.entry_uid,
2121
{
2222
entryUid: cslpData.entry_uid,

0 commit comments

Comments
 (0)