Skip to content

Commit 4264c10

Browse files
committed
fix: retain empty strings value for alt attr for img and asset while conversion to html
1 parent db47a9e commit 4264c10

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/toRedactor.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,15 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
506506
delete attrsJson['url']
507507
}
508508
delete attrsJson['redactor-attributes']
509-
Object.entries(attrsJson).forEach((key) => {
510-
if (forbiddenAttrChars.some(char => key[0].includes(char))) {
509+
Object.entries(attrsJson).forEach((item) => {
510+
if (forbiddenAttrChars.some(char => item[0].includes(char))) {
511511
return;
512512
}
513-
return key[1] ? (key[1] !== '' ? (attrs += `${key[0]}="${replaceHtmlEntities(key[1])}" `) : '') : ''
513+
if((jsonValue['type'] === 'img' || (jsonValue['type'] === 'reference') && jsonValue.attrs['display-type'] === 'display' ) && item[0] === 'alt'){
514+
attrs += `${item[0]}="${replaceHtmlEntities(item[1])}" `
515+
return;
516+
}
517+
return item[1] ? (item[1] !== '' ? (attrs += `${item[0]}="${replaceHtmlEntities(item[1])}" `) : '') : ''
514518
})
515519
attrs = (attrs.trim() ? ' ' : '') + attrs.trim()
516520
}

test/expectedJson.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,36 @@ export default {
22042204

22052205
]
22062206

2207+
},
2208+
"RT-268":{
2209+
"html": `<img alt="" src="image_url.jpeg" width="100" style="width: 100; height: auto;" />`,
2210+
"json":
2211+
{
2212+
"id": "a4794fb7214745a2a47fc24104b762f9",
2213+
"type": "docs",
2214+
"children": [
2215+
{
2216+
"type": "img",
2217+
"attrs": {
2218+
"url": "image_url.jpeg",
2219+
"redactor-attributes": {
2220+
"alt": "",
2221+
"src": "image_url.jpeg",
2222+
"width": "100"
2223+
},
2224+
"width": "100"
2225+
},
2226+
"uid": "18ff239605014dcaaa23c705caf99403",
2227+
"children": [
2228+
{
2229+
"text": ""
2230+
}
2231+
]
2232+
}
2233+
]
2234+
}
2235+
2236+
22072237
}
22082238

22092239
}

test/toRedactor.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import isEqual from "lodash.isequal"
33

44
import expectedValue from "./expectedJson"
55
import { imageAssetData } from "./testingData"
6+
import exp from "constants"
67

78
describe("Testing json to html conversion", () => {
89
it("heading conversion", () => {
@@ -292,5 +293,11 @@ describe("Testing json to html conversion", () => {
292293
const html = toRedactor(json);
293294
expect(html).toBe(`<img alt="Infographic showing 3 results from Forrester study of Contentstack CMS: $3M increase in profit, $507.3K productivity savings and $2.0M savings due to reduced time to publish." src="https://images.contentstack.io/v3/assets/blt7359e2a55efae483/bltea2a11144a2c68b5/63c08b7f438f80612c397994/CS_Infographics_ForresterReport_Data_3_1200x628_(1).png" position="center" width="641" style="width: 641; height: auto;" />`)
294295
})
296+
297+
test(' should retain empty string value for alt attribute', () => {
298+
const json = expectedValue['RT-268'].json;
299+
const html = toRedactor(json);
300+
expect(html).toBe(expectedValue['RT-268'].html);
301+
})
295302
})
296303

0 commit comments

Comments
 (0)