Skip to content

Commit 3a19496

Browse files
committed
fix: RT-285 add figcaption as a standard element tag
1 parent f8c9198 commit 3a19496

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/fromRedactor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const isInline = ['span', 'a', 'inlineCode', 'reference']
1515
const isVoid = ['img', 'embed']
1616

1717

18-
const ELEMENT_TAGS: IHtmlToJsonElementTags = {
18+
export const ELEMENT_TAGS: IHtmlToJsonElementTags = {
1919
A: (el: HTMLElement) => {
2020
const attrs: Record<string, string> = {}
2121
const target = el.getAttribute('target');
@@ -98,7 +98,8 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
9898
SCRIPT: (el: HTMLElement) => {
9999
return { type: 'script', attrs: {} }
100100
},
101-
HR: () => ({ type: 'hr', attrs: {} })
101+
HR: () => ({ type: 'hr', attrs: {} }),
102+
FIGCAPTION: () => ({ type: 'figcaption', attrs: {} }),
102103
}
103104

104105
const TEXT_TAGS: IHtmlToJsonTextTags = {

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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-nocheck
2-
import { fromRedactor, getNestedValueIfAvailable } from "../src/fromRedactor"
2+
import { ELEMENT_TAGS, fromRedactor, getNestedValueIfAvailable } from "../src/fromRedactor"
33
import { JSDOM } from "jsdom"
44
import isEqual from "lodash.isequal"
55
import omitdeep from "omit-deep-lodash"
@@ -295,6 +295,16 @@ describe("Testing html to json conversion", () => {
295295
const json = htmlToJson(html)
296296
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"reference","attrs":{"style":{"text-align":"right"},"redactor-attributes":{"src":"https://picsum.photos/200","height":"141","alt":"image_(9).png","caption":"ss","type":"asset","asset-alt":"image_(9).png","max-height":"141","max-width":"148","sys-style-type":"display","position":"right","captionAttrs":{"style":"text-align:center"},"anchorLink":"ss.com","target":true,"asset-caption":"ss"},"class-name":"embedded-asset","width":148,"type":"asset","asset-caption":"ss","link":"ss.com","asset-alt":"image_(9).png","target":"_blank","position":"right","asset-link":"https://picsum.photos/200","asset-uid":"blt137d845621ef8168","display-type":"display","asset-name":"image_(9).png","asset-type":"image/png","content-type-uid":"sys_assets"},"uid":"uid","children":[{"text":""}]},{"type":"p","attrs":{},"uid":"uid","children":[{"text":""}]}] })
297297
})
298+
test("should convert asset to reference with non standard tags", () => {
299+
const html = `<figure style="margin: 0; text-align: right">
300+
<div style="display: inline-block"><a href="ss.com" target="_blank"><img src="https://picsum.photos/200" height="141" alt="image_(9).png" caption="ss" anchorLink="ss.com" class="embedded-asset" content-type-uid="sys_assets" type="asset" asset-alt="image_(9).png" width="148" max-height="141" max-width="148" style="max-height: 141px; height: 141px; text-align: right; max-width: 148px; width: auto" data-sys-asset-filelink="https://picsum.photos/200" data-sys-asset-uid="blt137d845621ef8168" data-sys-asset-filename="image_(9).png" data-sys-asset-contenttype="image/png" data-sys-asset-caption="ss" data-sys-asset-alt="image_(9).png" data-sys-asset-link="ss.com" data-sys-asset-position="right" data-sys-asset-isnewtab="true" sys-style-type="display" /></a>
301+
<figcaption style="text-align:center">ss</figcaption>
302+
</div>
303+
</figure>
304+
<p></p>`
305+
const json = htmlToJson(html, { allowNonStandardTags: true })
306+
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"reference","attrs":{"style":{"text-align":"right"},"redactor-attributes":{"src":"https://picsum.photos/200","height":"141","alt":"image_(9).png","caption":"ss","type":"asset","asset-alt":"image_(9).png","max-height":"141","max-width":"148","sys-style-type":"display","position":"right","captionAttrs":{"style":"text-align:center"},"anchorLink":"ss.com","target":true,"asset-caption":"ss"},"class-name":"embedded-asset","width":148,"type":"asset","asset-caption":"ss","link":"ss.com","asset-alt":"image_(9).png","target":"_blank","position":"right","asset-link":"https://picsum.photos/200","asset-uid":"blt137d845621ef8168","display-type":"display","asset-name":"image_(9).png","asset-type":"image/png","content-type-uid":"sys_assets"},"uid":"uid","children":[{"text":""}]},{"type":"p","attrs":{},"uid":"uid","children":[{"text":""}]}] })
307+
})
298308
})
299309

300310

@@ -372,14 +382,16 @@ describe("CS-41001", () =>{
372382
})
373383
})
374384

375-
376-
377-
385+
describe("ELEMENT_TAGS", () => {
386+
test("should have FIGCAPTION as a standard element tag", () => {
387+
const standardElementTags = Object.keys(ELEMENT_TAGS);
388+
expect(standardElementTags).toContain("FIGCAPTION");
389+
})
390+
})
378391

379392
function htmlToJson (html: string, options: IHtmlToJsonOptions) {
380393
const dom = new JSDOM(html);
381394
let htmlDoc = dom.window.document.querySelector("body");
382395
return fromRedactor(htmlDoc, options);
383396

384-
}
385-
397+
}

0 commit comments

Comments
 (0)