mirror of
https://git.jami.net/savoirfairelinux/jami-client-qt.git
synced 2025-08-03 14:25:38 +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 description = null
|
||||
var image = null
|
||||
if (!url.includes("http://") && !url.includes("https://")) {
|
||||
url = "http://".concat(url)
|
||||
var u = new URL(url)
|
||||
if (u.protocol === '') {
|
||||
url = "https://".concat(url)
|
||||
}
|
||||
var domain = (new URL(url))
|
||||
fetch(url, {
|
||||
|
|
|
@ -1,118 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2016 SoapBox Innovations Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* 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
|
||||
* THE SOFTWARE.
|
||||
* Copyright (c) 2021 SoapBox Innovations Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* 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
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
var linkifyStr = (function (linkifyjs) {
|
||||
'use strict';
|
||||
|
||||
'use strict';
|
||||
/**
|
||||
Convert strings of text into linkable HTML text
|
||||
*/
|
||||
|
||||
;(function (window, linkify) {
|
||||
var linkifyString = function (linkify) {
|
||||
'use strict';
|
||||
function escapeText(text) {
|
||||
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
|
||||
/**
|
||||
Convert strings of text into linkable HTML text
|
||||
*/
|
||||
function escapeAttr(href) {
|
||||
return href.replace(/"/g, '"');
|
||||
}
|
||||
|
||||
var tokenize = linkify.tokenize;
|
||||
var options = linkify.options;
|
||||
var Options = options.Options;
|
||||
function attributesToString(attributes) {
|
||||
var result = [];
|
||||
|
||||
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) {
|
||||
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
function linkifyStr(str, opts) {
|
||||
if (opts === void 0) {
|
||||
opts = {};
|
||||
}
|
||||
|
||||
function escapeAttr(href) {
|
||||
return href.replace(/"/g, '"');
|
||||
}
|
||||
opts = new linkifyjs.Options(opts, defaultRender);
|
||||
var tokens = linkifyjs.tokenize(str);
|
||||
var result = [];
|
||||
|
||||
function attributesToString(attributes) {
|
||||
if (!attributes) {
|
||||
return '';
|
||||
}
|
||||
var result = [];
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
var token = tokens[i];
|
||||
|
||||
for (var attr in attributes) {
|
||||
var val = attributes[attr] + '';
|
||||
result.push(attr + '="' + escapeAttr(val) + '"');
|
||||
}
|
||||
return result.join(' ');
|
||||
}
|
||||
if (token.t === 'nl' && opts.get('nl2br')) {
|
||||
result.push('<br>\n');
|
||||
} else if (!token.isLink || !opts.check(token)) {
|
||||
result.push(escapeText(token.toString()));
|
||||
} else {
|
||||
result.push(opts.render(token));
|
||||
}
|
||||
}
|
||||
|
||||
function linkifyStr(str, color='#0645AD') {
|
||||
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
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);
|
||||
var result = [];
|
||||
return linkifyStr;
|
||||
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
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);
|
||||
})(linkify);
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue