|
@@ -3,7 +3,6 @@
|
|
|
* @license Mozilla Public License (MPLv2)
|
|
|
* @description This is the DOM/HTML utilities module.
|
|
|
*/
|
|
|
-import URI from 'urijs';
|
|
|
import isFunction from 'lodash-es/isFunction';
|
|
|
import log from '@converse/headless/log';
|
|
|
import tpl_audio from 'templates/audio.js';
|
|
@@ -19,7 +18,8 @@ import tpl_form_username from '../templates/form_username.js';
|
|
|
import tpl_hyperlink from 'templates/hyperlink.js';
|
|
|
import tpl_video from 'templates/video.js';
|
|
|
import u from '../headless/utils/core';
|
|
|
-import { api, converse } from '@converse/headless/core';
|
|
|
+import { converse } from '@converse/headless/core';
|
|
|
+import { getURI, isAudioURL, isImageURL, isVideoURL } from '@converse/headless/utils/url.js';
|
|
|
import { render } from 'lit';
|
|
|
|
|
|
const { sizzle } = converse.env;
|
|
@@ -52,99 +52,6 @@ function slideOutWrapup (el) {
|
|
|
el.style.height = '';
|
|
|
}
|
|
|
|
|
|
-export function getURI (url) {
|
|
|
- try {
|
|
|
- return url instanceof URI ? url : new URI(url);
|
|
|
- } catch (error) {
|
|
|
- log.debug(error);
|
|
|
- return null;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function checkTLS (uri) {
|
|
|
- return (
|
|
|
- window.location.protocol === 'http:' ||
|
|
|
- (window.location.protocol === 'https:' && uri.protocol().toLowerCase() === 'https')
|
|
|
- );
|
|
|
-}
|
|
|
-
|
|
|
-function checkFileTypes (types, url) {
|
|
|
- const uri = getURI(url);
|
|
|
- if (uri === null || !checkTLS(uri)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- const filename = uri.filename().toLowerCase();
|
|
|
- return !!types.filter(ext => filename.endsWith(ext)).length;
|
|
|
-}
|
|
|
-
|
|
|
-export function isURLWithImageExtension (url) {
|
|
|
- return checkFileTypes(['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.svg'], url);
|
|
|
-}
|
|
|
-
|
|
|
-export function isAudioURL (url) {
|
|
|
- return checkFileTypes(['.ogg', '.mp3', '.m4a'], url);
|
|
|
-}
|
|
|
-
|
|
|
-export function isVideoURL (url) {
|
|
|
- return checkFileTypes(['.mp4', '.webm'], url);
|
|
|
-}
|
|
|
-
|
|
|
-export function isEncryptedFileURL (url) {
|
|
|
- return url.startsWith('aesgcm://');
|
|
|
-}
|
|
|
-
|
|
|
-export function isImageURL (url) {
|
|
|
- const regex = api.settings.get('image_urls_regex');
|
|
|
- return regex?.test(url) || isURLWithImageExtension(url);
|
|
|
-}
|
|
|
-
|
|
|
-function isDomainAllowed (whitelist, url) {
|
|
|
- const uri = getURI(url);
|
|
|
- const subdomain = uri.subdomain();
|
|
|
- const domain = uri.domain();
|
|
|
- const fulldomain = `${subdomain ? `${subdomain}.` : ''}${domain}`;
|
|
|
- return whitelist.includes(domain) || whitelist.includes(fulldomain);
|
|
|
-}
|
|
|
-
|
|
|
-export function isAudioDomainAllowed (url) {
|
|
|
- const embed_audio = api.settings.get('embed_audio');
|
|
|
- if (!Array.isArray(embed_audio)) {
|
|
|
- return embed_audio;
|
|
|
- }
|
|
|
- try {
|
|
|
- return isDomainAllowed(embed_audio, url);
|
|
|
- } catch (error) {
|
|
|
- log.debug(error);
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export function isVideoDomainAllowed (url) {
|
|
|
- const embed_videos = api.settings.get('embed_videos');
|
|
|
- if (!Array.isArray(embed_videos)) {
|
|
|
- return embed_videos;
|
|
|
- }
|
|
|
- try {
|
|
|
- return isDomainAllowed(embed_videos, url);
|
|
|
- } catch (error) {
|
|
|
- log.debug(error);
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export function isImageDomainAllowed (url) {
|
|
|
- const show_images_inline = api.settings.get('show_images_inline');
|
|
|
- if (!Array.isArray(show_images_inline)) {
|
|
|
- return show_images_inline;
|
|
|
- }
|
|
|
- try {
|
|
|
- return isDomainAllowed(show_images_inline, url);
|
|
|
- } catch (error) {
|
|
|
- log.debug(error);
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
function getFileName (uri) {
|
|
|
try {
|
|
|
return decodeURI(uri.filename());
|
|
@@ -166,11 +73,11 @@ export function getOOBURLMarkup (url) {
|
|
|
if (uri === null) {
|
|
|
return url;
|
|
|
}
|
|
|
- if (u.isVideoURL(uri)) {
|
|
|
+ if (isVideoURL(uri)) {
|
|
|
return tpl_video(url);
|
|
|
- } else if (u.isAudioURL(uri)) {
|
|
|
+ } else if (isAudioURL(uri)) {
|
|
|
return tpl_audio(url);
|
|
|
- } else if (u.isImageURL(uri)) {
|
|
|
+ } else if (isImageURL(uri)) {
|
|
|
return tpl_file(uri.toString(), getFileName(uri));
|
|
|
} else {
|
|
|
return tpl_file(uri.toString(), getFileName(uri));
|
|
@@ -340,13 +247,6 @@ export function getHyperlinkTemplate (url) {
|
|
|
return url;
|
|
|
}
|
|
|
|
|
|
-export function filterQueryParamsFromURL (url) {
|
|
|
- const paramsArray = api.settings.get('filter_url_query_params');
|
|
|
- if (!paramsArray) return url;
|
|
|
- const parsed_uri = getURI(url);
|
|
|
- return parsed_uri.removeQuery(paramsArray).toString();
|
|
|
-}
|
|
|
-
|
|
|
u.slideInAllElements = function (elements, duration = 300) {
|
|
|
return Promise.all(Array.from(elements).map(e => u.slideIn(e, duration)));
|
|
|
};
|
|
@@ -591,15 +491,6 @@ u.xForm2TemplateResult = function (field, stanza, options) {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-Object.assign(u, {
|
|
|
- filterQueryParamsFromURL,
|
|
|
- getURI,
|
|
|
- isAudioURL,
|
|
|
- isImageURL,
|
|
|
- isImageDomainAllowed,
|
|
|
- isURLWithImageExtension,
|
|
|
- isVideoURL,
|
|
|
- getOOBURLMarkup,
|
|
|
-});
|
|
|
+Object.assign(u, { getOOBURLMarkup });
|
|
|
|
|
|
export default u;
|