Skip to content

Commit d0f8038

Browse files
Merge pull request #84 from contentstack/RT-483
No.of br tags should match /n on shift+enter
2 parents 8c071ce + 08fa679 commit d0f8038

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/json-rte-serializer",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "This Package converts Html Document to Json and vice-versa.",
55
"main": "lib/index.js",
66
"module": "lib/index.mjs",

src/toRedactor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
234234
if (jsonValue.hasOwnProperty('text')) {
235235
let text = jsonValue['text'].replace(/</g, '&lt;').replace(/>/g, '&gt;')
236236
if (jsonValue['break']) {
237-
text += `<br/>`
237+
text = text.replace(/\n/g, '<br/>')
238238
}
239239
if(jsonValue['classname'] || jsonValue['id']){
240240
if(jsonValue['classname'] && jsonValue['id']){

test/expectedJson.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,8 +1870,7 @@ export default {
18701870
expectedJson: {"type":"doc","uid":"uid","attrs":{},"children":[{"type":"table","attrs":{"style":{},"redactor-attributes":{"border":"1"},"rows":5,"cols":4,"colWidths":[250,250,250,250],"disabledCols":[1,2,3]},"uid":"uid","children":[{"type":"thead","attrs":{},"uid":"uid","children":[{"type":"tr","attrs":{},"uid":"uid","children":[{"type":"th","attrs":{},"uid":"uid","children":[{"text":"Header 1"}]},{"type":"th","attrs":{"colSpan":3,"style":{},"redactor-attributes":{"colspan":"3"}},"uid":"uid","children":[{"text":"Header 2"}]},{"type":"th","attrs":{"void":true},"children":[{"text":""}]},{"type":"th","attrs":{"void":true},"children":[{"text":""}]},{"type":"th","attrs":{},"uid":"uid","children":[{"text":"Header 3"}]}]}]},{"type":"tbody","attrs":{},"uid":"uid","children":[{"type":"trgrp","children":[{"type":"tr","attrs":{},"uid":"uid","children":[{"type":"td","attrs":{"rowSpan":2,"style":{},"redactor-attributes":{"rowspan":"2"}},"uid":"uid","children":[{"text":"Row 1, Col 1"}]},{"type":"td","attrs":{"colSpan":2,"style":{},"redactor-attributes":{"colspan":"2"}},"uid":"uid","children":[{"text":"Row 1, Col 2"}]},{"type":"td","attrs":{"void":true},"children":[{"text":""}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 1, Col 3"}]},{"type":"td","attrs":{"rowSpan":2,"style":{},"redactor-attributes":{"rowspan":"2"}},"uid":"uid","children":[{"text":"Row 1, Col 4"}]}]},{"type":"tr","attrs":{},"uid":"uid","children":[{"type":"td","attrs":{"void":true},"children":[{"text":""}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 2, Col 2"}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 2, Col 3"}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 2, Col 4"}]},{"type":"td","attrs":{"void":true},"children":[{"text":""}]}]}]},{"type":"trgrp","children":[{"type":"tr","attrs":{},"uid":"uid","children":[{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 3, Col 1"}]},{"type":"td","attrs":{"rowSpan":2,"colSpan":3,"style":{},"redactor-attributes":{"colspan":"3","rowspan":"2"}},"uid":"uid","children":[{"text":"Row 3, Col 2"}]},{"type":"td","attrs":{"void":true},"children":[{"text":""}]},{"type":"td","attrs":{"void":true},"children":[{"text":""}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 3, Col 5"}]}]},{"type":"tr","attrs":{},"uid":"uid","children":[{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 4, Col 1"}]},{"type":"td","attrs":{"void":true},"children":[{"text":""}]},{"type":"td","attrs":{"void":true},"children":[{"text":""}]},{"type":"td","attrs":{"void":true},"children":[{"text":""}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Row 4, Col 5"}]}]}]}]}]}]}
18711871
},
18721872
'fix_EB-745' : {
1873-
html: `<ul><li dir="ltr">Vehicle
1874-
<br/>This is test</li></ul>`,
1873+
html: `<ul><li dir="ltr">Vehicle<br/>This is test</li></ul>`,
18751874
expectedJson: {
18761875
"uid": "uid",
18771876
"type": "doc",
@@ -2347,6 +2346,76 @@ export default {
23472346
"_version": 2
23482347
}
23492348
]
2349+
},
2350+
"RT-483":{
2351+
"html" : [
2352+
`<p dir="ltr">HI<br/><br/>Hi shift enter x2</p>`,
2353+
`<p dir="ltr"><br/>HI<br/><br/>Hi <br/> shift enter x2</p>`
2354+
],
2355+
"json" : [
2356+
{
2357+
"uid": "a59f9108e99747d4b3358d9e22b7c685",
2358+
"type": "doc",
2359+
"attrs": {
2360+
"dirty": true
2361+
},
2362+
"children": [
2363+
{
2364+
"type": "p",
2365+
"uid": "bedef68ac71c4f41bdee935e78fb7c31",
2366+
"attrs": {
2367+
"style": {},
2368+
"redactor-attributes": {},
2369+
"dir": "ltr"
2370+
},
2371+
"children": [
2372+
{
2373+
"text": "HI"
2374+
},
2375+
{
2376+
"text": "\n\n",
2377+
"break": true
2378+
},
2379+
{
2380+
"text": "Hi shift enter x2"
2381+
}
2382+
]
2383+
}
2384+
],
2385+
"_version": 2
2386+
},
2387+
{
2388+
"uid": "a59f9108e99747d4b3358d9e22b7c685",
2389+
"type": "doc",
2390+
"attrs": {
2391+
"dirty": true
2392+
},
2393+
"children": [
2394+
{
2395+
"type": "p",
2396+
"uid": "bedef68ac71c4f41bdee935e78fb7c31",
2397+
"attrs": {
2398+
"style": {},
2399+
"redactor-attributes": {},
2400+
"dir": "ltr"
2401+
},
2402+
"children": [
2403+
{
2404+
"text": "\nHI"
2405+
},
2406+
{
2407+
"text": "\n\n",
2408+
"break": true
2409+
},
2410+
{
2411+
"text": "Hi \n shift enter x2"
2412+
}
2413+
]
2414+
}
2415+
],
2416+
"_version": 2
2417+
}
2418+
]
23502419
}
23512420

23522421
}

test/toRedactor.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,18 @@ describe("Testing json to html conversion", () => {
316316

317317
})
318318

319+
describe("RT-483",()=>{
320+
it("should enter br tags equivalent to no.of times 'shift+enter' is pressed",()=>{
321+
const json = expectedValue['RT-483'].json[0];
322+
const html = toRedactor(json);
323+
expect(html).toBe(expectedValue['RT-483'].html[0]);
324+
})
325+
it("should replace /n withing other texts to br tags",()=>{
326+
const json = expectedValue['RT-483'].json[1];
327+
const html = toRedactor(json);
328+
expect(html).toBe(expectedValue['RT-483'].html[1]);
329+
})
330+
})
331+
319332
})
320333

0 commit comments

Comments
 (0)