Skip to content

Commit

Permalink
Merge pull request #65 from contentstack/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Jayesh2812 authored Dec 5, 2024
2 parents 27c1708 + 7a51491 commit 3ea05b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/json-rte-serializer",
"version": "2.0.11",
"version": "2.0.12",
"description": "This Package converts Html Document to Json and vice-versa.",
"main": "lib/index.js",
"module": "lib/index.mjs",
Expand Down
5 changes: 3 additions & 2 deletions src/fromRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const TEXT_TAGS: IHtmlToJsonTextTags = {
I: () => ({ italic: true }),
S: () => ({ strikethrough: true }),
STRONG: () => ({ bold: true }),
B: () => ({ bold: true }),
U: () => ({ underline: true }),
SUP: () => ({ superscript: true }),
SUB: () => ({ subscript: true })
Expand Down Expand Up @@ -863,7 +864,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
}
let noOfInlineElement = 0
Array.from(el.parentNode?.childNodes || []).forEach((child: any) => {
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline'))) {
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline')) || child.nodeName in TEXT_TAGS) {
noOfInlineElement += 1
}
})
Expand Down Expand Up @@ -1084,4 +1085,4 @@ function getTbodyChildren (rows: any[]) {

}, [])
return newTbodyChildren
}
}
13 changes: 13 additions & 0 deletions test/fromRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ describe("Testing html to json conversion", () => {
const json = htmlToJson(html)
expect(json).toEqual({ type: "doc", attrs: {}, uid: "uid", children:[{ type: "social-embeds", uid: 'uid', attrs: { src: "https://www.***REMOVED***.com/embed/3V-Sq7_uHXQ" }, children: [{ text: ""}] }]})
})

test("should replace all instances of <b> and <i> proper json marks", () => {
const html = `<p><b>Hello</b><i>Test2</i><b>World</b></p>`
const json = htmlToJson(html)
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}]}]})
})

test("should not add fragment for html containing a text tag and span tag", () => {
const html = `<p><strong>Hello</strong><span> Hii</span></p>`
const json = htmlToJson(html)
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":"Hello","attrs":{"style":{}},"bold":true},{"text":" Hii"}]}]})

})
})


Expand Down

0 comments on commit 3ea05b3

Please sign in to comment.