From 44d92d4de0c2d1674de3f325ba37a24aa6c86880 Mon Sep 17 00:00:00 2001 From: Adam Gruber Date: Thu, 17 Mar 2022 16:07:54 -0400 Subject: [PATCH 1/2] feat: support video context as base64 data uri --- CHANGELOG.md | 2 + src/client/components/test/context.js | 42 ++++++++++------ test/spec/components/test/context.test.js | 61 +++++++++++++++++++++-- 3 files changed, 85 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5146944e..f9f3cc92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # mochawesome-report-generator changelog ## [Unreleased] +### Added +- Support adding video in context via data uri [mochawesome #372](https://github.com/adamgruber/mochawesome/issues/372) ## [6.1.1] - 2022-03-05 ### Fixed diff --git a/src/client/components/test/context.js b/src/client/components/test/context.js index e64567d5..4d14d819 100644 --- a/src/client/components/test/context.js +++ b/src/client/components/test/context.js @@ -7,20 +7,26 @@ import styles from './test.css'; const cx = classNames.bind(styles); -const videoRegEx = /(?:mp4|webm)$/i; +const videoRegEx = /(mp4|webm)$/i; const imgRegEx = /(?:png|jpe?g|gif)$/i; const protocolRegEx = /^(?:(?:https?|ftp):\/\/)/i; const urlRegEx = /^(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/; // eslint-disable-line const base64ImgRegEx = /^data:image\/([a-zA-Z0-9-_.])+;base64,([^"]*)$/i; +const base64VidRegEx = /^data:video\/(mp4|webm);base64,(?:[^"]*)$/i; -const isVideo = str => { +const getVideoType = str => { if (!isString(str)) { - return false; + return undefined; } const hashIndex = str.indexOf('#'); - return videoRegEx.test(hashIndex > 0 ? str.slice(0, hashIndex) : str); -} + + const [, type] = + videoRegEx.exec(hashIndex > 0 ? str.slice(0, hashIndex) : str) || + base64VidRegEx.exec(str) || + []; + return type || undefined; +}; class TestContext extends Component { static displayName = 'TestContext'; @@ -34,22 +40,25 @@ class TestContext extends Component { ]), }; - renderVideo = (ctx, title) => { + renderVideo = (ctx, title, type) => { const isUrl = urlRegEx.test(ctx); const hasProtocol = protocolRegEx.test(ctx); const linkUrl = isUrl && !hasProtocol ? `http://${ctx}` : ctx; return ( -