Skip to content

Commit 18384fd

Browse files
committed
test: cases added for indent
1 parent 1ef95f1 commit 18384fd

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed

test/fromRedactor.test.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,141 @@ describe("ELEMENT_TAGS", () => {
421421
})
422422
})
423423

424+
describe("Indent support", () => {
425+
test("should parse data-indent-level from paragraph", () => {
426+
const html = `<p data-indent-level="1">Indented paragraph</p>`
427+
const json = htmlToJson(html)
428+
expect(json).toStrictEqual({
429+
"type": "doc",
430+
"uid": "uid",
431+
"attrs": {},
432+
"children": [{
433+
"type": "p",
434+
"attrs": {
435+
"style": {},
436+
"data-indent-level": "1",
437+
"redactor-attributes": {}
438+
},
439+
"uid": "uid",
440+
"children": [{ "text": "Indented paragraph" }]
441+
}]
442+
})
443+
})
444+
445+
test("should parse data-indent-level from paragraph with other attributes", () => {
446+
const html = `<p class="custom-class" data-indent-level="1" id="para-1">Indented paragraph</p>`
447+
const json = htmlToJson(html)
448+
expect(json).toStrictEqual({
449+
"type": "doc",
450+
"uid": "uid",
451+
"attrs": {},
452+
"children": [{
453+
"type": "p",
454+
"attrs": {
455+
"style": {},
456+
"data-indent-level": "1",
457+
"id": "para-1",
458+
"class-name": "custom-class",
459+
"redactor-attributes": {
460+
"class": "custom-class",
461+
"id": "para-1"
462+
}
463+
},
464+
"uid": "uid",
465+
"children": [{ "text": "Indented paragraph" }]
466+
}]
467+
})
468+
})
469+
470+
test("should parse data-indent-level from paragraph with bold text", () => {
471+
const html = `<p data-indent-level="1"><strong>Bold indented text</strong></p>`
472+
const json = htmlToJson(html)
473+
expect(json).toStrictEqual({
474+
"type": "doc",
475+
"uid": "uid",
476+
"attrs": {},
477+
"children": [{
478+
"type": "p",
479+
"attrs": {
480+
"style": {},
481+
"data-indent-level": "1",
482+
"redactor-attributes": {}
483+
},
484+
"uid": "uid",
485+
"children": [{ "text": "Bold indented text", "attrs": { "style": {} }, "bold": true }]
486+
}]
487+
})
488+
})
489+
490+
test("should parse data-indent-level from heading", () => {
491+
const html = `<h1 data-indent-level="1">Indented Heading</h1>`
492+
const json = htmlToJson(html)
493+
expect(json).toStrictEqual({
494+
"type": "doc",
495+
"uid": "uid",
496+
"attrs": {},
497+
"children": [{
498+
"type": "h1",
499+
"attrs": {
500+
"style": {},
501+
"data-indent-level": "1",
502+
"redactor-attributes": {}
503+
},
504+
"uid": "uid",
505+
"children": [{ "text": "Indented Heading" }]
506+
}]
507+
})
508+
})
509+
510+
test("should parse data-indent-level from blockquote", () => {
511+
const html = `<blockquote data-indent-level="1">Indented blockquote</blockquote>`
512+
const json = htmlToJson(html)
513+
expect(json).toStrictEqual({
514+
"type": "doc",
515+
"uid": "uid",
516+
"attrs": {},
517+
"children": [{
518+
"type": "blockquote",
519+
"attrs": {
520+
"style": {},
521+
"data-indent-level": "1",
522+
"redactor-attributes": {}
523+
},
524+
"uid": "uid",
525+
"children": [{ "text": "Indented blockquote" }]
526+
}]
527+
})
528+
})
529+
530+
test("should parse data-indent-level from figure with image", () => {
531+
const html = `<figure data-indent-level="1"><img src="test.png" /></figure>`
532+
const json = htmlToJson(html)
533+
expect(json).toStrictEqual({
534+
"type": "doc",
535+
"uid": "uid",
536+
"attrs": {},
537+
"children": [{
538+
"type": "img",
539+
"attrs": {
540+
"style": {
541+
"text-align": undefined
542+
},
543+
"url": "test.png",
544+
"width": "auto",
545+
"data-indent-level": "1",
546+
"redactor-attributes": {
547+
"src": "test.png",
548+
"position": null,
549+
"width": "auto"
550+
}
551+
},
552+
"uid": "uid",
553+
"children": [{ "text": "" }]
554+
}]
555+
})
556+
})
557+
})
558+
424559
function htmlToJson (html: string, options: IHtmlToJsonOptions) {
425560
const dom = new JSDOM(html);
426561
let htmlDoc = dom.window.document.querySelector("body");

test/toRedactor.test.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,116 @@ test("should convert codeblock to proper html, where \n should not be replaced w
353353
const html = toRedactor(json);
354354
expect(html).toBe(`<pre>Hi\nHello</pre>`);
355355
})
356+
357+
describe("Indent support", () => {
358+
test("should convert paragraph with data-indent-level to HTML", () => {
359+
const json = {
360+
"type": "doc",
361+
"uid": "uid",
362+
"attrs": {},
363+
"children": [{
364+
"type": "p",
365+
"attrs": {
366+
"data-indent-level": "1"
367+
},
368+
"uid": "uid",
369+
"children": [{ "text": "Indented paragraph" }]
370+
}]
371+
}
372+
const html = toRedactor(json)
373+
expect(html).toBe(`<p data-indent-level="1">Indented paragraph</p>`)
374+
})
375+
376+
test("should convert paragraph with data-indent-level and other attributes to HTML", () => {
377+
const json = {
378+
"type": "doc",
379+
"uid": "uid",
380+
"attrs": {},
381+
"children": [{
382+
"type": "p",
383+
"attrs": {
384+
"data-indent-level": "1",
385+
"id": "para-1",
386+
"class-name": "custom-class"
387+
},
388+
"uid": "uid",
389+
"children": [{ "text": "Indented paragraph" }]
390+
}]
391+
}
392+
const html = toRedactor(json)
393+
expect(html).toBe(`<p class="custom-class" data-indent-level="1" id="para-1">Indented paragraph</p>`)
394+
})
395+
396+
test("should convert paragraph with data-indent-level and bold text to HTML", () => {
397+
const json = {
398+
"type": "doc",
399+
"uid": "uid",
400+
"attrs": {},
401+
"children": [{
402+
"type": "p",
403+
"attrs": {
404+
"data-indent-level": "1"
405+
},
406+
"uid": "uid",
407+
"children": [{ "text": "Bold indented text", "bold": true }]
408+
}]
409+
}
410+
const html = toRedactor(json)
411+
expect(html).toBe(`<p data-indent-level="1"><strong>Bold indented text</strong></p>`)
412+
})
413+
414+
test("should convert heading with data-indent-level to HTML", () => {
415+
const json = {
416+
"type": "doc",
417+
"uid": "uid",
418+
"attrs": {},
419+
"children": [{
420+
"type": "h1",
421+
"attrs": {
422+
"data-indent-level": "1"
423+
},
424+
"uid": "uid",
425+
"children": [{ "text": "Indented Heading" }]
426+
}]
427+
}
428+
const html = toRedactor(json)
429+
expect(html).toBe(`<h1 data-indent-level="1">Indented Heading</h1>`)
430+
})
431+
432+
test("should convert blockquote with data-indent-level to HTML", () => {
433+
const json = {
434+
"type": "doc",
435+
"uid": "uid",
436+
"attrs": {},
437+
"children": [{
438+
"type": "blockquote",
439+
"attrs": {
440+
"data-indent-level": "1"
441+
},
442+
"uid": "uid",
443+
"children": [{ "text": "Indented blockquote" }]
444+
}]
445+
}
446+
const html = toRedactor(json)
447+
expect(html).toBe(`<blockquote data-indent-level="1">Indented blockquote</blockquote>`)
448+
})
449+
450+
test("should convert image with data-indent-level to HTML", () => {
451+
const json = {
452+
"type": "doc",
453+
"uid": "uid",
454+
"attrs": {},
455+
"children": [{
456+
"type": "img",
457+
"attrs": {
458+
"url": "test.png",
459+
"data-indent-level": "1"
460+
},
461+
"uid": "uid",
462+
"children": [{ "text": "" }]
463+
}]
464+
}
465+
const html = toRedactor(json)
466+
expect(html).toBe(`<img data-indent-level="1" src="test.png" />`)
467+
})
468+
})

0 commit comments

Comments
 (0)