Skip to content

Commit

Permalink
lazyload the old way using state and useEffect
Browse files Browse the repository at this point in the history
- My previous attempt gave the following sonarqube error:
   Expected non-Promise value in a boolean conditional.
  • Loading branch information
ichim-david committed May 31, 2024
1 parent 612f477 commit ae4ae91
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 81 deletions.
17 changes: 14 additions & 3 deletions src/Utils/FormattedValue.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
// FormattedValue.jsx

import React, { useState, useEffect } from 'react';
import isObject from 'lodash/isObject';
import loadable from '@loadable/component';
import cx from 'classnames';
import { CountUp } from '@eeacms/countup';
import { UniversalLink } from '@plone/volto/components';
import { isUrl } from '@plone/volto/helpers';
import { sanitizeHtml } from '../helpers';

const D3 = loadable.lib(() => import('d3'));

Expand Down Expand Up @@ -34,6 +35,16 @@ const FormattedValue = ({
animatedCounter,
link = null,
}) => {
const [sanitizeHtmlModule, setSanitizeHtmlModule] = useState(null);

useEffect(() => {
const loadModule = async () => {
const module = await import('sanitize-html');
setSanitizeHtmlModule(module);
};
loadModule();
}, []);

const originalValue = value;
const animateValue = typeof value === 'number' && animatedCounter;

Expand Down Expand Up @@ -68,7 +79,7 @@ const FormattedValue = ({
const html =
isLink && isObject(link) && link.title
? link.title
: sanitizeHtml(value, {
: sanitizeHtmlModule(value, {
allowedAttributes: {
span: ['id'],
},
Expand Down
15 changes: 0 additions & 15 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,3 @@ export const getFilteredURL = (url, connected_data_parameters = []) => {
}
return decodedURL;
};

/**
* Sanitizes the given HTML value using the `sanitize-html` library.
*
* @param {string} value - The HTML value to be sanitized.
* @param {object} allowedAttributes - An object specifying the allowed attributes for the sanitized HTML.
* @returns {Promise<string>} - The sanitized HTML value.
*/
export const sanitizeHtml = async (value, allowedAttributes) => {
const sanitized = await import('sanitize-html');
const result = sanitized.default(value, {
...allowedAttributes,
});
return result;
};
63 changes: 0 additions & 63 deletions src/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,67 +697,4 @@ describe('getFilteredURL', () => {
const expectedUrl = 'value1';
expect(getFilteredURL(url, connected_data_parameters)).toBe(expectedUrl);
});

describe('sanitizeHtml', () => {
it('sanitizes HTML with allowed attributes', async () => {
const value = '<div><a href="https://example.com">Link</a></div>';
const allowedAttributes = {
allowedTags: ['a'],
allowedAttributes: { a: ['href'] },
};

const result = await sanitizeHtml(value, allowedAttributes);

expect(result).toBe('<a href="https://example.com">Link</a>');
});

it('removes disallowed attributes', async () => {
const value =
'<div><a href="https://example.com" target="_blank">Link</a></div>';
const allowedAttributes = {
allowedTags: ['a'],
allowedAttributes: { a: ['href'] },
};

const result = await sanitizeHtml(value, allowedAttributes);

expect(result).toBe('<a href="https://example.com">Link</a>');
});

it('removes disallowed tags', async () => {
const value = '<div><a href="https://example.com">Link</a></div>';
const allowedAttributes = {
allowedTags: ['b'],
allowedAttributes: { b: [] },
};

const result = await sanitizeHtml(value, allowedAttributes);

expect(result).toBe('Link');
});

it('returns empty string for empty input', async () => {
const value = '';
const allowedAttributes = {
allowedTags: ['a'],
allowedAttributes: { a: ['href'] },
};

const result = await sanitizeHtml(value, allowedAttributes);

expect(result).toBe('');
});

it('returns the same string if no sanitization is needed', async () => {
const value = '<a href="https://example.com">Link</a>';
const allowedAttributes = {
allowedTags: ['a'],
allowedAttributes: { a: ['href'] },
};

const result = await sanitizeHtml(value, allowedAttributes);

expect(result).toBe('<a href="https://example.com">Link</a>');
});
});
});

0 comments on commit ae4ae91

Please sign in to comment.