Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON RTE to support video tag #31

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/fromRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
DIV: (el: HTMLElement) => {
return { type: 'div', attrs: {} }
},
VIDEO: (el: HTMLElement) => {
const srcArray = Array.from(el.querySelectorAll("source")).map((source) =>
source.getAttribute("src")
);

return {
type: 'embed',
attrs: {
src: srcArray.length > 0 ? srcArray[0] : null,
},
}
},
STYLE: (el: HTMLElement) => {
return { type: 'style', attrs: { "style-text": el.textContent } }
},
Expand Down Expand Up @@ -403,6 +415,9 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
if (nodeName === 'FIGCAPTION') {
return null
}
if (nodeName === 'SOURCE') {
return null;
}
if (nodeName === 'DIV') {
const dataType = el.attributes['data-type']?.value
if (dataType === 'row') {
Expand Down Expand Up @@ -610,7 +625,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
return jsx('element', elementAttrs, [{ text: '' }])
}
}
if (nodeName === 'IMG' || nodeName === 'IFRAME') {
if (nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO') {
if (elementAttrs?.attrs?.["redactor-attributes"]?.width) {
let width = elementAttrs.attrs["redactor-attributes"].width
if (width.slice(width.length - 1) === '%') {
Expand Down
9 changes: 9 additions & 0 deletions test/expectedJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,15 @@ export default {
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>`
},
],
"video-tag": [
{
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":""}]}]},
html: `<video><source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4" /></video>`},
{
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":""}]}]},
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>`,
}
],
'table-rowspan-colspan': {
html: `<p></p>
<table style="text-align: center;">
Expand Down
8 changes: 8 additions & 0 deletions test/fromRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ describe("CS-41001", () =>{
const jsonValue = fromRedactor(body);
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"}]}]})
})
test("should convert video tag into embed", () => {
expectedValue['video-tag'].forEach((testCase) => {
const dom = new JSDOM(testCase.html);
let htmlDoc = dom.window.document.querySelector("body");
const jsonValue = fromRedactor(htmlDoc);
expect(omitdeep(jsonValue, "uid")).toStrictEqual( omitdeep(testCase.json, "uid"))
})
})

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