Skip to content

Commit 42bc798

Browse files
Merge pull request #31 from contentstack/EB-635-json-rte-to-support-video-tag
JSON RTE to support video tag
2 parents 3b1bb7c + dbebd15 commit 42bc798

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/fromRedactor.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
7070
DIV: (el: HTMLElement) => {
7171
return { type: 'div', attrs: {} }
7272
},
73+
VIDEO: (el: HTMLElement) => {
74+
const srcArray = Array.from(el.querySelectorAll("source")).map((source) =>
75+
source.getAttribute("src")
76+
);
77+
78+
return {
79+
type: 'embed',
80+
attrs: {
81+
src: srcArray.length > 0 ? srcArray[0] : null,
82+
},
83+
}
84+
},
7385
STYLE: (el: HTMLElement) => {
7486
return { type: 'style', attrs: { "style-text": el.textContent } }
7587
},
@@ -403,6 +415,9 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
403415
if (nodeName === 'FIGCAPTION') {
404416
return null
405417
}
418+
if (nodeName === 'SOURCE') {
419+
return null;
420+
}
406421
if (nodeName === 'DIV') {
407422
const dataType = el.attributes['data-type']?.value
408423
if (dataType === 'row') {
@@ -610,7 +625,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
610625
return jsx('element', elementAttrs, [{ text: '' }])
611626
}
612627
}
613-
if (nodeName === 'IMG' || nodeName === 'IFRAME') {
628+
if (nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO') {
614629
if (elementAttrs?.attrs?.["redactor-attributes"]?.width) {
615630
let width = elementAttrs.attrs["redactor-attributes"].width
616631
if (width.slice(width.length - 1) === '%') {

test/expectedJson.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,15 @@ export default {
17561756
html : `<hangout-module><hangout-chat from="Paul, Addy" nested-json='{"to":"Hello World","more-nesting":{"from":"Beautiful World"}}'><hangout-discussion><hangout-message from="Paul" profile="profile.png" datetime="2013-07-17T12:02"><p>Feelin' this Web Components thing.</p><p>Heard of it?</p></hangout-message></hangout-discussion></hangout-chat><hangout-chat>Hi There!</hangout-chat></hangout-module>`
17571757
},
17581758
],
1759+
"video-tag": [
1760+
{
1761+
json: {"type":"doc","uid":"cfd06695ff85459c90f2d2d4da01af10","attrs":{},"children":[{"type":"embed","attrs":{"src":"https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4"},"uid":"2c144d07b3a14d8f98c78125b964edcb","children":[{"text":""}]}]},
1762+
html: `<video><source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4" /></video>`},
1763+
{
1764+
json: {"type":"doc","uid":"00459467e3184fdcab0ba1819f0e3645","attrs":{},"children":[{"type":"embed","attrs":{"src":"https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm","style":{},"redactor-attributes":{"controls":"","width":"250"},"width":250},"uid":"83fb5d7ce52c4f78872d33478bb12f2f","children":[{"text":""}]}]},
1765+
html: `<video controls width="250"><source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" type="video/webm" /><source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4" /></video>`,
1766+
}
1767+
],
17591768
'table-rowspan-colspan': {
17601769
html: `<p></p>
17611770
<table style="text-align: center;">

test/fromRedactor.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ describe("CS-41001", () =>{
303303
const jsonValue = fromRedactor(body);
304304
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"}]}]})
305305
})
306+
test("should convert video tag into embed", () => {
307+
expectedValue['video-tag'].forEach((testCase) => {
308+
const dom = new JSDOM(testCase.html);
309+
let htmlDoc = dom.window.document.querySelector("body");
310+
const jsonValue = fromRedactor(htmlDoc);
311+
expect(omitdeep(jsonValue, "uid")).toStrictEqual( omitdeep(testCase.json, "uid"))
312+
})
313+
})
306314

307315
test('table JSON should have proper structure with rowspan and colspan', () => {
308316
const testCases = ['table-rowspan-colspan', 'table-rowspan-colspan-2', 'table-rowspan-colspan-3']

0 commit comments

Comments
 (0)