0b41b471d36b5828cb9d89f6588097b14ea1d73c40bdd96196ae1f7f4fa9d78de41286b30842f8ddf12da1a2c0725b6dbe82aa094e4ddb56bc26b2991610cb 1.1 KB

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