Skip to content

Commit 5158edb

Browse files
ENG-2305: Add experience config ID and Property ID class to embedded consent for per-brand CSS (#7228)
1 parent 30bcb81 commit 5158edb

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

changelog/7228.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: Fixed
2+
description: Added experience config ID and property ID classes to embedded consent body
3+
pr: 7228
4+
labels: []
5+

clients/privacy-center/public/embedded-consent.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,54 @@ <h3 id="fides-embed-no-results" class="no-results no-results--is-hidden">
8181
</h3>
8282
</main>
8383
<script>
84+
/**
85+
* Sanitize a string to be a valid CSS class name.
86+
* - Replaces invalid characters with hyphens
87+
* - Collapses multiple hyphens
88+
* - Trims leading/trailing hyphens
89+
* - Prefixes with underscore if starts with digit or hyphen+digit
90+
*/
91+
const toValidClassName = (str) => {
92+
let result = str
93+
.replace(/[^A-Za-z0-9_-]/g, "-")
94+
.replace(/-+/g, "-")
95+
.replace(/^-|-$/g, "");
96+
if (/^[0-9]/.test(result) || /^-[0-9]/.test(result)) {
97+
result = "_" + result;
98+
}
99+
return result;
100+
};
101+
84102
const onInitialized = () => {
103+
// Add experience config ID and property ID as classes to the body for per-brand CSS styling
104+
const experienceConfigId =
105+
window.Fides?.experience?.experience_config?.id;
106+
if (experienceConfigId) {
107+
try {
108+
window.document.body.classList.add(
109+
toValidClassName(experienceConfigId),
110+
);
111+
} catch (e) {
112+
window.fidesDebugger?.(
113+
"Failed to add experience config ID as class:",
114+
experienceConfigId,
115+
e,
116+
);
117+
}
118+
}
119+
const propertyId = window.Fides?.experience?.property_id;
120+
if (propertyId) {
121+
try {
122+
window.document.body.classList.add(toValidClassName(propertyId));
123+
} catch (e) {
124+
window.fidesDebugger?.(
125+
"Failed to add property ID as class:",
126+
propertyId,
127+
e,
128+
);
129+
}
130+
}
131+
85132
let fidesUIShown = false;
86133
window.addEventListener("FidesUIShown", () => {
87134
fidesUIShown = true;

0 commit comments

Comments
 (0)