Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/lib/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,48 @@ describe('utils lib', () => {
});
});

it('strips HTML from labels', () => {
const values = { phone: '1234567890', email: 'test@example.com' };
const fields: JSFFields = [
{ name: 'phone', type: 'text', label: 'Employee's phone number' },
{ name: 'email', type: 'text', label: '<strong>Work email</strong>' },
];
expect(prettifyFormValues(values, fields)).toEqual({
phone: {
prettyValue: '1234567890',
label: "Employee's phone number",
inputType: 'text',
},
email: {
prettyValue: 'test@example.com',
label: 'Work email',
inputType: 'text',
},
});
});

it('strips HTML from prettyValue in radio/select fields', () => {
const values = { status: 'active' };
const fields: JSFFields = [
{
name: 'status',
type: 'select',
label: 'Status',
options: [
{ value: 'active', label: '<strong>Active</strong>' },
{ value: 'inactive', label: 'Employee&#39;s status' },
],
},
];
expect(prettifyFormValues(values, fields)).toEqual({
status: {
prettyValue: 'Active',
label: 'Status',
inputType: 'select',
},
});
});

it('handles multiple fields of different types', () => {
const values = {
name: 'John',
Expand Down
40 changes: 33 additions & 7 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ export const sanitizeHtmlWithImageErrorHandling = (html: string) => {
});
};

/**
* Strips HTML tags and decodes HTML entities from a string
* @param html - The HTML string to strip
* @returns Plain text without HTML tags or entities
*/
export const stripHtml = (
html: string | undefined | null,
): string | undefined | null => {
if (html === null || html === undefined) {
return html;
}

const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');

return doc.body.textContent || '';
};
Comment thread
cursor[bot] marked this conversation as resolved.

/**
* Ensures base64 data has the correct data URI prefix for PDF content
* @param base64Data - The base64 data string
Expand Down Expand Up @@ -184,8 +202,8 @@ export function prettifyFormValues(
return [
key,
{
prettyValue: option?.label,
label: field?.label,
prettyValue: stripHtml(option?.label),
label: stripHtml(field?.label as string),
inputType: field?.type,
},
];
Expand All @@ -196,7 +214,11 @@ export function prettifyFormValues(
if (field?.type === 'checkbox' && field?.const) {
return [
key,
{ prettyValue: true, label: field.label, inputType: field?.type },
{
prettyValue: true,
label: stripHtml(field.label as string),
inputType: field?.type,
},
];
}

Expand All @@ -205,7 +227,7 @@ export function prettifyFormValues(
key,
{
prettyValue: value.join(),
label: field.label,
label: stripHtml(field.label as string),
inputType: field?.type,
},
];
Expand All @@ -223,7 +245,7 @@ export function prettifyFormValues(
if (!prettiedFieldset.label && prettiedFieldset.value) {
const prettyValue: Record<string, unknown> = {
...prettiedFieldset.value,
label: field.label,
label: stripHtml(field.label as string),
inputType: field?.type,
};
return [key, prettyValue];
Expand All @@ -243,7 +265,7 @@ export function prettifyFormValues(
(typeof value === 'string' || typeof value === 'number')
? convertFromCents(value)
: value,
label: field.label,
label: stripHtml(field.label as string),
inputType: field?.type,
currency: field?.currency,
},
Expand All @@ -253,7 +275,11 @@ export function prettifyFormValues(
if (field) {
return [
key,
{ prettyValue: value, label: field.label, inputType: field?.type },
{
prettyValue: value,
label: stripHtml(field.label as string),
inputType: field?.type,
},
];
}
})
Expand Down
Loading