Skip to content

Commit 45fda17

Browse files
committed
test: RT-258 non standard element as inline in inline is true
1 parent bfc6736 commit 45fda17

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/fromRedactor.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const traverseChildAndModifyChild = (element: any, attrsForChild: any) => {
144144
Array.from(element.children || []).map((el) => traverseChildAndModifyChild(el, attrsForChild)).flat()
145145
return
146146
}
147-
const traverseChildAndWarpChild = (children: Array<Object>) => {
147+
const traverseChildAndWarpChild = (children: Array<Object>, allowNonStandardTags: boolean = false) => {
148148
let inlineElementIndex: Array<number> = []
149149
let hasBlockElement = false
150150
let childrenCopy = cloneDeep(children)
@@ -165,7 +165,7 @@ const traverseChildAndWarpChild = (children: Array<Object>) => {
165165
inlineElementIndex.push(index)
166166
}
167167
}
168-
else if (child.attrs.inline) {
168+
else if (allowNonStandardTags && child?.attrs?.inline) {
169169
inlineElementIndex.push(index)
170170
} else {
171171
hasBlockElement = true
@@ -282,7 +282,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
282282
}
283283
let children: any = flatten(Array.from(parent.childNodes).map((child) => fromRedactor(child, options)))
284284
children = children.filter((child: any) => child !== null)
285-
children = traverseChildAndWarpChild(children)
285+
children = traverseChildAndWarpChild(children, options?.allowNonStandardTags)
286286
if (children.length === 0) {
287287
children = [{ text: '' }]
288288
}
@@ -825,7 +825,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
825825
}
826826
let noOfInlineElement = 0
827827
Array.from(el.parentNode?.childNodes || []).forEach((child: any) => {
828-
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || child.getAttribute('inline')) {
828+
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline'))) {
829829
noOfInlineElement += 1
830830
}
831831
})

test/fromRedactor.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { JSDOM } from "jsdom"
44
import isEqual from "lodash.isequal"
55
import omitdeep from "omit-deep-lodash"
66
import expectedValue from "./expectedJson"
7+
import { IHtmlToJsonOptions } from "../src/types"
78

89
const docWrapper = (children: any) => {
910
return {
@@ -241,7 +242,7 @@ describe("Testing html to json conversion", () => {
241242

242243
test("should not convert stringify attrs when `allowNonStandardTags` is not true", () => {
243244
const html = `<p><span from="Paul, Addy" to="[object Object]">Hi There!</span></p>`;
244-
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"};
245+
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"};
245246

246247
const dom = new JSDOM(html);
247248
let htmlDoc = dom.window.document.querySelector("body");
@@ -250,6 +251,38 @@ describe("Testing html to json conversion", () => {
250251
});
251252
})
252253

254+
describe("SPAN", () => {
255+
256+
test("should properly convert inline properties id and class to json", () => {
257+
let html =`<p dir="ltr">Hello <span class="class" id="id">World</span></p>`
258+
const json = htmlToJson(html)
259+
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"}]}]})
260+
})
261+
262+
test("should skip span if other element are inline and it does not have any attributes", () => {
263+
let html =`<p dir="ltr">Hello <span>World</span></p>`
264+
const json = htmlToJson(html)
265+
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{"style":{},"redactor-attributes":{"dir":"ltr"}},"uid":"uid","children":[{"text":"Hello "},{"text":"World"}]}]})
266+
})
267+
268+
test("should not skip span if other element are inline and it does have any attribute", () => {
269+
let html =`<p dir="ltr">Hello <span data-test="test">World</span></p>`
270+
const json = htmlToJson(html)
271+
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"}]}]}]})
272+
})
273+
274+
test("should consider the non standard elements as inline if it has attribute of inline with the span tag", () => {
275+
let html = `<p><unknown inline="true"></unknown>Being an absolute <span>tropical</span> stunner</p>`
276+
let jsonValue = htmlToJson(html, { allowNonStandardTags: true })
277+
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"}]}] })
278+
})
279+
})
280+
281+
test("should consider the non standard elements as inline if it has attribute of inline", () => {
282+
let html = `<p><unknown inline="true"></unknown>Being an absolute <a href="https://chess.com">tropical</a> stunner</p>`
283+
let jsonValue = htmlToJson(html, { allowNonStandardTags: true })
284+
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"}]}] })
285+
})
253286
})
254287

255288

@@ -327,7 +360,7 @@ describe("CS-41001", () =>{
327360
})
328361
})
329362

330-
function htmlToJson (html, options) {
363+
function htmlToJson (html: string, options: IHtmlToJsonOptions) {
331364
const dom = new JSDOM(html);
332365
let htmlDoc = dom.window.document.querySelector("body");
333366
return fromRedactor(htmlDoc, options);

0 commit comments

Comments
 (0)