246ef053317ec7abbc65e20d3910610ff74be86a5589a384884ab2504b87cd3bc08d39eb0139871830bd03a905f5be339970f69eb5c1aa70057179f3c2da75 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { EmbedBlot } from 'parchment';
  2. import { sanitize } from './link.js';
  3. const ATTRIBUTES = ['alt', 'height', 'width'];
  4. class Image extends EmbedBlot {
  5. static blotName = 'image';
  6. static tagName = 'IMG';
  7. static create(value) {
  8. const node = super.create(value);
  9. if (typeof value === 'string') {
  10. node.setAttribute('src', this.sanitize(value));
  11. }
  12. return node;
  13. }
  14. static formats(domNode) {
  15. return ATTRIBUTES.reduce((formats, attribute) => {
  16. if (domNode.hasAttribute(attribute)) {
  17. formats[attribute] = domNode.getAttribute(attribute);
  18. }
  19. return formats;
  20. }, {});
  21. }
  22. static match(url) {
  23. return /\.(jpe?g|gif|png)$/.test(url) || /^data:image\/.+;base64/.test(url);
  24. }
  25. static sanitize(url) {
  26. return sanitize(url, ['http', 'https', 'data']) ? url : '//:0';
  27. }
  28. static value(domNode) {
  29. return domNode.getAttribute('src');
  30. }
  31. format(name, value) {
  32. if (ATTRIBUTES.indexOf(name) > -1) {
  33. if (value) {
  34. this.domNode.setAttribute(name, value);
  35. } else {
  36. this.domNode.removeAttribute(name);
  37. }
  38. } else {
  39. super.format(name, value);
  40. }
  41. }
  42. }
  43. export default Image;
  44. //# sourceMappingURL=image.js.map