From 7ea7ee02931485a678e3dfe207004d78bbc5654a Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Sun, 28 Jul 2024 21:09:28 +0530 Subject: [PATCH 01/13] fix: RT-253 add missing url for images --- src/toRedactor.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/toRedactor.tsx b/src/toRedactor.tsx index 6449121..f349344 100644 --- a/src/toRedactor.tsx +++ b/src/toRedactor.tsx @@ -478,6 +478,9 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string if (jsonValue['type'] === "style") { delete attrsJson['style-text'] } + if(jsonValue['type'] === 'img'){ + attrsJson['src'] = allattrs['url'] + } if(!(options?.customElementTypes && !isEmpty(options.customElementTypes) && options.customElementTypes[jsonValue['type']])) { delete attrsJson['url'] } From b6a66a4c5574f4c0bfa26f2a37f94c6229bd4741 Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Sun, 28 Jul 2024 21:12:11 +0530 Subject: [PATCH 02/13] fix: RT-264 incorrect condition for width units in images --- src/toRedactor.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/toRedactor.tsx b/src/toRedactor.tsx index f349344..18264ad 100644 --- a/src/toRedactor.tsx +++ b/src/toRedactor.tsx @@ -384,9 +384,9 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string let width = String(allattrs['width']) if (width.slice(width.length - 1) === '%') { - allattrs['width'] = String(allattrs['width']) - } else { allattrs['width'] = allattrs['width'] + '%' + } else { + allattrs['width'] = String(allattrs['width']) } // style = `width: ${allattrs['width']}; height: auto;` } From 1e2da3e1195b45e0f8af4d48eb47c7127a2175fc Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 31 Jul 2024 17:06:26 +0530 Subject: [PATCH 03/13] fix: RT-258 proper HTML conversion for inline properties id and class --- src/fromRedactor.tsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 5bb4d6c..f5fc552 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -191,6 +191,8 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject } if (el.parentNode.nodeName === 'SPAN') { let attrs = { style: {} } + const metadata = {} + if (el.parentNode.style?.color) { attrs = { ...attrs, @@ -218,7 +220,20 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject } } } - return jsx('text', { attrs: attrs }, el.textContent) + if(el.parentNode.getAttribute("id")){ + metadata['id'] = el.parentNode.getAttribute("id") + el.parentNode.removeAttribute("id") + } + if(el.parentNode.getAttribute("class")){ + metadata['classname'] = el.parentNode.getAttribute("class") + el.parentNode.removeAttribute("class") + } + + if(!isEmpty(attrs.style)){ + metadata['attrs'] = attrs + } + + return jsx('text', metadata, el.textContent) } return el.textContent } else if (el.nodeType !== 1) { From de4cc2827786ee7acd19585ba2fab9525485a4c4 Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 31 Jul 2024 17:07:38 +0530 Subject: [PATCH 04/13] fix: RT-258 consider element having inline attribute as inline --- src/fromRedactor.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index f5fc552..5c88a2d 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -819,7 +819,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') { + if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || child.getAttribute('inline')) { noOfInlineElement += 1 } }) From 04915a2c3053da0bf3b66c3930f71cb4103d0467 Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 31 Jul 2024 17:08:07 +0530 Subject: [PATCH 05/13] fix: RT-258 skip span tag if it des not have any attributes --- src/fromRedactor.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 5c88a2d..2d4bdc5 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -832,6 +832,9 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject uid: generateId() } } + if (noOfInlineElement === el.parentNode?.childNodes.length && Array.from(el.attributes).length === 0) { + return children + } } if (children.length === 0) { From 28f9a65520dc0e6efe4bc185870326e345eaecef Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Tue, 6 Aug 2024 11:10:59 +0530 Subject: [PATCH 06/13] fix: RT-263 transformation of assets between html and json and vice-versa --- src/fromRedactor.tsx | 18 +++++++++++++----- src/toRedactor.tsx | 6 ++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 2d4bdc5..35a18e0 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -497,6 +497,9 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject ] ) } + if(el.parentNode?.nodeName === 'FIGURE'){ + return children + } } if (ELEMENT_TAGS[nodeName]) { @@ -874,7 +877,7 @@ const getImageAttributes = (elementAttrs: any, childAttrs: any, extraAttrs: any) ...extraAttrs }, "asset-caption": extraAttrs["asset-caption"], - "link": extraAttrs.link + "link": extraAttrs.link ?? extraAttrs.anchorLink } } if (elementAttrs?.attrs?.["redactor-attributes"]?.link) { @@ -891,11 +894,16 @@ const getImageAttributes = (elementAttrs: any, childAttrs: any, extraAttrs: any) const getReferenceAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAttrs} : any) => { - let { style } = elementAttrs.attrs; - extraAttrs['asset-caption'] = extraAttrs['caption']; + if(newChildren[0].attrs.width){ + delete sizeAttrs.width + } + const style = {} + if (elementAttrs?.attrs?.style?.['text-align']) { + style['text-align'] = elementAttrs?.attrs?.style?.['text-align'] + } - const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style: { 'text-align': style['text-align'] }, position: extraAttrs.position } + const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style , position: extraAttrs.position } extraAttrs = { ...extraAttrs, ...sizeAttrs } if (!childAttrs.position) { @@ -905,7 +913,7 @@ const getReferenceAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAttr const referenceAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs); referenceAttrs.type = "reference"; - + delete referenceAttrs?.attrs?.['redactor-attributes']?.['anchorlink']; return referenceAttrs } diff --git a/src/toRedactor.tsx b/src/toRedactor.tsx index 18264ad..5a200fb 100644 --- a/src/toRedactor.tsx +++ b/src/toRedactor.tsx @@ -125,7 +125,7 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = { } else if (extraAttrs?.displayType === "display") { - const anchor = jsonBlock?.["attrs"]?.["link"]; + const anchor = jsonBlock?.["attrs"]?.["link"] ?? jsonBlock?.["attrs"]?.["anchorLink"]; const caption = jsonBlock?.["attrs"]?.["asset-caption"]; const position = jsonBlock?.["attrs"]?.["position"]; @@ -134,7 +134,9 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = { const figureStyles = { margin: "0", }; - attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs; + if(!attrs.includes(`src="${jsonBlock?.["attrs"]?.["asset-link"]}`)){ + attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs; + } let img = ``; if (anchor) { From 82bfaf8b48b88dd866168ef074d5db18b0447220 Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Tue, 6 Aug 2024 16:02:11 +0530 Subject: [PATCH 07/13] fix: RT-258 consider element having inline attribute as inline --- src/fromRedactor.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 35a18e0..ceeea77 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -164,6 +164,9 @@ const traverseChildAndWarpChild = (children: Array) => { } else { inlineElementIndex.push(index) } + } + else if (child.attrs.inline) { + inlineElementIndex.push(index) } else { hasBlockElement = true } From 8bbd60ac96d77e87c1edf5082ca495facaf4a2dd Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 7 Aug 2024 14:36:14 +0530 Subject: [PATCH 08/13] test: use the jest matchers for verbose failures --- test/toRedactor.test.ts | 43 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/test/toRedactor.test.ts b/test/toRedactor.test.ts index a3ba579..947783e 100644 --- a/test/toRedactor.test.ts +++ b/test/toRedactor.test.ts @@ -9,29 +9,25 @@ describe("Testing json to html conversion", () => { let jsonValue = expectedValue["2"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['2'].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['2'].html) }) it("table conversion", () => { let jsonValue = expectedValue["3"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['3'].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['3'].html) }) it("basic formating, block and code conversion", () => { let jsonValue = expectedValue["4"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['4'].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['4'].html) }) it("List and alignment conversion", () => { let jsonValue = expectedValue["5"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['5'].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['5'].html) }) it.each(["6", "RT-154"])("Image and iframe conversion", (index) => { let jsonValue = expectedValue[index].json @@ -43,8 +39,7 @@ describe("Testing json to html conversion", () => { let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['7'].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['7'].html) }) it("Embed entry as link", () => { let jsonValue = expectedValue["8"].json @@ -52,22 +47,19 @@ describe("Testing json to html conversion", () => { let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['8'].htmlUpdated) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['8'].htmlUpdated) }) it("Embedded entry as inline block", () => { let jsonValue = expectedValue["9"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['9'].htmlUpdated) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['9'].htmlUpdated) }) it("Embedded entry as block", () => { let jsonValue = expectedValue["10"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue['10'].htmlValue) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue['10'].htmlValue) }) it("Custom ELEMENT_TYPES",() => { let cases = ["15","16","18"] @@ -76,8 +68,7 @@ describe("Testing json to html conversion", () => { let htmlValue = toRedactor({ type: "doc", attrs: {}, children: json },{customElementTypes:expectedValue[index].customElementTypes}) //console.log(htmlValue) //console.log(expectedValue[index].html) - let testResult = isEqual(htmlValue, expectedValue[index].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue[index].html) }) }) it("Custom TEXT_WRAPPER",() => { @@ -87,8 +78,7 @@ describe("Testing json to html conversion", () => { let htmlValue = toRedactor({ type: "doc", attrs: {}, children: json },{customTextWrapper:expectedValue[index].customTextWrapper}) //console.log(htmlValue) //console.log(expectedValue[index].html) - let testResult = isEqual(htmlValue, expectedValue[index].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue[index].html) }) }) it("Conversion with allowNonStandardTags", () => { @@ -98,12 +88,7 @@ describe("Testing json to html conversion", () => { console.warn = mockFunction let json = expectedValue[index]?.json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: json },{allowNonStandardTypes:true,customTextWrapper:expectedValue[index].customTextWrapper}) - let testResult = isEqual(htmlValue, expectedValue[index].html) - if(!testResult){ - //console.log(htmlValue) - //console.log(expectedValue[index].html) - } - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue[index].html) expect(mockFunction).toHaveBeenCalledTimes(expectedValue[index].nonStandardTags) }) @@ -111,15 +96,13 @@ describe("Testing json to html conversion", () => { it('"\n" to
conversion', () => { let jsonValue = expectedValue["'\n' to
"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue["'\n' to
"].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue["'\n' to
"].html) }) it("Inline classname and id", () => { let jsonValue = expectedValue["inline-classname-and-id"].json let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) - let testResult = isEqual(htmlValue, expectedValue["inline-classname-and-id"].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue["inline-classname-and-id"].html) }) describe("Nested attrs", () => { From a1ea2f2776af08b43675c0c34d0e18e03ef8af8c Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 7 Aug 2024 14:55:43 +0530 Subject: [PATCH 09/13] test: RT-253 proper src attribute for images --- test/toRedactor.test.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/toRedactor.test.ts b/test/toRedactor.test.ts index 947783e..459ad57 100644 --- a/test/toRedactor.test.ts +++ b/test/toRedactor.test.ts @@ -222,8 +222,15 @@ describe("Testing json to html conversion", () => { let jsonValue = expectedValue["fix_EB-745"].expectedJson let htmlValue = toRedactor(jsonValue) - let testResult = isEqual(htmlValue, expectedValue["fix_EB-745"].html) - expect(testResult).toBe(true) + expect(htmlValue).toBe(expectedValue["fix_EB-745"].html) }) + + test("RT-253 - should convert to proper HTML image code", () => { + const json = {"type":"doc","attrs":{},"children":[{"type":"img","attrs":{"url":"***REMOVED***200","width":100},"children":[{"text":""}]}],"_version":3 } + const html = toRedactor(json); + console.log(html) + expect(html).toBe(''); + }); -}) \ No newline at end of file +}) + From bfc67364d703d7125c5d69f0ea815d6d3f43afaf Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 7 Aug 2024 15:50:55 +0530 Subject: [PATCH 10/13] test: RT-264 reference asset width gets converted to % unit --- test/toRedactor.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/toRedactor.test.ts b/test/toRedactor.test.ts index 459ad57..2ec85e4 100644 --- a/test/toRedactor.test.ts +++ b/test/toRedactor.test.ts @@ -228,9 +228,13 @@ describe("Testing json to html conversion", () => { test("RT-253 - should convert to proper HTML image code", () => { const json = {"type":"doc","attrs":{},"children":[{"type":"img","attrs":{"url":"***REMOVED***200","width":100},"children":[{"text":""}]}],"_version":3 } const html = toRedactor(json); - console.log(html) expect(html).toBe(''); }); + test("RT-264 - reference asset should have proper unit in the converted image", () => { + const json = {"type":"doc","attrs":{},"uid":"6a547ebccbd74c0c9a521ee95acfb223","children":[{"uid":"942be31c040145b6a7541ec4f73754c5","type":"reference","attrs":{"display-type":"display","asset-uid":"bltcbce74d3891aaa9d","content-type-uid":"sys_assets","asset-link":"***REMOVED***200","asset-name":"MATHERAN.jpg","asset-type":"image/jpeg","type":"asset","class-name":"embedded-asset","width":"192","style":{"max-height":"144px","height":"144px","text-align":"right","max-width":"192px","width":"auto"},"redactor-attributes":{"height":"144","position":"right"},"max-height":"144","height":"144","position":"right","max-width":"192"},"children":[{"text":""}]}],"_version":1 } + const html = toRedactor(json); + expect(html).toBe(`
`); + }) }) From 45fda17e2db207dc9f3c5a0385023655ec9c08ba Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 7 Aug 2024 17:03:20 +0530 Subject: [PATCH 11/13] test: RT-258 non standard element as inline in inline is true --- src/fromRedactor.tsx | 8 ++++---- test/fromRedactor.test.ts | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index ceeea77..555ce99 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -144,7 +144,7 @@ const traverseChildAndModifyChild = (element: any, attrsForChild: any) => { Array.from(element.children || []).map((el) => traverseChildAndModifyChild(el, attrsForChild)).flat() return } -const traverseChildAndWarpChild = (children: Array) => { +const traverseChildAndWarpChild = (children: Array, allowNonStandardTags: boolean = false) => { let inlineElementIndex: Array = [] let hasBlockElement = false let childrenCopy = cloneDeep(children) @@ -165,7 +165,7 @@ const traverseChildAndWarpChild = (children: Array) => { inlineElementIndex.push(index) } } - else if (child.attrs.inline) { + else if (allowNonStandardTags && child?.attrs?.inline) { inlineElementIndex.push(index) } else { hasBlockElement = true @@ -282,7 +282,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject } let children: any = flatten(Array.from(parent.childNodes).map((child) => fromRedactor(child, options))) children = children.filter((child: any) => child !== null) - children = traverseChildAndWarpChild(children) + children = traverseChildAndWarpChild(children, options?.allowNonStandardTags) if (children.length === 0) { children = [{ text: '' }] } @@ -825,7 +825,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' || child.getAttribute('inline')) { + if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline'))) { noOfInlineElement += 1 } }) diff --git a/test/fromRedactor.test.ts b/test/fromRedactor.test.ts index 30a47d8..a5f9d68 100644 --- a/test/fromRedactor.test.ts +++ b/test/fromRedactor.test.ts @@ -4,6 +4,7 @@ import { JSDOM } from "jsdom" import isEqual from "lodash.isequal" import omitdeep from "omit-deep-lodash" import expectedValue from "./expectedJson" +import { IHtmlToJsonOptions } from "../src/types" const docWrapper = (children: any) => { return { @@ -241,7 +242,7 @@ describe("Testing html to json conversion", () => { test("should not convert stringify attrs when `allowNonStandardTags` is not true", () => { const html = `

Hi There!

`; - const json = {"attrs": {}, "children": [{"attrs": {}, "children": [{"attrs": {"redactor-attributes": {"from": "Paul, Addy", "to": "[object Object]"}, "style": {}}, "children": [{"attrs": {"style": {}}, "text": "Hi There!"}], "type": "span", "uid": "uid"}], "type": "p", "uid": "uid"}], "type": "doc", "uid": "uid"}; + const json = {"attrs": {}, "children": [{"attrs": {}, "children": [{"attrs": {"redactor-attributes": {"from": "Paul, Addy", "to": "[object Object]"}, "style": {}}, "children": [{"text": "Hi There!"}], "type": "span", "uid": "uid"}], "type": "p", "uid": "uid"}], "type": "doc", "uid": "uid"}; const dom = new JSDOM(html); let htmlDoc = dom.window.document.querySelector("body"); @@ -250,6 +251,38 @@ describe("Testing html to json conversion", () => { }); }) + describe("SPAN", () => { + + test("should properly convert inline properties id and class to json", () => { + let html =`

Hello World

` + const json = htmlToJson(html) + expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{"style":{},"redactor-attributes":{"dir":"ltr"}},"uid":"uid","children":[{"text":"Hello "},{"text":"World","id":"id","classname":"class"}]}]}) + }) + + test("should skip span if other element are inline and it does not have any attributes", () => { + let html =`

Hello World

` + const json = htmlToJson(html) + expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{"style":{},"redactor-attributes":{"dir":"ltr"}},"uid":"uid","children":[{"text":"Hello "},{"text":"World"}]}]}) + }) + + test("should not skip span if other element are inline and it does have any attribute", () => { + let html =`

Hello World

` + const json = htmlToJson(html) + expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{"style":{},"redactor-attributes":{"dir":"ltr"}},"uid":"uid","children":[{"text":"Hello "},{"type":"span","attrs":{"style":{},"redactor-attributes":{"data-test":"test"}},"uid":"uid","children":[{"text":"World"}]}]}]}) + }) + + test("should consider the non standard elements as inline if it has attribute of inline with the span tag", () => { + let html = `

Being an absolute tropical stunner

` + let jsonValue = htmlToJson(html, { allowNonStandardTags: true }) + expect(jsonValue).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"type":"unknown","attrs":{"inline":"true"},"children":[{"text":""}]},{"text":"Being an absolute "},{"text":"tropical"},{"text":" stunner"}]}] }) + }) + }) + + test("should consider the non standard elements as inline if it has attribute of inline", () => { + let html = `

Being an absolute tropical stunner

` + let jsonValue = htmlToJson(html, { allowNonStandardTags: true }) + expect(jsonValue).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"type":"unknown","attrs":{"inline":"true"},"children":[{"text":""}]},{"text":"Being an absolute "},{"type":"a","attrs":{"url":"https://chess.com","style":{},"redactor-attributes":{"href":"https://chess.com"}},"uid":"uid","children":[{"text":"tropical"}]},{"text":" stunner"}]}] }) + }) }) @@ -327,7 +360,7 @@ describe("CS-41001", () =>{ }) }) -function htmlToJson (html, options) { +function htmlToJson (html: string, options: IHtmlToJsonOptions) { const dom = new JSDOM(html); let htmlDoc = dom.window.document.querySelector("body"); return fromRedactor(htmlDoc, options); From b8d1d1ae6cbf6dbcbf14344545ce62e6fce9e4e6 Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 7 Aug 2024 18:09:06 +0530 Subject: [PATCH 12/13] test: RT-263 asset transformation with caption and position --- test/expectedJson.ts | 4 ++-- test/fromRedactor.test.ts | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/test/expectedJson.ts b/test/expectedJson.ts index 5bdaf86..d59648e 100644 --- a/test/expectedJson.ts +++ b/test/expectedJson.ts @@ -1693,7 +1693,7 @@ export default {
Landscape
`, - "json": {"type":"doc","uid":"d712f7746e984559bbf591d689ef69f6","attrs":{},"children":[{"type":"p","attrs":{},"uid":"79d6defd99624ecc8302db0479330d72","children":[{"text":""}]},{"type":"reference","attrs":{"style":{"text-align":"center"},"redactor-attributes":{"asset_uid":"bltfea8157ddfb8e776","src":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","position":"center","captionAttrs":{"style":"text-align: center;"},"caption":"Landscape","anchorLink":"https://app.contentstack.com/","asset-caption":"Landscape","width":204},"asset-name":"landscape-3.jpg","content-type-uid":"sys_assets","asset-link":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","asset-type":"image/jpg","display-type":"display","type":"asset","asset-uid":"bltfea8157ddfb8e776","width":204,"position":"center","asset-caption":"Landscape"},"uid":"abc8e6a7f2974ad2a876356a6f4ae0fb","children":[{"text":""}]}]} + "json": {"type":"doc","uid":"d712f7746e984559bbf591d689ef69f6","attrs":{},"children":[{"type":"p","attrs":{},"uid":"79d6defd99624ecc8302db0479330d72","children":[{"text":""}]},{"type":"reference","attrs":{"style":{"text-align":"center"},"redactor-attributes":{"asset_uid":"bltfea8157ddfb8e776","src":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","position":"center","captionAttrs":{"style":"text-align: center;"},"caption":"Landscape","anchorLink":"https://app.contentstack.com/","asset-caption":"Landscape","width":204},"link": "https://app.contentstack.com/","asset-name":"landscape-3.jpg","content-type-uid":"sys_assets","asset-link":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","asset-type":"image/jpg","display-type":"display","type":"asset","asset-uid":"bltfea8157ddfb8e776","width":204,"position":"center","asset-caption":"Landscape"},"uid":"abc8e6a7f2974ad2a876356a6f4ae0fb","children":[{"text":""}]}]} }, "anchor-reference-width-position-caption": { "html": @@ -1701,7 +1701,7 @@ export default {
Landscape
`, - "json": {"type":"doc","uid":"d6cd7b938dcc41a8a75fb8bad29aa2e9","attrs":{},"children":[{"type":"p","attrs":{},"uid":"c17f2b982464422aaa58499b9525b437","children":[{"text":""}]},{"type":"img","attrs":{"style":{"text-align":"center"},"redactor-attributes":{"src":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","position":"center","captionAttrs":{"style":"text-align: center;"},"caption":"Landscape","anchorLink":"https://app.contentstack.com/","width":204},"url":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","width":204,"caption":"Landscape"},"uid":"929929b627704d38ae53ba6792ec8f69","children":[{"text":""}]}]} + "json": {"type":"doc","uid":"d6cd7b938dcc41a8a75fb8bad29aa2e9","attrs":{},"children":[{"type":"p","attrs":{},"uid":"c17f2b982464422aaa58499b9525b437","children":[{"text":""}]},{"type":"img","attrs":{"style":{"text-align":"center"},"redactor-attributes":{"src":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","position":"center","captionAttrs":{"style":"text-align: center;"},"caption":"Landscape","anchorLink":"https://app.contentstack.com/","width":204},"url":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","width":204,"caption":"Landscape","link":"https://app.contentstack.com/"},"children":[{"text":""}]}]} }, "'\n' to
": { "html": '

This is test for break element
This is text on the next line.

', diff --git a/test/fromRedactor.test.ts b/test/fromRedactor.test.ts index a5f9d68..37317a9 100644 --- a/test/fromRedactor.test.ts +++ b/test/fromRedactor.test.ts @@ -283,6 +283,18 @@ describe("Testing html to json conversion", () => { let jsonValue = htmlToJson(html, { allowNonStandardTags: true }) expect(jsonValue).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"type":"unknown","attrs":{"inline":"true"},"children":[{"text":""}]},{"text":"Being an absolute "},{"type":"a","attrs":{"url":"https://chess.com","style":{},"redactor-attributes":{"href":"https://chess.com"}},"uid":"uid","children":[{"text":"tropical"}]},{"text":" stunner"}]}] }) }) + + + test("should convert asset to reference", () => { + const html = `
+
image_(9).png +
ss
+
+
+

` + const json = htmlToJson(html) + expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"reference","attrs":{"style":{"text-align":"right"},"redactor-attributes":{"src":"***REMOVED***200","height":"141","alt":"image_(9).png","caption":"ss","type":"asset","asset-alt":"image_(9).png","max-height":"141","max-width":"148","sys-style-type":"display","position":"right","captionAttrs":{"style":"text-align:center"},"anchorLink":"ss.com","target":true,"asset-caption":"ss"},"class-name":"embedded-asset","width":148,"type":"asset","asset-caption":"ss","link":"ss.com","asset-alt":"image_(9).png","target":"_blank","position":"right","asset-link":"***REMOVED***200","asset-uid":"blt137d845621ef8168","display-type":"display","asset-name":"image_(9).png","asset-type":"image/png","content-type-uid":"sys_assets"},"uid":"uid","children":[{"text":""}]},{"type":"p","attrs":{},"uid":"uid","children":[{"text":""}]}] }) + }) }) @@ -360,9 +372,14 @@ describe("CS-41001", () =>{ }) }) + + + + function htmlToJson (html: string, options: IHtmlToJsonOptions) { const dom = new JSDOM(html); let htmlDoc = dom.window.document.querySelector("body"); return fromRedactor(htmlDoc, options); -} \ No newline at end of file +} + From 4494f2c2416247c6a4034841086d8bd68d245e1a Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Wed, 7 Aug 2024 18:10:00 +0530 Subject: [PATCH 13/13] chore: update package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ecd7740..2de8391 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/json-rte-serializer", - "version": "2.0.8", + "version": "2.0.9", "description": "This Package converts Html Document to Json and vice-versa.", "main": "lib/index.js", "module": "lib/index.mjs",