From 4cb73b6aa5dad356c76f95034f6ec798ff4cb45b Mon Sep 17 00:00:00 2001 From: Sachin Arya Date: Wed, 17 Jun 2026 11:39:21 +0530 Subject: [PATCH] fix: skip text wrappers when mark value is falsy Only apply a text wrapper (e.g. bold/italic/underline) when its value is truthy, so marks explicitly set to false no longer wrap the text. Adds tests covering falsy and mixed mark values. Co-authored-by: Cursor --- src/toRedactor.tsx | 2 +- test/toRedactor.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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", () => {