Skip to content

Commit fda7929

Browse files
authored
Merge pull request #29 from contentstack/CS-41001--text-node-whitespace
feat: added condiition to check for text nodes with empty white space
2 parents 3998211 + 9c1b350 commit fda7929

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/fromRedactor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ const traverseChildAndWarpChild = (children: Array<Object>) => {
160160

161161
const whiteCharPattern = /^[\s ]{2,}$/
162162
export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject | null => {
163+
// If node is text node
163164
if (el.nodeType === 3) {
164165
if (whiteCharPattern.test(el.textContent)) return null
166+
if (["TABLE", "THEAD", "TBODY", "TR"].includes(el?.parentElement?.nodeName ?? "") && el?.textContent?.trim?.()?.length === 0) return null
165167
if (el.textContent === '\n') {
166168
return null
167169
}

test/fromRedactor.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,38 @@ describe('getNestedValueIfAvailable', () => {
270270
});
271271

272272
});
273+
describe("CS-41001", () =>{
274+
test("should not add fragment for text nodes having white spaces", () => {
275+
const dom = new JSDOM();
276+
const document = dom.window.document;
277+
const body = document.createElement("body");
278+
const td1 = document.createElement("td");
279+
const td2 = document.createElement("td");
280+
const tr = document.createElement("tr");
281+
const text = document.createTextNode(` `)
282+
td1.textContent = "Hello";
283+
td2.textContent = "World";
284+
tr.appendChild(td1);
285+
tr.append(text)
286+
tr.appendChild(td2);
287+
body.append(tr)
288+
const jsonValue = fromRedactor(body);
289+
expect(jsonValue).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"tr","attrs":{},"uid":"uid","children":[{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Hello"}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"World"}]}]}]})
290+
})
291+
test("should add fragment for text nodes between block nodes", () => {
292+
const dom = new JSDOM();
293+
const document = dom.window.document;
294+
const body = document.createElement("body");
295+
const p1 = document.createElement("p");
296+
const p2 = document.createElement("p");
297+
const text = document.createTextNode(` beautiful `)
298+
p1.textContent = "Hello";
299+
p2.textContent = "World";
300+
body.appendChild(p1);
301+
body.append(text)
302+
body.appendChild(p2);
303+
const jsonValue = fromRedactor(body);
304+
expect(jsonValue).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":"Hello"}]},{"type":"fragment","attrs":{},"uid":"uid","children":[{"text":" beautiful "}]},{"type":"p","attrs":{},"uid":"uid","children":[{"text":"World"}]}]})
305+
})
306+
})
307+

0 commit comments

Comments
 (0)