1 | import Inline from '../blots/inline';
|
2 |
|
3 |
|
4 | class Link extends Inline {
|
5 | static create(value) {
|
6 | let node = super.create(value);
|
7 | value = this.sanitize(value);
|
8 | node.setAttribute('href', value);
|
9 | node.setAttribute('target', '_blank');
|
10 | return node;
|
11 | }
|
12 |
|
13 | static formats(domNode) {
|
14 | return domNode.getAttribute('href');
|
15 | }
|
16 |
|
17 | static sanitize(url) {
|
18 | return sanitize(url, this.PROTOCOL_WHITELIST) ? url : this.SANITIZED_URL;
|
19 | }
|
20 |
|
21 | format(name, value) {
|
22 | if (name !== this.statics.blotName || !value) return super.format(name, value);
|
23 | value = this.constructor.sanitize(value);
|
24 | this.domNode.setAttribute('href', value);
|
25 | }
|
26 | }
|
27 | Link.blotName = 'link';
|
28 | Link.tagName = 'A';
|
29 | Link.SANITIZED_URL = 'about:blank';
|
30 | Link.PROTOCOL_WHITELIST = ['http', 'https', 'mailto', 'tel'];
|
31 |
|
32 |
|
33 | function sanitize(url, protocols) {
|
34 | let anchor = document.createElement('a');
|
35 | anchor.href = url;
|
36 | let protocol = anchor.href.slice(0, anchor.href.indexOf(':'));
|
37 | return protocols.indexOf(protocol) > -1;
|
38 | }
|
39 |
|
40 |
|
41 | export { Link as default, sanitize };
|