mirror of
https://git.jami.net/savoirfairelinux/jami-client-qt.git
synced 2025-08-05 23:35:50 +02:00
linkify: update and do not add un-necessary http scheme
Change-Id: I7bd12dbc3bf512b3cd880892d2afda17c8117bd6 GitLab: #862
This commit is contained in:
parent
b92119b4b8
commit
cd5bdbb211
3 changed files with 3407 additions and 1194 deletions
|
@ -10,8 +10,9 @@ function getPreviewInfo(messageId, url) {
|
||||||
var title = null
|
var title = null
|
||||||
var description = null
|
var description = null
|
||||||
var image = null
|
var image = null
|
||||||
if (!url.includes("http://") && !url.includes("https://")) {
|
var u = new URL(url)
|
||||||
url = "http://".concat(url)
|
if (u.protocol === '') {
|
||||||
|
url = "https://".concat(url)
|
||||||
}
|
}
|
||||||
var domain = (new URL(url))
|
var domain = (new URL(url))
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
|
|
|
@ -1,118 +1,100 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2016 SoapBox Innovations Inc.
|
* Copyright (c) 2021 SoapBox Innovations Inc.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
var linkifyStr = (function (linkifyjs) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
'use strict';
|
/**
|
||||||
|
Convert strings of text into linkable HTML text
|
||||||
|
*/
|
||||||
|
|
||||||
;(function (window, linkify) {
|
function escapeText(text) {
|
||||||
var linkifyString = function (linkify) {
|
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||||
'use strict';
|
}
|
||||||
|
|
||||||
/**
|
function escapeAttr(href) {
|
||||||
Convert strings of text into linkable HTML text
|
return href.replace(/"/g, '"');
|
||||||
*/
|
}
|
||||||
|
|
||||||
var tokenize = linkify.tokenize;
|
function attributesToString(attributes) {
|
||||||
var options = linkify.options;
|
var result = [];
|
||||||
var Options = options.Options;
|
|
||||||
|
for (var attr in attributes) {
|
||||||
|
var val = attributes[attr] + '';
|
||||||
|
result.push(attr + "=\"" + escapeAttr(val) + "\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
function defaultRender(_ref) {
|
||||||
|
var tagName = _ref.tagName,
|
||||||
|
attributes = _ref.attributes,
|
||||||
|
content = _ref.content;
|
||||||
|
return "<" + tagName + " " + attributesToString(attributes) + ">" + escapeText(content) + "</" + tagName + ">";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Convert a plan text string to an HTML string with links. Expects that the
|
||||||
|
* given strings does not contain any HTML entities. Use the linkify-html
|
||||||
|
* interface if you need to parse HTML entities.
|
||||||
|
*
|
||||||
|
* @param {string} str string to linkify
|
||||||
|
* @param {import('linkifyjs').Opts} [opts] overridable options
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
function escapeText(text) {
|
function linkifyStr(str, opts) {
|
||||||
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
if (opts === void 0) {
|
||||||
}
|
opts = {};
|
||||||
|
}
|
||||||
|
|
||||||
function escapeAttr(href) {
|
opts = new linkifyjs.Options(opts, defaultRender);
|
||||||
return href.replace(/"/g, '"');
|
var tokens = linkifyjs.tokenize(str);
|
||||||
}
|
var result = [];
|
||||||
|
|
||||||
function attributesToString(attributes) {
|
for (var i = 0; i < tokens.length; i++) {
|
||||||
if (!attributes) {
|
var token = tokens[i];
|
||||||
return '';
|
|
||||||
}
|
|
||||||
var result = [];
|
|
||||||
|
|
||||||
for (var attr in attributes) {
|
if (token.t === 'nl' && opts.get('nl2br')) {
|
||||||
var val = attributes[attr] + '';
|
result.push('<br>\n');
|
||||||
result.push(attr + '="' + escapeAttr(val) + '"');
|
} else if (!token.isLink || !opts.check(token)) {
|
||||||
}
|
result.push(escapeText(token.toString()));
|
||||||
return result.join(' ');
|
} else {
|
||||||
}
|
result.push(opts.render(token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function linkifyStr(str, color='#0645AD') {
|
return result.join('');
|
||||||
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
}
|
||||||
|
|
||||||
opts = new Options(opts);
|
if (!String.prototype.linkify) {
|
||||||
|
Object.defineProperty(String.prototype, 'linkify', {
|
||||||
|
writable: false,
|
||||||
|
value: function linkify(options) {
|
||||||
|
return linkifyStr(this, options);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
var tokens = tokenize(str);
|
return linkifyStr;
|
||||||
var result = [];
|
|
||||||
|
|
||||||
for (var i = 0; i < tokens.length; i++) {
|
})(linkify);
|
||||||
var token = tokens[i];
|
|
||||||
|
|
||||||
if (token.type === 'nl' && opts.nl2br) {
|
|
||||||
result.push('<br>\n');
|
|
||||||
continue;
|
|
||||||
} else if (!token.isLink || !opts.check(token)) {
|
|
||||||
result.push(escapeText(token.toString()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var _opts$resolve = opts.resolve(token);
|
|
||||||
|
|
||||||
var formatted = _opts$resolve.formatted;
|
|
||||||
var formattedHref = _opts$resolve.formattedHref;
|
|
||||||
var tagName = _opts$resolve.tagName;
|
|
||||||
var className = _opts$resolve.className;
|
|
||||||
var target = _opts$resolve.target;
|
|
||||||
var attributes = _opts$resolve.attributes;
|
|
||||||
|
|
||||||
|
|
||||||
var link = '<' + tagName + ' href="' + escapeAttr(formattedHref) + '"';
|
|
||||||
|
|
||||||
if (className) {
|
|
||||||
link += ' class="' + escapeAttr(className) + '"';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target) {
|
|
||||||
link += ' target="' + escapeAttr(target) + '"';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (attributes) {
|
|
||||||
link += ' ' + attributesToString(attributes);
|
|
||||||
}
|
|
||||||
|
|
||||||
link += ' style="color: '+color+';">' + escapeText(formatted) + '</' + tagName + '>';
|
|
||||||
result.push(link);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.join('');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!String.prototype.linkify) {
|
|
||||||
String.prototype.linkify = function (opts) {
|
|
||||||
return linkifyStr(this, opts);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return linkifyStr;
|
|
||||||
}(linkify);
|
|
||||||
window.linkifyStr = linkifyString;
|
|
||||||
})(window, linkify);
|
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue