In my react app I am using react-quill for the text editor and on the backend sanitize-html with JOI for the validation. In the text editor leaving empty lines results to this: <p><br></p>
. sanitize-html does not allow ‘br’ tags by default, but no matter the option it will not allow the tag.
My code is
const extension = (joi) => ({
type: 'string',
base: joi.string(),
messages: {
'string.escapeHTML': '{{#label}} must not include HTML tags',
},
rules: {
escapeHTML: {
validate(value, helpers) {
const clean = sanitizeHtml(value, {
allowedClasses: {
'*': [
'ql-align-right',
'ql-align-center',
'ql-align-justify',
'ql-code-block',
'ql-code-block-container',
'ql-syntax',
'ql-direction-rtl',
'ql-font-serif',
'ql-font-monospace',
'ql-formula',
'ql-indent-1',
'ql-indent-2',
'ql-indent-3',
'ql-indent-4',
'ql-indent-5',
'ql-indent-6',
'ql-indent-7',
'ql-indent-8',
'ql-size-small',
'ql-size-large',
'ql-size-huge',
],
},
});
if (clean != value)
return helpers.error('string.escapeHTML', { value });
return clean;
},
},
},
});
I have tried
const clean = sanitizeHtml(value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'br' ])
});
and even
allowedTags: false,
allowedAttributes: false
but still it will not allow the br tag.