diff --git a/src/toRedactor.tsx b/src/toRedactor.tsx index 1bfd4b5..9360120 100644 --- a/src/toRedactor.tsx +++ b/src/toRedactor.tsx @@ -48,7 +48,7 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string text = text.replace(/\n/g, '
') } Object.entries(jsonValue).forEach(([key, value]) => { - if(localTextWrappers.hasOwnProperty(key)){ + if(localTextWrappers.hasOwnProperty(key) && value){ text = localTextWrappers[key](text,value) } }) diff --git a/test/toRedactor.test.ts b/test/toRedactor.test.ts index ef9cbf1..81fe280 100644 --- a/test/toRedactor.test.ts +++ b/test/toRedactor.test.ts @@ -104,6 +104,28 @@ describe("Testing json to html conversion", () => { expect(htmlValue).toBe(expectedValue["inline-classname-and-id"].html) }) + describe("Falsy text marks", () => { + it("does not wrap text when bold/italic/underline are false", () => { + const jsonValue = [{ + type: "p", + attrs: {}, + children: [{ text: "Plain text", bold: false, italic: false, underline: false }] + }] + const htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) + expect(htmlValue).toBe("

Plain text

") + }) + + it("wraps only the marks whose value is true", () => { + const jsonValue = [{ + type: "p", + attrs: {}, + children: [{ text: "Mixed", bold: true, italic: false, underline: true }] + }] + const htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue }) + expect(htmlValue).toBe("

Mixed

") + }) + }) + describe("Nested attrs", () => { test("should have stringified attrs for nested json", () => {