Skip to content

Commit e5e0f67

Browse files
committed
chore: code optimisation
1 parent b471e3a commit e5e0f67

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/toRedactor.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
227227
Object.keys(options.allowedEmptyAttributes).forEach(key => {
228228
if (key === 'img' || key === 'reference') {
229229
ALLOWED_EMPTY_ATTRIBUTES[key] = [
230-
'alt',
230+
...ALLOWED_EMPTY_ATTRIBUTES[key],
231231
...(options.allowedEmptyAttributes?.[key] || [])
232232
];
233233
} else {
@@ -523,24 +523,21 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
523523
delete attrsJson['url']
524524
}
525525
delete attrsJson['redactor-attributes']
526+
526527
Object.entries(attrsJson).forEach((item) => {
527528
if (forbiddenAttrChars.some(char => item[0].includes(char))) {
528529
return;
529530
}
530-
if (ALLOWED_EMPTY_ATTRIBUTES.hasOwnProperty(jsonValue['type'])) {
531-
if (ALLOWED_EMPTY_ATTRIBUTES[jsonValue['type']].includes(item[0])) {
532-
// Check for 'display-type' attribute for reference type, as refernce is used for entries and assets
533-
if (jsonValue['type'] === 'reference' && jsonValue.attrs['display-type'] === 'display') {
531+
532+
if (ALLOWED_EMPTY_ATTRIBUTES.hasOwnProperty(jsonValue['type']) && ALLOWED_EMPTY_ATTRIBUTES[jsonValue['type']].includes(item[0])) {
533+
if ( jsonValue['type'] !== 'reference' || (jsonValue['type'] === 'reference' && jsonValue.attrs['display-type'] === 'display')) {
534534
attrs += `${item[0]}="${replaceHtmlEntities(item[1])}" `;
535535
return;
536-
}
537-
attrs += `${item[0]}="${replaceHtmlEntities(item[1])}" `;
538-
return;
539-
}
536+
}
540537
}
541538
return item[1] ? (item[1] !== '' ? (attrs += `${item[0]}="${replaceHtmlEntities(item[1])}" `) : '') : ''
542539
})
543-
540+
544541
attrs = (attrs.trim() ? ' ' : '') + attrs.trim()
545542
}
546543
if (jsonValue['type'] === 'table') {

0 commit comments

Comments
 (0)