Skip to content

Commit 9e94c9b

Browse files
committed
Merge branch 'ACP2E-485' of https://github.com/magento-l3/magento2-page-builder into L3-PR-2022-04-19
2 parents 236621c + 2c8f14c commit 9e94c9b

File tree

3 files changed

+185
-3
lines changed
  • app/code/Magento/PageBuilder/view/adminhtml/web
  • dev/tests/js/jasmine/tests/app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/video/converter/attribute

3 files changed

+185
-3
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/video/converter/attribute/videosrc.js

Lines changed: 53 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/video/converter/attribute/videosrc.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,68 @@ import {DataObject} from "../../../../data-store";
88
import {get} from "../../../../utils/object";
99

1010
export default class VideoSrc implements ConverterInterface {
11+
12+
/**
13+
* Parse YouTube parameters from given URL and Autoplay setting from UI
14+
*
15+
* @param url string
16+
* @param data DataObject
17+
* @returns string
18+
* @private
19+
*/
20+
private static parseYoutubeGetParams(url: string, data: DataObject): string {
21+
const acceptableYouTubeParams = [
22+
"rel",
23+
"controls",
24+
"autoplay",
25+
"mute",
26+
"loop",
27+
"playlist",
28+
"cc_lang_pref",
29+
"cc_load_policy",
30+
"color",
31+
"disablekb",
32+
"end",
33+
"fs",
34+
"hl",
35+
"iv_load_policy",
36+
"modestbranding",
37+
"start",
38+
];
39+
40+
const a = document.createElement("a");
41+
a.href = url;
42+
const urlGetParams: {[key: string]: string} = {};
43+
a.search.slice(a.search.indexOf("?") + 1).split("&").map((hash) => {
44+
const [key, val] = hash.split("=");
45+
urlGetParams[key] = decodeURIComponent(val);
46+
});
47+
48+
const filteredGetParams: {[key: string]: string} = {};
49+
for (const param of acceptableYouTubeParams) {
50+
if (urlGetParams.hasOwnProperty(param)) {
51+
filteredGetParams[param] = urlGetParams[param];
52+
}
53+
}
54+
55+
if (data.autoplay === "true") {
56+
filteredGetParams.autoplay = "1";
57+
filteredGetParams.mute = "1";
58+
} else {
59+
delete filteredGetParams.autoplay;
60+
delete filteredGetParams.mute;
61+
}
62+
63+
const processedGetParams = [];
64+
for (const param in filteredGetParams) {
65+
if (filteredGetParams.hasOwnProperty(param)) {
66+
processedGetParams.push(encodeURI(param + "=" + filteredGetParams[param]));
67+
}
68+
}
69+
70+
return processedGetParams.length > 0 ? "?" + processedGetParams.join("&") : "";
71+
}
72+
1173
/**
1274
* Convert value to internal format
1375
*
@@ -16,6 +78,7 @@ export default class VideoSrc implements ConverterInterface {
1678
*/
1779
public fromDom(value: string): string | object {
1880
value = value.replace(/\?autoplay=1&mute=1/g, "");
81+
value = value.replace(/&autoplay=1&mute=1/g, "");
1982
value = value.replace(/\?title=0&byline=0&portrait=0/g, "");
2083
value = value.replace(/&autoplay=1&autopause=0&muted=1/g, "");
2184
return value;
@@ -41,7 +104,7 @@ export default class VideoSrc implements ConverterInterface {
41104

42105
if (youtubeRegExp.test(value)) {
43106
return "https://www.youtube.com/embed/" + youtubeRegExp.exec(value)[1] +
44-
(data.autoplay === "true" ? "?autoplay=1&mute=1" : "");
107+
VideoSrc.parseYoutubeGetParams(value, data);
45108
} else if (vimeoRegExp.test(value)) {
46109
return "https://player.vimeo.com/video/" + vimeoRegExp.exec(value)[3] +
47110
"?title=0&byline=0&portrait=0" + (data.autoplay === "true" ? "&autoplay=1&autopause=0&muted=1" : "");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint-disable max-nested-callbacks */
7+
define([
8+
'jquery',
9+
'Magento_PageBuilder/js/content-type/video/converter/attribute/videosrc'
10+
], function ($, VideoSrc) {
11+
'use strict';
12+
13+
describe('Magento_PageBuilder/js/content-type/video/converter/attribute/videosrc', function () {
14+
var model;
15+
16+
beforeEach(function () {
17+
model = new VideoSrc();
18+
});
19+
20+
describe('toDom', function () {
21+
it('Should remove non-youtube and preserve youtube tags in URL', function () {
22+
var data = {
23+
video_source: 'https://www.youtube.com/embed/ishbTyLs6ps?rel=1&non-youtube-tag=value',
24+
autoplay: 'false'
25+
},
26+
result = model.toDom('video_source', data);
27+
28+
expect(result).toBe('https://www.youtube.com/embed/ishbTyLs6ps?rel=1');
29+
});
30+
});
31+
32+
describe('toDom', function () {
33+
it('Should add autoplay and mute to URL if Autoplay enabled in UI', function () {
34+
var data = {
35+
video_source: 'https://www.youtube.com/embed/ishbTyLs6ps?rel=1&controls=0',
36+
autoplay: 'true'
37+
},
38+
result = model.toDom('video_source', data);
39+
40+
expect(result).toBe('https://www.youtube.com/embed/ishbTyLs6ps?rel=1&controls=0&autoplay=1&mute=1');
41+
});
42+
});
43+
44+
describe('toDom', function () {
45+
it('Should remove autoplay and mute from URL if Autoplay disabled in UI', function () {
46+
var data = {
47+
video_source: 'https://www.youtube.com/embed/ishbTyLs6ps?rel=1&controls=0&autoplay=1&mute=1',
48+
autoplay: 'false'
49+
},
50+
result = model.toDom('video_source', data);
51+
52+
expect(result).toBe('https://www.youtube.com/embed/ishbTyLs6ps?rel=1&controls=0');
53+
});
54+
});
55+
56+
describe('fromDom', function () {
57+
it('Should remove autoplay and mute from the URL as it is controlled via UI', function () {
58+
var result;
59+
60+
result = model.fromDom('https://www.youtube.com/embed/ishbTyLs6ps?rel=1&controls=0&autoplay=1&mute=1');
61+
expect(result).toBe('https://www.youtube.com/embed/ishbTyLs6ps?rel=1&controls=0');
62+
63+
result = model.fromDom('https://www.youtube.com/embed/ishbTyLs6ps?autoplay=1&mute=1');
64+
expect(result).toBe('https://www.youtube.com/embed/ishbTyLs6ps');
65+
});
66+
});
67+
});
68+
});

0 commit comments

Comments
 (0)