Skip to content

Commit

Permalink
Move isUrlYoutubeVideo function into Helpers class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 9, 2024
1 parent f8fa6af commit 2934162
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
10 changes: 10 additions & 0 deletions packages/survey-core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ export class Helpers {
}
return len1 === len2 ? 0 : (len1 < len2 ? -1 : 1);
}
public static isUrlYoutubeVideo(url: string): boolean {
if (!url) return false;
const youtubeDomains = ["www.youtube.com", "m.youtube.com", "youtube.com", "youtu.be"];
url = url.toLowerCase();
url = url.replace(/^https?:\/\//, "");
for (let i = 0; i < youtubeDomains.length; i++) {
if (url.indexOf(youtubeDomains[i] + "/") === 0) return true;
}
return false;
}
}
if (!(<any>String.prototype)["format"]) {
(<any>String.prototype)["format"] = function() {
Expand Down
17 changes: 3 additions & 14 deletions packages/survey-core/src/question_image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@ import { QuestionFactory } from "./questionfactory";
import { LocalizableString } from "./localizablestring";
import { CssClassBuilder } from "./utils/cssClassBuilder";
import { getRenderedStyleSize, getRenderedSize } from "./utils/utils";
import { Helpers } from "./helpers";

const youtubeDomains = ["www.youtube.com", "m.youtube.com", "youtube.com", "youtu.be"];
const videoSuffics = [".mp4", ".mov", ".wmv", ".flv", ".avi", ".mkv"];
const youtubeUrl = "https://www.youtube.com/";
const youtubeEmbed = "embed";

function isUrlYoutubeVideo(url: string): boolean {
if (!url) return false;
url = url.toLowerCase();
url = url.replace(/^https?:\/\//, "");
for (let i = 0; i < youtubeDomains.length; i++) {
if (url.indexOf(youtubeDomains[i] + "/") === 0) return true;
}
return false;
}

/**
* A class that describes the Image question type. Unlike other question types, Image cannot have a title or value.
*
Expand Down Expand Up @@ -191,7 +181,7 @@ export class QuestionImageModel extends QuestionNonValue {
}
}
private isYoutubeVideo(): boolean {
return isUrlYoutubeVideo(this.imageLink);
return Helpers.isUrlYoutubeVideo(this.imageLink);
}
private isVideo(): boolean {
let link = this.imageLink;
Expand All @@ -205,8 +195,7 @@ export class QuestionImageModel extends QuestionNonValue {
}

function getCorrectImageLink(val: string, isYouTube: boolean): string {
if (!val || !isUrlYoutubeVideo(val)) return isYouTube ? "" : val;
//if(!val || !isUrlYoutubeVideo(val)) return val;
if (!val || !Helpers.isUrlYoutubeVideo(val)) return isYouTube ? "" : val;
let res = val.toLocaleLowerCase();
if(res.indexOf(youtubeEmbed) > -1) return val;
let id = "";
Expand Down

0 comments on commit 2934162

Please sign in to comment.