UNPKG

1.05 kBJavaScriptView Raw
1import Inline from '../blots/inline';
2
3
4class 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}
27Link.blotName = 'link';
28Link.tagName = 'A';
29Link.SANITIZED_URL = 'about:blank';
30Link.PROTOCOL_WHITELIST = ['http', 'https', 'mailto', 'tel'];
31
32
33function 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
41export { Link as default, sanitize };