Skip to content

Commit 14c94f1

Browse files
authored
Merge pull request #64 from contentstack/RT-377--unwanted-fragments
feat: added conditions to reduce fragment and convert b,i tags to strong and em
2 parents bb0c5c0 + 66e50e0 commit 14c94f1

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/fromRedactor.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ const TEXT_TAGS: IHtmlToJsonTextTags = {
121121
I: () => ({ italic: true }),
122122
S: () => ({ strikethrough: true }),
123123
STRONG: () => ({ bold: true }),
124+
B: () => ({ bold: true }),
124125
U: () => ({ underline: true }),
125126
SUP: () => ({ superscript: true }),
126127
SUB: () => ({ subscript: true })
@@ -863,7 +864,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
863864
}
864865
let noOfInlineElement = 0
865866
Array.from(el.parentNode?.childNodes || []).forEach((child: any) => {
866-
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline'))) {
867+
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline')) || child.nodeName in TEXT_TAGS) {
867868
noOfInlineElement += 1
868869
}
869870
})
@@ -1084,4 +1085,4 @@ function getTbodyChildren (rows: any[]) {
10841085

10851086
}, [])
10861087
return newTbodyChildren
1087-
}
1088+
}

test/fromRedactor.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ describe("Testing html to json conversion", () => {
315315
const json = htmlToJson(html)
316316
expect(json).toEqual({ type: "doc", attrs: {}, uid: "uid", children:[{ type: "social-embeds", uid: 'uid', attrs: { src: "https://www.youtube.com/embed/3V-Sq7_uHXQ" }, children: [{ text: ""}] }]})
317317
})
318+
319+
test("should replace all instances of <b> and <i> proper json marks", () => {
320+
const html = `<p><b>Hello</b><i>Test2</i><b>World</b></p>`
321+
const json = htmlToJson(html)
322+
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":"Hello","attrs":{"style":{}},"bold":true},{"text":"Test2","attrs":{"style":{}},"italic":true},{"text":"World","attrs":{"style":{}},"bold":true}]}]})
323+
})
324+
325+
test("should not add fragment for html containing a text tag and span tag", () => {
326+
const html = `<p><strong>Hello</strong><span> Hii</span></p>`
327+
const json = htmlToJson(html)
328+
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":"Hello","attrs":{"style":{}},"bold":true},{"text":" Hii"}]}]})
329+
330+
})
318331
})
319332

320333

0 commit comments

Comments
 (0)