-
Notifications
You must be signed in to change notification settings - Fork 31
/
link.js
84 lines (76 loc) · 2.08 KB
/
link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var H5P = H5P || {};
/**
* H5P Link Library Module.
*/
H5P.Link = (function ($) {
/**
* Link constructor.
*
* @param {Object} parameters
*/
function Link(parameters) {
// Add default parameters
parameters = $.extend(true, {
title: 'New link',
linkWidget: {
protocol: '',
url: ''
}
}, parameters);
var url = '';
if (parameters.linkWidget.protocol !== 'other') {
url += parameters.linkWidget.protocol;
}
url += parameters.linkWidget.url;
/**
* Public. Attach.
*
* @param {jQuery} $container
*/
this.attach = function ($container) {
var sanitizedUrl = sanitizeUrlProtocol(url);
$container.addClass('h5p-link').html('<a href="' + sanitizedUrl + '" target="_blank">' + parameters.title + '</a>')
.keypress(function (event) {
if (event.which === 32) {
this.click();
}
});
};
/**
* Return url
*
* @returns {string}
*/
this.getUrl = function () {
return url;
};
/**
* Private. Remove illegal url protocols from uri
*/
var sanitizeUrlProtocol = function(uri) {
var allowedProtocols = ['http', 'https', 'ftp', 'irc', 'mailto', 'news', 'nntp', 'rtsp', 'sftp', 'ssh', 'tel', 'telnet', 'webcal'];
var first = true;
var before = '';
while (first || uri != before) {
first = false;
before = uri;
var colonPos = uri.indexOf(':');
if (colonPos > 0) {
// We found a possible protocol
var protocol = uri.substr(0, colonPos);
// If the colon is preceeded by a hash, slash or question mark it isn't a protocol
if (protocol.match(/[/?#]/g)) {
break;
}
// Is this a forbidden protocol?
if (allowedProtocols.indexOf(protocol.toLowerCase()) == -1) {
// If illegal, remove the protocol...
uri = uri.substr(colonPos + 1);
}
}
}
return uri;
};
}
return Link;
})(H5P.jQuery);