Skip to content

Commit 21e0458

Browse files
committed
fix: ref asset _self attr added by default & test cases enhancements
1 parent dc66fc8 commit 21e0458

File tree

4 files changed

+28
-44
lines changed

4 files changed

+28
-44
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fromRedactor.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
437437
}
438438
}
439439
elementAttrs.attrs["redactor-attributes"] = redactor
440-
return jsx('element', { attrs: { ...elementAttrs?.attrs, type, "asset-caption": caption, "link": link, "asset-alt": alt, target, position, "asset-link": fileLink, "asset-uid": uid, "display-type": displayType, "asset-name": fileName, "asset-type": contentType, "content-type-uid": contentTypeUid }, type: "reference", uid: generateId() }, children)
440+
441+
const assetAttrs = { ...elementAttrs?.attrs, type, "asset-caption": caption, "link": link, "asset-alt": alt, target, position, "asset-link": fileLink, "asset-uid": uid, "display-type": displayType, "asset-name": fileName, "asset-type": contentType, "content-type-uid": contentTypeUid }
442+
443+
if(assetAttrs.target === "_self"){
444+
delete assetAttrs.target
445+
}
446+
447+
return jsx('element', { attrs: assetAttrs, type: "reference", uid: generateId() }, children)
441448
}
442449
}
443450
if (nodeName === 'FIGCAPTION') {

test/expectedJson.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,6 @@ export default {
13461346
"sys-style-type": "display"
13471347
},
13481348
"type": "asset",
1349-
"target": "_self",
13501349
"asset-link": "https://images.contentstack.io/v3/assets/bltbdb397c7cc18a214/blt9fedd0336c2f7f0d/61fe9fb699c8a84a577b9f40/crop_area.jpeg",
13511350
"asset-uid": "blt9fedd0336c2f7f0d",
13521351
"display-type": "display",

test/fromRedactor.test.ts

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const docWrapper = (children: any) => {
1414
}
1515
}
1616
const compareValue = (json1,json2) => {
17-
return isEqual(JSON.stringify(omitdeep(json1, "uid")), JSON.stringify(omitdeep(docWrapper(json2), "uid")))
17+
return expect(JSON.stringify(omitdeep(json1, "uid"))).toEqual(JSON.stringify(omitdeep(docWrapper(json2), "uid")));
1818
}
1919

2020
jest.mock('uuid', () => ({ v4: () => 'uid' }));
@@ -24,66 +24,62 @@ describe("Testing html to json conversion", () => {
2424
const dom = new JSDOM(html)
2525
let htmlDoc = dom.window.document.querySelector('body')
2626
let jsonValue = fromRedactor(htmlDoc)
27-
let testResult = isEqual(omitdeep(jsonValue, "uid"), docWrapper([{ "attrs": {}, "children": [{ "text": "This is test" }], "type": "p" }]))
28-
expect(testResult).toBe(true)
27+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(docWrapper([{ "attrs": {}, "children": [{ "text": "This is test" }], "type": "p" }]));
28+
2929
})
3030

3131
it("heading conversion", () => {
3232
let html = expectedValue[2].html
3333
const dom = new JSDOM(html)
3434
let htmlDoc = dom.window.document.querySelector('body')
3535
let jsonValue = fromRedactor(htmlDoc)
36-
let testResult = isEqual(omitdeep(jsonValue, "uid"), omitdeep(docWrapper(expectedValue[2].json), "uid"))
37-
expect(testResult).toBe(true)
36+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(omitdeep(docWrapper(expectedValue[2].json), "uid"));
37+
3838
})
3939
it("table conversion", () => {
4040
let html = expectedValue[3].html
4141
const dom = new JSDOM(html)
4242
let htmlDoc = dom.window.document.querySelector('body')
4343
let jsonValue = fromRedactor(htmlDoc)
44-
let testResult = isEqual(omitdeep(jsonValue, "uid"), omitdeep(docWrapper(expectedValue[3].json), "uid"))
45-
expect(testResult).toBe(true)
44+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(omitdeep(docWrapper(expectedValue[3].json), "uid"));
45+
4646
})
4747
it("basic formating, block and code conversion", () => {
4848
let html = expectedValue[4].html
4949
const dom = new JSDOM(html)
5050
let htmlDoc = dom.window.document.querySelector('body')
5151
let jsonValue = fromRedactor(htmlDoc)
52-
let testResult = isEqual(omitdeep(jsonValue, "uid"), omitdeep(docWrapper(expectedValue[4].json), "uid"))
53-
expect(testResult).toBe(true)
52+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(omitdeep(docWrapper(expectedValue[4].json), "uid"));
53+
5454
})
5555
it("List and alignment conversion", () => {
5656
let html = expectedValue[5].html
5757
const dom = new JSDOM(html)
5858
let htmlDoc = dom.window.document.querySelector('body')
5959
let jsonValue = fromRedactor(htmlDoc)
60-
let testResult = isEqual(omitdeep(jsonValue, "uid"), omitdeep(docWrapper(expectedValue[5].json), "uid"))
61-
expect(testResult).toBe(true)
60+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(omitdeep(docWrapper(expectedValue[5].json), "uid"));
6261
})
6362
it("Link ,divider and property conversion", () => {
6463
let html = expectedValue[7].html
6564
const dom = new JSDOM(html)
6665
let htmlDoc = dom.window.document.querySelector('body')
6766
let jsonValue = fromRedactor(htmlDoc)
68-
let testResult = isEqual(omitdeep(jsonValue, "uid"), omitdeep(docWrapper(expectedValue[7].json), "uid"))
69-
expect(testResult).toBe(true)
67+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(omitdeep(docWrapper(expectedValue[7].json), "uid"));
7068
})
7169

7270
it("Embedded entry as link", () => {
7371
let html = expectedValue[8].html
7472
const dom = new JSDOM(html)
7573
let htmlDoc = dom.window.document.querySelector('body')
7674
let jsonValue = fromRedactor(htmlDoc)
77-
let testResult = isEqual(JSON.stringify(omitdeep(jsonValue, "uid"), null, 2), JSON.stringify(omitdeep(docWrapper(expectedValue[8].json), "uid"), null, 2))
78-
expect(testResult).toBe(true)
75+
expect(JSON.stringify(omitdeep(jsonValue, "uid"), null, 2)).toStrictEqual(JSON.stringify(omitdeep(docWrapper(expectedValue[8].json), "uid"), null, 2));
7976
})
8077
it("Embedded entry as inline block", () => {
8178
let html = expectedValue[9].html
8279
const dom = new JSDOM(html)
8380
let htmlDoc = dom.window.document.querySelector('body')
8481
let jsonValue = fromRedactor(htmlDoc)
85-
let testResult = isEqual(omitdeep(jsonValue, "uid"), omitdeep(docWrapper(expectedValue[9].json), "uid"))
86-
expect(testResult).toBe(true)
82+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(omitdeep(docWrapper(expectedValue[9].json), "uid"));
8783
})
8884

8985
it("Embedded entry as block", () => {
@@ -92,8 +88,7 @@ describe("Testing html to json conversion", () => {
9288
let htmlDoc = dom.window.document.querySelector('body')
9389

9490
let jsonValue = fromRedactor(htmlDoc)
95-
let testResult = isEqual(omitdeep(jsonValue, "uid"), omitdeep(docWrapper(expectedValue[10].json), "uid"))
96-
expect(testResult).toBe(true)
91+
expect(omitdeep(jsonValue, "uid")).toStrictEqual(omitdeep(docWrapper(expectedValue[10].json), "uid"));
9792
})
9893

9994
it("Basic Scenarios", () => {
@@ -104,12 +99,7 @@ describe("Testing html to json conversion", () => {
10499
let htmlDoc = dom.window.document.querySelector('body')
105100
let jsonValue = fromRedactor(htmlDoc)
106101
//console.log(JSON.stringify(omitdeep(jsonValue.children, "uid"), null, 2))
107-
let testResult = compareValue(jsonValue,expectedValue[index].json)
108-
if(!testResult){
109-
//console.log(JSON.stringify(omitdeep(jsonValue, "uid")))
110-
//console.log(JSON.stringify(omitdeep(expectedValue[index].json, "uid")))
111-
}
112-
expect(testResult).toBe(true)
102+
compareValue(jsonValue,expectedValue[index].json)
113103
})
114104
})
115105
it("Custom ELEMENT_TAGS",()=>{
@@ -120,8 +110,7 @@ describe("Testing html to json conversion", () => {
120110
let htmlDoc = dom.window.document.querySelector('body')
121111
let jsonValue = fromRedactor(htmlDoc,{customElementTags:expectedValue[index].customElementTags})
122112
//console.log(JSON.stringify(omitdeep(jsonValue.children, "uid"), null, 2))
123-
let testResult = compareValue(jsonValue,expectedValue[index].json)
124-
expect(testResult).toBe(true)
113+
compareValue(jsonValue,expectedValue[index].json)
125114
})
126115
})
127116
it("Custom TEXT_TAGS",()=>{
@@ -132,8 +121,7 @@ describe("Testing html to json conversion", () => {
132121
let htmlDoc = dom.window.document.querySelector('body')
133122
let jsonValue = fromRedactor(htmlDoc,{customTextTags:expectedValue[index].customTextTags})
134123
//console.log(JSON.stringify(omitdeep(jsonValue.children, "uid"), null, 2))
135-
let testResult = compareValue(jsonValue,expectedValue[index].json)
136-
expect(testResult).toBe(true)
124+
compareValue(jsonValue,expectedValue[index].json)
137125
})
138126
})
139127
it("Conversion with allowNonStandardTags", () => {
@@ -145,12 +133,7 @@ describe("Testing html to json conversion", () => {
145133
const dom = new JSDOM(html)
146134
let htmlDoc = dom.window.document.querySelector('body')
147135
let jsonValue = fromRedactor(htmlDoc,{allowNonStandardTags:true,customTextTags:expectedValue[index].customTextTags})
148-
let testResult = compareValue(jsonValue,expectedValue[index].json)
149-
if(!testResult){
150-
//console.log(JSON.stringify(omitdeep(jsonValue, "uid")))
151-
//console.log(JSON.stringify(omitdeep(expectedValue[index].json, "uid")))
152-
}
153-
expect(testResult).toBe(true)
136+
compareValue(jsonValue,expectedValue[index].json)
154137
expect(mockFunction).toHaveBeenCalledTimes(expectedValue[index].nonStandardTags)
155138
})
156139
})
@@ -372,14 +355,9 @@ describe("CS-41001", () =>{
372355
})
373356
})
374357

375-
376-
377-
378-
379358
function htmlToJson (html: string, options: IHtmlToJsonOptions) {
380359
const dom = new JSDOM(html);
381360
let htmlDoc = dom.window.document.querySelector("body");
382361
return fromRedactor(htmlDoc, options);
383-
384362
}
385363

0 commit comments

Comments
 (0)