Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

test(editor): cover markdown-on-paste (decision branches + parsed payload)

Sergey Kozyrenko5abfede29aa4
diff --git a/frontend/src/components/shared/markdown-editor/editor-paste.test.ts b/frontend/src/components/shared/markdown-editor/editor-paste.test.ts
new file mode 100644
index 00000000..bbad3da9
--- /dev/null
+++ b/frontend/src/components/shared/markdown-editor/editor-paste.test.ts
@@ -0,0 +1,84 @@
+import { Editor } from '@tiptap/core';
+import { Slice } from '@tiptap/pm/model';
+import { beforeAll, describe, expect, it } from 'vitest';
+
+import { shouldParseMarkdownOnPaste } from './editor-paste';
+import { createMarkdownExtensions } from './markdown-editor-extensions';
+
+beforeAll(() => {
+ document.elementFromPoint = () => null;
+ const r = { bottom: 0, height: 0, left: 0, right: 0, toJSON: () => ({}), top: 0, width: 0, x: 0, y: 0 };
+ Range.prototype.getBoundingClientRect = () => r as DOMRect;
+ Range.prototype.getClientRects = () =>
+ ({ item: () => null, length: 0, [Symbol.iterator]: [][Symbol.iterator] }) as unknown as DOMRectList;
+});
+
+describe('shouldParseMarkdownOnPaste — markdown-parse plain text, defer rich sources', () => {
+ it('parses plain-text markdown (no HTML on the clipboard)', () => {
+ expect(shouldParseMarkdownOnPaste('# Heading', '', false)).toBe(true);
+ });
+
+ it('ignores an empty / whitespace-only paste', () => {
+ expect(shouldParseMarkdownOnPaste(' \n\t ', '', false)).toBe(false);
+ });
+
+ it('defers an in-editor copy (data-pm-slice) so ProseMirror keeps its fidelity', () => {
+ expect(shouldParseMarkdownOnPaste('# x', '<p data-pm-slice="1 1 []">x</p>', false)).toBe(false);
+ });
+
+ it.each([
+ '<ul><li>x</li></ul>',
+ '<table><tbody><tr><td>x</td></tr></tbody></table>',
+ '<h2>x</h2>',
+ '<blockquote>x</blockquote>',
+ '<pre>x</pre>',
+ ])('defers rich HTML carrying block tags: %s', (html) => {
+ expect(shouldParseMarkdownOnPaste('# x', html, false)).toBe(false);
+ });
+
+ it('still parses when the HTML is only styled-inline (e.g. a VS Code span, no block tags)', () => {
+ expect(shouldParseMarkdownOnPaste('**x**', '<span style="color:red">**x**</span>', false)).toBe(true);
+ });
+
+ it('keeps a paste inside a code context literal', () => {
+ expect(shouldParseMarkdownOnPaste('# x', '', true)).toBe(false);
+ });
+});
+
+describe('MarkdownPaste — the parsed payload matches load (same faithful markdown layer)', () => {
+ const pasteEvent = (text: string, html = ''): ClipboardEvent =>
+ ({
+ clipboardData: { getData: (type: string) => (type === 'text/html' ? html : type === 'text/plain' ? text : '') },
+ }) as unknown as ClipboardEvent;
+
+ const pasteHtml = (text: string, html = ''): string => {
+ const editor = new Editor({ content: '', contentType: 'markdown', extensions: createMarkdownExtensions() });
+ editor.view.someProp('handlePaste', (handler) => handler(editor.view, pasteEvent(text, html), Slice.empty));
+ const out = editor.getHTML();
+ editor.destroy();
+
+ return out;
+ };
+
+ it('parses pasted block markdown into rich blocks (heading + list + table)', () => {
+ const html = pasteHtml('# Heading\n\n- one\n- two\n\n| A | B |\n| --- | --- |\n| 1 | 2 |');
+
+ expect(html).toContain('<h1>');
+ expect(html).toContain('<ul>');
+ expect(html).toContain('<table');
+ });
+
+ it('keeps _ and __ literal on paste, matching load (no <strong>/<em>)', () => {
+ const html = pasteHtml('a __dunder__ and _under_ here');
+
+ expect(html).not.toContain('<strong>');
+ expect(html).not.toContain('<em>');
+ expect(html).toContain('__dunder__');
+ });
+
+ it('defers to ProseMirror (no markdown parse) when the clipboard carries rich block HTML', () => {
+ const html = pasteHtml('# fake', '<ul><li>web item</li></ul>');
+
+ expect(html).not.toContain('<h1>');
+ });
+});
diff --git a/frontend/src/components/shared/markdown-editor/editor-paste.ts b/frontend/src/components/shared/markdown-editor/editor-paste.ts
index 3844d17a..9560bb07 100644
--- a/frontend/src/components/shared/markdown-editor/editor-paste.ts
+++ b/frontend/src/components/shared/markdown-editor/editor-paste.ts
@@ -8,6 +8,18 @@ import { Plugin, PluginKey } from '@tiptap/pm/state';
// survives, and keep pastes inside code literal.
const RICH_HTML_BLOCK = /<(?:h[1-6]|ul|ol|li|table|thead|tbody|tr|td|th|blockquote|pre|img|hr)\b/i;
+export const shouldParseMarkdownOnPaste = (text: string, html: string, isCodeContext: boolean): boolean => {
+ if (!text.trim()) {
+ return false;
+ }
+
+ if (html.includes('data-pm-slice') || RICH_HTML_BLOCK.test(html)) {
+ return false;
+ }
+
+ return !isCodeContext;
+};
+
export const MarkdownPaste = Extension.create({
addProseMirrorPlugins() {
const { editor } = this;
@@ -18,18 +30,11 @@ export const MarkdownPaste = Extension.create({
props: {
handlePaste(view, event) {
const text = event.clipboardData?.getData('text/plain') ?? '';
-
- if (!text.trim()) {
- return false;
- }
-
const html = event.clipboardData?.getData('text/html') ?? '';
+ const isCodeContext =
+ view.state.selection.$from.parent.type.spec.code || editor.isActive('code');
- if (html.includes('data-pm-slice') || RICH_HTML_BLOCK.test(html)) {
- return false;
- }
-
- if (view.state.selection.$from.parent.type.spec.code || editor.isActive('code')) {
+ if (!shouldParseMarkdownOnPaste(text, html, isCodeContext)) {
return false;
}