Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retain empty strings value for alt attr for img and asset while … #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions src/toRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,15 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
delete attrsJson['url']
}
delete attrsJson['redactor-attributes']
Object.entries(attrsJson).forEach((key) => {
if (forbiddenAttrChars.some(char => key[0].includes(char))) {
Object.entries(attrsJson).forEach((item) => {
if (forbiddenAttrChars.some(char => item[0].includes(char))) {
return;
}
return key[1] ? (key[1] !== '' ? (attrs += `${key[0]}="${replaceHtmlEntities(key[1])}" `) : '') : ''
if((jsonValue['type'] === 'img' || (jsonValue['type'] === 'reference') && jsonValue.attrs['display-type'] === 'display' ) && item[0] === 'alt'){
attrs += `${item[0]}="${replaceHtmlEntities(item[1])}" `
return;
}
return item[1] ? (item[1] !== '' ? (attrs += `${item[0]}="${replaceHtmlEntities(item[1])}" `) : '') : ''
})
attrs = (attrs.trim() ? ' ' : '') + attrs.trim()
}
Expand Down
30 changes: 30 additions & 0 deletions test/expectedJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,36 @@ export default {

]

},
"RT-268":{
"html": `<img alt="" src="image_url.jpeg" width="100" style="width: 100; height: auto;" />`,
"json":
{
"id": "a4794fb7214745a2a47fc24104b762f9",
"type": "docs",
"children": [
{
"type": "img",
"attrs": {
"url": "image_url.jpeg",
"redactor-attributes": {
"alt": "",
"src": "image_url.jpeg",
"width": "100"
},
"width": "100"
},
"uid": "18ff239605014dcaaa23c705caf99403",
"children": [
{
"text": ""
}
]
}
]
}


}

}
7 changes: 7 additions & 0 deletions test/toRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import isEqual from "lodash.isequal"

import expectedValue from "./expectedJson"
import { imageAssetData } from "./testingData"
import exp from "constants"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove unwanted imports ?


describe("Testing json to html conversion", () => {
it("heading conversion", () => {
Expand Down Expand Up @@ -292,5 +293,11 @@ describe("Testing json to html conversion", () => {
const html = toRedactor(json);
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;" />`)
})

test(' should retain empty string value for alt attribute', () => {
const json = expectedValue['RT-268'].json;
const html = toRedactor(json);
expect(html).toBe(expectedValue['RT-268'].html);
})
})

Loading