Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Common/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/common-case-roundtrip.test.js
Original file line number Diff line number Diff line change
@@ -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 &: 0<x\le1 \\ (1-p)^3+3p(1-p)^2 &: 1<x\le2 \\ (1-p)^3+3p(1-p)^2+3p^2(1-p) &: 2<x\le3 \\ 1 &: x>3\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');
});
Loading