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

✨ Serialize current video frame into a poster on the <video> #794

Merged
merged 5 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = config => config.set({
// local package files
{ pattern: 'src/index.js', watched: false },
{ pattern: 'test/helpers.js', watched: false },
{ pattern: 'test/**/*.test.js', watched: false }
{ pattern: 'test/**/*.test.js', watched: false },
{ pattern: 'test/assets/**', watched: false, included: false }
],

proxies: {
Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/prepare-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function uid() {

// Marks elements that are to be serialized later with a data attribute.
export function prepareDOM(dom) {
for (let elem of dom.querySelectorAll('input, textarea, select, iframe, canvas')) {
for (let elem of dom.querySelectorAll('input, textarea, select, iframe, canvas, video')) {
if (!elem.getAttribute('data-percy-element-id')) {
elem.setAttribute('data-percy-element-id', uid());
}
Expand Down
2 changes: 2 additions & 0 deletions packages/dom/src/serialize-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import serializeInputs from './serialize-inputs';
import serializeFrames from './serialize-frames';
import serializeCSSOM from './serialize-cssom';
import serializeCanvas from './serialize-canvas';
import serializeVideos from './serialize-video';

// Returns a copy or new doctype for a document.
function doctype(dom) {
Expand Down Expand Up @@ -34,6 +35,7 @@ export function serializeDOM(options) {
let clone = dom.cloneNode(true);
serializeInputs(dom, clone);
serializeFrames(dom, clone, { enableJavaScript });
serializeVideos(dom, clone);

if (!enableJavaScript) {
serializeCSSOM(dom, clone);
Expand Down
22 changes: 22 additions & 0 deletions packages/dom/src/serialize-video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Captures the current frame of videos and sets the poster image
export function serializeVideos(dom, clone) {
for (let video of dom.querySelectorAll('video')) {
// If the video already has a poster image, no work for us to do
if (video.getAttribute('poster')) continue;

let videoId = video.getAttribute('data-percy-element-id');
let cloneEl = clone.querySelector(`[data-percy-element-id="${videoId}"]`);
let canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;

canvas.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
let dataUrl = canvas.toDataURL();
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved
// If the canvas produces a blank image, skip
if (!dataUrl || dataUrl === 'data:,') continue;

cloneEl.setAttribute('poster', dataUrl);
}
}

export default serializeVideos;
Binary file added packages/dom/test/assets/example.webm
Binary file not shown.
45 changes: 45 additions & 0 deletions packages/dom/test/serialize-videos.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { withExample, parseDOM } from './helpers';
import serializeDOM from '@percy/dom';

describe('serializeVideos', () => {
let $;

it('serializes video elements', async () => {
withExample(`
<video src="base/test/assets/example.webm" id="video" controls />
`);

let $video = window.video;
await new Promise(r => {
if ($video.readyState > 2) r();
else $video.addEventListener('canplay', r);
});
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved

$ = parseDOM(serializeDOM());
expect($('#video')[0].getAttribute('poster').length > 25).toBe(true);
});

it('does not serialize videos with an existing poster', async () => {
withExample(`
<video src="base/test/assets/example.webm" id="video" poster="//:0" />
`);

let $video = window.video;
await new Promise(r => {
if ($video.readyState > 2) r();
else $video.addEventListener('canplay', r);
});
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved

$ = parseDOM(serializeDOM());
expect($('#video')[0].getAttribute('poster')).toBe('//:0');
});

it('does not apply blank poster images', () => {
withExample(`
<video src="//:0" id="video" />
`);

$ = parseDOM(serializeDOM());
expect($('#video')[0].getAttribute('poster')).toBe(null);
});
});