diff --git a/Common/Code.ts b/Common/Code.ts index 5f8fdb8..800adfa 100644 --- a/Common/Code.ts +++ b/Common/Code.ts @@ -391,6 +391,11 @@ function getCustomEncode(equation: string, direction: number, time: number, app: if (direction === 0) equation = equation.split(toFind[time][i]).join(toReplace[time][i]); else if (direction === 1 && time === 0) { // the single, double quotes, and hyphens should stay minus signs. + // REASON: two encoded backslashes followed by a space are a literal TeX + // row break. Restoring them as an app newline makes MathJax treat cases + // rows as cosmetic whitespace after a CodeCogs de-render/re-render. + // Only the four-backslash marker represents an encoded app newline. + if (i === 3) continue; equation = equation.split(toReplace[time][i]).join(toFind[time][i]); } } diff --git a/tests/common-case-roundtrip.test.js b/tests/common-case-roundtrip.test.js new file mode 100644 index 0000000..ec5618c --- /dev/null +++ b/tests/common-case-roundtrip.test.js @@ -0,0 +1,35 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const test = require('node:test'); +const vm = require('node:vm'); +const { compileScript } = require('./helpers/compile-script'); + +function runtime() { + const context = { console: { log() {}, warn() {}, error() {} } }; + vm.createContext(context); + for (const file of ['Common/Unicode.ts', 'Common/Code.ts', 'SidebarMathJaxShared.ts']) { + vm.runInContext(compileScript(path.join(__dirname, '..', file)), context); + } + return context; +} + +test('CodeCogs de-render preserves explicit cases row breaks for MathJax re-render', () => { + const context = runtime(); + context.equation = String.raw`F_K(x)=\begin{cases}0 &: x\leq0 \\ (1-p)^3 &: 03\end{cases}`; + for (const newlineCharacter of ['%0D', '%0B', '%0A']) { + context.app = { newlineCharacter }; + const restored = vm.runInContext('deEncode(reEncode(equation, app), app)', context); + assert.equal(restored, context.equation); + context.restored = restored; + const mathjax = vm.runInContext('prepareEquationForMathJax(getClientEquation(reEncode(restored, app), app))', context); + assert.equal(mathjax, context.equation); + assert.equal((mathjax.match(/\\\\/g) || []).length, 4); + } +}); + +test('legacy encoded four-backslash newline markers still restore app line breaks', () => { + const context = runtime(); + context.app = { newlineCharacter: '%0D' }; + assert.equal(vm.runInContext("deEncode('a%5C%5C%5C%5C%20b', app)", context), 'a\rb'); +});