@@ -141,6 +141,129 @@ describe('Node parser paragraph content', () => {
141141 expect ( entry . rich_text_editor ) . toEqual ( escapeHtml )
142142 done ( )
143143 } )
144+
145+ it ( 'Should escape HTML tags in text content to prevent rendering as HTML' , done => {
146+ const entry = {
147+ uid : 'test_uid' ,
148+ rich_text_editor : {
149+ uid : "uid" ,
150+ _version : 1 ,
151+ attrs : { } ,
152+ children : [
153+ {
154+ children : [
155+ {
156+ text : '<b>Hello <i>world</i></b>! This is paragraph 1.'
157+ }
158+ ] ,
159+ type : 'p' ,
160+ uid : 'hjsbhys1234' ,
161+ attrs : { }
162+ }
163+ ] ,
164+ type : "doc"
165+ }
166+ }
167+
168+ jsonToHTML ( { entry, paths : [ 'rich_text_editor' ] } )
169+
170+ expect ( entry . rich_text_editor ) . toEqual ( '<p><b>Hello <i>world</i></b>! This is paragraph 1.</p>' )
171+ done ( )
172+ } )
173+
174+ it ( 'Should escape HTML tags in text with actual bold formatting' , done => {
175+ const entry = {
176+ uid : 'test_uid' ,
177+ rich_text_editor : {
178+ uid : "uid" ,
179+ _version : 1 ,
180+ attrs : { } ,
181+ children : [
182+ {
183+ children : [
184+ {
185+ text : 'This is ' ,
186+ } ,
187+ {
188+ text : '<b>bold text</b>' ,
189+ bold : true
190+ } ,
191+ {
192+ text : ' with HTML tags'
193+ }
194+ ] ,
195+ type : 'p' ,
196+ uid : 'test_uid_1' ,
197+ attrs : { }
198+ }
199+ ] ,
200+ type : "doc"
201+ }
202+ }
203+
204+ jsonToHTML ( { entry, paths : [ 'rich_text_editor' ] } )
205+
206+ expect ( entry . rich_text_editor ) . toEqual ( '<p>This is <strong><b>bold text</b></strong> with HTML tags</p>' )
207+ done ( )
208+ } )
209+
210+ it ( 'Should escape dangerous script tags in text content' , done => {
211+ const entry = {
212+ uid : 'test_uid' ,
213+ rich_text_editor : {
214+ uid : "uid" ,
215+ _version : 1 ,
216+ attrs : { } ,
217+ children : [
218+ {
219+ children : [
220+ {
221+ text : '<script>alert("XSS")</script> This should be safe'
222+ }
223+ ] ,
224+ type : 'p' ,
225+ uid : 'test_uid_2' ,
226+ attrs : { }
227+ }
228+ ] ,
229+ type : "doc"
230+ }
231+ }
232+
233+ jsonToHTML ( { entry, paths : [ 'rich_text_editor' ] } )
234+
235+ expect ( entry . rich_text_editor ) . toEqual ( '<p><script>alert("XSS")</script> This should be safe</p>' )
236+ done ( )
237+ } )
238+
239+ it ( 'Should escape HTML entities like ampersand and quotes in text' , done => {
240+ const entry = {
241+ uid : 'test_uid' ,
242+ rich_text_editor : {
243+ uid : "uid" ,
244+ _version : 1 ,
245+ attrs : { } ,
246+ children : [
247+ {
248+ children : [
249+ {
250+ text : 'Text with & ampersand and "quotes" should be escaped'
251+ }
252+ ] ,
253+ type : 'p' ,
254+ uid : 'test_uid_3' ,
255+ attrs : { }
256+ }
257+ ] ,
258+ type : "doc"
259+ }
260+ }
261+
262+ jsonToHTML ( { entry, paths : [ 'rich_text_editor' ] } )
263+
264+ expect ( entry . rich_text_editor ) . toEqual ( '<p>Text with & ampersand and "quotes" should be escaped</p>' )
265+ done ( )
266+ } )
144267} )
145268
146269
0 commit comments