@bobfrankston/rmfmail 1.2.194 → 1.2.195

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3568,6 +3568,23 @@ function markdownToEmailHtml(md) {
3568
3568
  }
3569
3569
  return out.join("\n");
3570
3570
  }
3571
+ function looksLikeHtml(text) {
3572
+ if (!text || text.length > 5e5)
3573
+ return false;
3574
+ const t = text.trim();
3575
+ if (!t.startsWith("<"))
3576
+ return false;
3577
+ if (!/^<(!doctype|html|head|body|div|p|table|ul|ol|li|h[1-6]|blockquote|section|article|header|footer|span|b|i|strong|em|a|img|br|hr|pre|code|font|center)\b/i.test(t))
3578
+ return false;
3579
+ return /<\/[a-z][a-z0-9]*\s*>/i.test(t) || /^<(img|br|hr|!doctype)\b/i.test(t);
3580
+ }
3581
+ function sanitizeHtmlSource(html) {
3582
+ let t = html;
3583
+ const bm = t.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
3584
+ if (bm)
3585
+ t = bm[1];
3586
+ return t.replace(/<script\b[\s\S]*?<\/script\s*>/gi, "").replace(/<style\b[\s\S]*?<\/style\s*>/gi, "").replace(/\son[a-z]+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, "").replace(/\s(href|src)\s*=\s*(["']?)\s*javascript:[^"'\s>]*\2/gi, "");
3587
+ }
3571
3588
  function wireMarkdownPaste(target, insertHtml) {
3572
3589
  target.addEventListener("paste", (e) => {
3573
3590
  try {
@@ -3579,11 +3596,18 @@ function wireMarkdownPaste(target, insertHtml) {
3579
3596
  if (Array.from(cb.items).some((it) => it.kind === "file"))
3580
3597
  return;
3581
3598
  const plain = cb.getData("text/plain");
3582
- if (!plain || !looksLikeMarkdown(plain))
3599
+ if (!plain)
3600
+ return;
3601
+ let converted = null;
3602
+ if (looksLikeHtml(plain))
3603
+ converted = sanitizeHtmlSource(plain);
3604
+ else if (looksLikeMarkdown(plain))
3605
+ converted = markdownToEmailHtml(plain);
3606
+ if (converted === null)
3583
3607
  return;
3584
3608
  e.preventDefault();
3585
3609
  e.stopImmediatePropagation();
3586
- insertHtml(markdownToEmailHtml(plain));
3610
+ insertHtml(converted);
3587
3611
  } catch {
3588
3612
  }
3589
3613
  }, true);
@@ -4035,6 +4059,14 @@ function createQuillEditor(container2) {
4035
4059
  }
4036
4060
  return;
4037
4061
  }
4062
+ if (plain && looksLikeHtml(plain)) {
4063
+ consume();
4064
+ const range = q.getSelection(true) || { index: q.getLength(), length: 0 };
4065
+ if (range.length > 0)
4066
+ q.deleteText(range.index, range.length);
4067
+ q.clipboard.dangerouslyPasteHTML(range.index, sanitizeHtmlSource(plain));
4068
+ return;
4069
+ }
4038
4070
  if (plain && looksLikeMarkdown(plain)) {
4039
4071
  consume();
4040
4072
  const range = q.getSelection(true) || { index: q.getLength(), length: 0 };