Skip to content

Commit 1d138a7

Browse files
authored
Merge pull request #188 from contentstack/development
Enhance break and newline handling, update dependencies
2 parents fa953fa + 948ed59 commit 1d138a7

16 files changed

+4097
-4093
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fileignoreconfig:
33
ignore_detectors:
44
- filecontent
55
- filename: package-lock.json
6-
checksum: 88174adc8b9dcedecf549defe09988aa4ca95940801e923d829123ba2f3ef6f4
6+
checksum: f5a5aba11ae3778465d1c001151fefa136629e136a7037fbb70fc7c738cb9d36
77
- filename: src/entry-editable.ts
88
checksum: 3ba7af9ed1c1adef2e2bd5610099716562bebb8ba750d4b41ddda99fc9eaf115
99
- filename: .husky/pre-commit

__test__/json-to-html.test.ts

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ import {
3434
testJsonRte,
3535
testJsonAsset,
3636
embeddedAssetAsLinkJsonEntry,
37-
escapeJsonHtml } from './mock/json-element-mock'
37+
escapeJsonHtml,
38+
breakTestEntry,
39+
newlineBreakTestEntry,
40+
multipleNewlinesBreakTestEntry,
41+
plainNewlineTestEntry } from './mock/json-element-mock'
3842
import {
3943
blockquoteHtml,
4044
codeHtml,
@@ -50,19 +54,23 @@ import {
5054
orderListHtml,
5155
paragraphHtml,
5256
paragraphHtmlWithNewLine,
53-
plainTextHtml,
54-
styleinPHtml,
55-
tableHtml,
57+
plainTextHtml,
58+
styleinPHtml,
59+
tableHtml,
5660
unorderListHtml,
5761
plainTextHtmlWithClass,
5862
plainTextHtmlWithId,
5963
htmlTextIdInAttrs,
6064
classAndIdAttrsHtml,
61-
styleObjHtml,
65+
styleObjHtml,
6266
referenceObjHtml,
6367
referenceObjHtmlBlock,
6468
imagetags,
65-
escapeHtml } from './mock/json-element-mock-result'
69+
escapeHtml,
70+
breakTestHtml,
71+
newlineBreakTestHtml,
72+
multipleNewlinesBreakTestHtml,
73+
plainNewlineTestHtml } from './mock/json-element-mock-result'
6674
describe('Node parser paragraph content', () => {
6775
it('Should accept proper values', done => {
6876
const entry = { uid: 'uid'}
@@ -682,4 +690,59 @@ describe('Node parse json_rte Content', () => {
682690
expect(entry.json_rte).toEqual(imagetags)
683691
done()
684692
})
693+
})
694+
695+
describe('Break and Newline handling tests', () => {
696+
it('Should handle break flag in text nodes correctly', done => {
697+
const entry = {...breakTestEntry}
698+
const paths = ['rich_text_editor']
699+
700+
jsonToHTML({ entry, paths })
701+
702+
expect(entry.rich_text_editor).toEqual(breakTestHtml)
703+
done()
704+
})
705+
706+
it('Should handle newline with break flag without duplication', done => {
707+
const entry = {...newlineBreakTestEntry}
708+
const paths = ['rich_text_editor']
709+
710+
jsonToHTML({ entry, paths })
711+
712+
expect(entry.rich_text_editor).toEqual(newlineBreakTestHtml)
713+
done()
714+
})
715+
716+
it('Should handle multiple newlines with break flag correctly', done => {
717+
const entry = {...multipleNewlinesBreakTestEntry}
718+
const paths = ['rich_text_editor']
719+
720+
jsonToHTML({ entry, paths })
721+
722+
expect(entry.rich_text_editor).toEqual(multipleNewlinesBreakTestHtml)
723+
done()
724+
})
725+
726+
it('Should handle plain newlines without break flag', done => {
727+
const entry = {...plainNewlineTestEntry}
728+
const paths = ['rich_text_editor']
729+
730+
jsonToHTML({ entry, paths })
731+
732+
expect(entry.rich_text_editor).toEqual(plainNewlineTestHtml)
733+
done()
734+
})
735+
736+
it('Should handle break flag in arrays', done => {
737+
const entry = {
738+
...breakTestEntry,
739+
rich_text_editor: [breakTestEntry.rich_text_editor]
740+
}
741+
const paths = ['rich_text_editor']
742+
743+
jsonToHTML({ entry, paths })
744+
745+
expect(entry.rich_text_editor).toEqual([breakTestHtml])
746+
done()
747+
})
685748
})

__test__/mock/json-element-mock-result.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const referenceObjHtml = "<p><a class=\"embedded-entry redactor-component block-
2525
const referenceObjHtmlBlock = "<p><a class=\"embedded-entry redactor-component block-entry\" href=\"/Test\" target=\"_self\">Embed entry as a link</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Embed entry as a link open in new tab</a></p><ul><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\">Entry as a link</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Open entry as a link in new tab</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\"><strong><u>Entry as a link bold</u></strong></a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"khjgf\" target=\"_blank\"><strong><u>Open bold entry as a link in new tab </u></strong></a></li><li><a href=\"https://\" target=\"_self\"><strong><u>Link URL</u></strong></a></li><li><a href=\"https://\" target=\"_blank\"><strong><u>Open link in new tab</u></strong></a></li></ul>"
2626
const imagetags = "<figure style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\"><a href=\"https://batman.com\" target=\"_blank\"><img asset_uid=\"asset-UID\" class=\"embedded-asset\" src=\"https://images.contentstack.io/v3/assets/api-key/asset-UID/random-uid/batman.png\" alt=\"batman\" target=\"_blank\" style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\" /></a><figcaption>The Batman</figcaption></figure>"
2727
const escapeHtml = "<p>&lt;p&gt;Welcome to Contentstack! &lt;script&gt;console.log(/&quot;Hello from Contentstack!/&quot;);&lt;/script&gt; Explore our platform to create, manage, and publish content seamlessly.&lt;/p&gt;</p>"
28+
const breakTestHtml = "<p>Normal text with <br />break tag after break.</p>"
29+
const newlineBreakTestHtml = "<p>Text before newline break<br />Text after newline break</p>"
30+
const multipleNewlinesBreakTestHtml = "<p>Text before<br /><br /><br />Text after</p>"
31+
const plainNewlineTestHtml = "<p>Line 1<br />Line 2<br />Line 3</p>"
2832

2933
export {
3034
h1Html,
@@ -53,5 +57,9 @@ export {
5357
referenceObjHtmlBlock,
5458
imagetags,
5559
paragraphHtmlWithNewLine,
56-
escapeHtml
60+
escapeHtml,
61+
breakTestHtml,
62+
newlineBreakTestHtml,
63+
multipleNewlinesBreakTestHtml,
64+
plainNewlineTestHtml
5765
}

__test__/mock/json-element-mock.ts

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,139 @@ const escapeJsonHtml = {
24212421
uid: 'asset_uid_10',
24222422
}
24232423

2424+
const breakTestJson = {
2425+
uid: "break_test_uid",
2426+
_version: 1,
2427+
attrs: {},
2428+
children: [
2429+
{
2430+
type: "p",
2431+
attrs: {},
2432+
uid: "break_paragraph_uid",
2433+
children: [
2434+
{
2435+
text: "Normal text with ",
2436+
},
2437+
{
2438+
text: "break tag",
2439+
break: true
2440+
},
2441+
{
2442+
text: " after break."
2443+
}
2444+
]
2445+
}
2446+
],
2447+
type: "doc"
2448+
}
2449+
2450+
const newlineBreakTestJson = {
2451+
uid: "newline_break_test_uid",
2452+
_version: 1,
2453+
attrs: {},
2454+
children: [
2455+
{
2456+
type: "p",
2457+
attrs: {},
2458+
uid: "newline_break_paragraph_uid",
2459+
children: [
2460+
{
2461+
text: "Text before newline break",
2462+
},
2463+
{
2464+
text: "\n",
2465+
break: true
2466+
},
2467+
{
2468+
text: "Text after newline break"
2469+
}
2470+
]
2471+
}
2472+
],
2473+
type: "doc"
2474+
}
2475+
2476+
const multipleNewlinesBreakTestJson = {
2477+
uid: "multiple_newlines_break_test_uid",
2478+
_version: 1,
2479+
attrs: {},
2480+
children: [
2481+
{
2482+
type: "p",
2483+
attrs: {},
2484+
uid: "multiple_newlines_paragraph_uid",
2485+
children: [
2486+
{
2487+
text: "Text before",
2488+
},
2489+
{
2490+
text: "\n\n\n",
2491+
break: true
2492+
},
2493+
{
2494+
text: "Text after"
2495+
}
2496+
]
2497+
}
2498+
],
2499+
type: "doc"
2500+
}
2501+
2502+
const plainNewlineTestJson = {
2503+
uid: "plain_newline_test_uid",
2504+
_version: 1,
2505+
attrs: {},
2506+
children: [
2507+
{
2508+
type: "p",
2509+
attrs: {},
2510+
uid: "plain_newline_paragraph_uid",
2511+
children: [
2512+
{
2513+
text: "Line 1\nLine 2\nLine 3"
2514+
}
2515+
]
2516+
}
2517+
],
2518+
type: "doc"
2519+
}
2520+
2521+
const breakTestEntry = {
2522+
title: 'Break Test Entry',
2523+
url: '/break-test-entry',
2524+
rich_text_editor: {...breakTestJson},
2525+
locale: 'en-us',
2526+
_in_progress: false,
2527+
uid: 'break_test_entry_uid',
2528+
}
2529+
2530+
const newlineBreakTestEntry = {
2531+
title: 'Newline Break Test Entry',
2532+
url: '/newline-break-test-entry',
2533+
rich_text_editor: {...newlineBreakTestJson},
2534+
locale: 'en-us',
2535+
_in_progress: false,
2536+
uid: 'newline_break_test_entry_uid',
2537+
}
2538+
2539+
const multipleNewlinesBreakTestEntry = {
2540+
title: 'Multiple Newlines Break Test Entry',
2541+
url: '/multiple-newlines-break-test-entry',
2542+
rich_text_editor: {...multipleNewlinesBreakTestJson},
2543+
locale: 'en-us',
2544+
_in_progress: false,
2545+
uid: 'multiple_newlines_break_test_entry_uid',
2546+
}
2547+
2548+
const plainNewlineTestEntry = {
2549+
title: 'Plain Newline Test Entry',
2550+
url: '/plain-newline-test-entry',
2551+
rich_text_editor: {...plainNewlineTestJson},
2552+
locale: 'en-us',
2553+
_in_progress: false,
2554+
uid: 'plain_newline_test_entry_uid',
2555+
}
2556+
24242557
export {
24252558
h1Json,
24262559
h2Json,
@@ -2460,5 +2593,13 @@ export {
24602593
testJsonRte,
24612594
testJsonAsset,
24622595
paragraphEntryWithNewline,
2463-
escapeJsonHtml
2596+
escapeJsonHtml,
2597+
breakTestJson,
2598+
newlineBreakTestJson,
2599+
multipleNewlinesBreakTestJson,
2600+
plainNewlineTestJson,
2601+
breakTestEntry,
2602+
newlineBreakTestEntry,
2603+
multipleNewlinesBreakTestEntry,
2604+
plainNewlineTestEntry
24642605
}

__test__/text-node-to-html.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,94 @@ describe('Text Node To HTML', () => {
122122
expect(resultHtml).toEqual(`<strong><em><u><strike><span data-type='inlineCode'><sub><sup>${textNode.text}</sup></sub></span></strike></u></em></strong>`)
123123
done()
124124
})
125+
126+
it('Should return Break string text', done => {
127+
const node = {
128+
...textNode,
129+
break: true
130+
}
131+
132+
const resultHtml = textNodeToHTML(node, {
133+
...defaultNodeOption
134+
})
135+
136+
expect(resultHtml).toEqual(`<br />${textNode.text}`)
137+
done()
138+
})
139+
140+
it('Should handle newline character without break flag', done => {
141+
const node = {
142+
...textNode,
143+
text: "line1\nline2"
144+
}
145+
146+
const resultHtml = textNodeToHTML(node, {
147+
...defaultNodeOption
148+
})
149+
150+
expect(resultHtml).toEqual('line1<br />line2')
151+
done()
152+
})
153+
154+
it('Should handle single newline with break flag without duplication', done => {
155+
const node = {
156+
...textNode,
157+
text: "\n",
158+
break: true
159+
}
160+
161+
const resultHtml = textNodeToHTML(node, {
162+
...defaultNodeOption
163+
})
164+
165+
expect(resultHtml).toEqual('<br />')
166+
done()
167+
})
168+
169+
it('Should handle multiple newlines with break flag without duplication', done => {
170+
const node = {
171+
...textNode,
172+
text: "\n\n\n",
173+
break: true
174+
}
175+
176+
const resultHtml = textNodeToHTML(node, {
177+
...defaultNodeOption
178+
})
179+
180+
expect(resultHtml).toEqual('<br /><br /><br />')
181+
done()
182+
})
183+
184+
it('Should handle text with newline and break flag properly', done => {
185+
const node = {
186+
...textNode,
187+
text: "text with\nnewline",
188+
break: true
189+
}
190+
191+
const resultHtml = textNodeToHTML(node, {
192+
...defaultNodeOption
193+
})
194+
195+
expect(resultHtml).toEqual('<br />text with<br />newline')
196+
done()
197+
})
198+
199+
it('Should handle break with other marks', done => {
200+
const node = {
201+
...textNode,
202+
text: "break text",
203+
break: true,
204+
bold: true,
205+
italic: true
206+
}
207+
208+
const resultHtml = textNodeToHTML(node, {
209+
...defaultNodeOption
210+
})
211+
212+
expect(resultHtml).toEqual(`<strong><em><br />${node.text}</em></strong>`)
213+
done()
214+
})
125215
})

babel.config.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)