diff --git a/web/tests/copy_and_paste.test.cjs b/web/tests/copy_and_paste.test.cjs index 51e1add862..beb47c2005 100644 --- a/web/tests/copy_and_paste.test.cjs +++ b/web/tests/copy_and_paste.test.cjs @@ -5,6 +5,7 @@ const assert = require("node:assert/strict"); const {JSDOM} = require("jsdom"); const katex_tests = require("../../zerver/tests/fixtures/katex_test_cases.json"); +const {parse} = require("../src/markdown.ts"); const {zrequire, set_global} = require("./lib/namespace.cjs"); const {run_test} = require("./lib/test.cjs"); @@ -256,6 +257,15 @@ run_test("paste_handler_converter", () => { assert.equal(copy_and_paste.paste_handler_converter(input), " \n\n$ 20.00\n\n$ 7.00"); // Math block tests + + /* + This first batch of math block tests uses captured fixtures + (`input`). This lets us verify behavior like the empty + `.katex-display` divs in case of newlines in the + `original_markdown` See + https://github.com/zulip/zulip/pull/32629#discussion_r1883810127 + */ + for (const math_block_test of katex_tests.math_block_tests) { input = math_block_test.input; assert.equal( @@ -264,20 +274,33 @@ run_test("paste_handler_converter", () => { ); } - // Inline Math Expression tests + // This next batch of tests round-trips the LaTeX syntax through + // the Markdown processor and then the paste handler. + const dummy_helper_config = { + should_translate_emoticons: () => false, + get_linkifier_map: () => new Map(), + }; + assert.equal(dummy_helper_config.should_translate_emoticons(), false); + assert.deepEqual(dummy_helper_config.get_linkifier_map(), new Map()); + for (const inline_math_expression_test of katex_tests.inline_math_expression_tests) { - input = inline_math_expression_test.input; + const paste_html = parse({ + raw_content: inline_math_expression_test.original_markup, + helper_config: dummy_helper_config, + }).content; assert.equal( - copy_and_paste.paste_handler_converter(input), + copy_and_paste.paste_handler_converter(paste_html), inline_math_expression_test.expected_output, ); } - // Span conversion check for (const span_conversion_test of katex_tests.text_node_to_span_conversion_tests) { - input = span_conversion_test.input; + const paste_html = parse({ + raw_content: span_conversion_test.original_markup, + helper_config: dummy_helper_config, + }).content; assert.equal( - copy_and_paste.paste_handler_converter(input), + copy_and_paste.paste_handler_converter(paste_html), span_conversion_test.expected_output, ); } diff --git a/zerver/tests/fixtures/katex_test_cases.json b/zerver/tests/fixtures/katex_test_cases.json index 0455b01965..108a949232 100644 --- a/zerver/tests/fixtures/katex_test_cases.json +++ b/zerver/tests/fixtures/katex_test_cases.json @@ -23,51 +23,43 @@ { "original_markup":"intro text\n$$p+q =4$$", "description": "Inline Expression 1.", - "input": "

intro text
\np+q=4p+q =4p+q=4

", "expected_output": "intro text\n$$p+q =4$$" }, { "original_markup":"intro text\n$$p+q =4$$\n$$x+y=z$$\nhello world", "description": "Inline Expression 2 (multiline).", - "input": "

intro text
\n p+q=4p+q =4p+q=4
\n x+y=zx+y=zx+y=z
\n hello world

", "expected_output": "intro text\n$$p+q =4$$\n$$x+y=z$$\nhello world" }, { "original_markup":"hey there\n$$a+b$$ some text $$c+d$$\nhow", "description": "Inline expression with intermediate text node between two inline expressions.", - "input": "

hey there
\na+ba+ba+b some text c+dc+dc+d
\nhow

", "expected_output": "hey there\n$$a+b$$ some text $$c+d$$\nhow" }, { "original_markup":"I *think* $$e^{i\\pi} = -1$$ is the **coolest** thing I've seen since $$x^2 + y^2 = z^2$$ blew my mind.", "description": "Inline expression with bold and emphasized text.", - "input": "

I think eiĻ€=āˆ’1e^{i\\pi} = -1eiĻ€=āˆ’1 is the coolest thing I've seen since x2+y2=z2x^2 + y^2 = z^2x2+y2=z2 blew my mind.

", "expected_output":"I *think* $$e^{i\\pi} = -1$$ is the **coolest** thing I've seen since $$x^2 + y^2 = z^2$$ blew my mind." }, { "original_markup":"there $$a+b$$ `some code` some text _hello_ `more code` $$y+z=4444$$ ~~don't read this~~ some text `code` $$p+q=44$$ text haha wooo", "expected_output":"there $$a+b$$ `some code` some text _hello_ `more code` $$y+z=4444$$ ~~don't read this~~ some text `code` $$p+q=44$$ text haha wooo", - "input":"

there a+ba+ba+b some code some text _hello_ more code y+z=4444y+z=4444y+z=4444 don't read this some text code p+q=44p+q=44p+q=44 text haha wooo

", "description": "Inline expression with inline elements." }, { "description":"Nested inline katex siblings", "expected_output":"like ~~*this* is not $$x^2$$, it's $$xx/2$$, **see?**~~` good", - "original_markup":"like ~~*this* is not $$x^2$$, it's $$xx/2$$, **see?**~~` good", - "input":"

like this is not x2x^2x2, it's xx/2xx/2xx/2, see?` good

" + "original_markup":"like ~~*this* is not $$x^2$$, it's $$xx/2$$, **see?**~~` good" },{ "description":"Another katex inline expression containing nested tags", "expected_output":"world ~~it works **out $$a+b=c$$** so `well` if you try *this*** not $$p+q=r$$ that~~ good good", - "original_markup":"world ~~it works **out $$a+b=c$$** so `well` if you try *this*** not $$p+q=r$$ that~~ good good", - "input":"

world it works out a+b=ca+b=ca+b=c so well if you try this** not p+q=rp+q=rp+q=r that good good

" + "original_markup":"world ~~it works **out $$a+b=c$$** so `well` if you try *this*** not $$p+q=r$$ that~~ good good" } ], "text_node_to_span_conversion_tests":[ { "original_markup":"A paragraph with some `code` and a [link](https://example.com/).", "description":"Check conversion of intermediate text nodes and ensure it doesn't distubt pasting/quoting", - "input":"

A paragraph with some code and a link.

", "expected_output":"A paragraph with some `code` and a [link](https://example.com/)." } ]