Skip to content
Snippets Groups Projects
Verified Commit b72a7b04 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

Add more tests and complete implementation of ext_cosmetics.linkify

parent 75e87f47
No related branches found
No related tags found
2 merge requests!47Release v0.4.0,!42F test linkify
...@@ -28,22 +28,33 @@ ...@@ -28,22 +28,33 @@
*/ */
var cosmetics = new function () { var cosmetics = new function () {
/**
* Cut-off length of links. When linkify processes the links any href
* longer than this will be cut off at character 25 and "[...]" will be
* appended for the link text.
*/
var _link_cut_off_length = 40;
var _linkify = function () { var _linkify = function () {
$('.caosdb-f-property-text-value').each(function (index) { $('.caosdb-f-property-text-value').each(function (index) {
// TODO also extract and convert links surrounded by other text if (/https?:\/\//.test(this.innerText)) {
if (/^https?:\/\//.test(this.innerText)) { var result = this.innerText.replace(/https?:\/\/[^\s]*/g, function (href, index) {
var uri = this.innerText; var link_text = href;
var text = uri if (_link_cut_off_length > 4 && link_text.length > _link_cut_off_length) {
link_text = link_text.substring(0, _link_cut_off_length - 5) + "[...]";
}
return `<a title="Open ${href} in a new tab." target="_blank" class="caosdb-v-property-href-value" href="${href}">${link_text} <i class="bi bi-box-arrow-up-right"></i></a>`;
});
$(this).parent().css("overflow", "hidden"); $(this).html(result);
$(this).parent().css("text-overflow", "ellipsis");
$(this).html(`<a class="caosdb-v-property-href-value" href="${uri}">${text} <i class="bi bi-box-arrow-up-right"></i></a>`);
} }
}); });
} }
/** /**
* Convert any text-value beginning with 'http(s)://' into a link. * Convert any substring of a text-value beginning with 'http(s)://' into a
* link.
* *
* A listener detects edit-mode changes and previews * A listener detects edit-mode changes and previews
*/ */
......
...@@ -36,7 +36,52 @@ QUnit.module("ext_cosmetics.js", { ...@@ -36,7 +36,52 @@ QUnit.module("ext_cosmetics.js", {
} }
}); });
QUnit.test("linkify", function(assert) { QUnit.test("linkify - https", function (assert) {
assert.ok(cosmetics.linkify, "linkify available"); assert.ok(cosmetics.linkify, "linkify available");
var test_cases = [
["https://link", 1],
["this is other text https://link", 1],
["https://link this is other text", 1],
["this is other text https://link and this as well", 1],
["this is other text https://link", 1],
["this is other text https://link and here comes another link https://link and more text", 2],
];
for (let test_case of test_cases) {
var text_value = $(`<div class="caosdb-f-property-text-value">${test_case[0]}</div>`);
$(document.body).append(text_value);
assert.equal($(text_value).find("a[href='https://link']").length, 0, "no link present");
cosmetics.linkify();
assert.equal($(text_value).find("a[href='https://link']").length, test_case[1], "link is present");
text_value.remove();
}
}); });
QUnit.test("linkify - http", function (assert) {
var test_cases = [
["http://link", 1],
["this is other text http://link", 1],
["http://link this is other text", 1],
["this is other text http://link and this as well", 1],
["this is other text http://link", 1],
["this is other text http://link and here comes another link http://link and more text", 2],
];
for (let test_case of test_cases) {
var text_value = $(`<div class="caosdb-f-property-text-value">${test_case[0]}</div>`);
$(document.body).append(text_value);
assert.equal($(text_value).find("a[href='http://link']").length, 0, "no link present");
cosmetics.linkify();
assert.equal($(text_value).find("a[href='http://link']").length, test_case[1], "link is present");
text_value.remove();
}
});
QUnit.test("linkify cut-off (40)", function (assert) {
var test_case = "here is some text https://this.is.a.link/with/more/than/40/characters/ this is more text";
var text_value = $(`<div class="caosdb-f-property-text-value">${test_case}</div>`);
$(document.body).append(text_value);
assert.equal($(text_value).find("a").length, 0, "no link present");
cosmetics.linkify();
assert.equal($(text_value).find("a").length, 1, "link is present");
assert.equal($(text_value).find("a").text(), "https://this.is.a.link/with/more/th[...] ", "link text has been cut off");
text_value.remove();
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment