Skip to content

Build your personal knowledge base with Trilium Notes

test(ckeditor5-math): drop the stale KaTeX null-prototype probe, cover \gdef isolation

Elian Doranc2195abef63e
diff --git a/packages/ckeditor5-math/src/utils.ts b/packages/ckeditor5-math/src/utils.ts
index f93af7777c..01a9e46f6f 100644
--- a/packages/ckeditor5-math/src/utils.ts
+++ b/packages/ckeditor5-math/src/utils.ts
@@ -216,10 +216,11 @@ export function getBalloonPositionData( editor: Editor ): {
}
}
-// CKEditor stores plain-object config values with a null prototype (`Object.create( null )`),
-// but KaTeX calls `macros.hasOwnProperty()` directly while expanding macros, which throws on a
-// prototype-less object. Rebuild `macros` as an ordinary object so the lookup works (this also
-// hands KaTeX a disposable copy it can safely mutate, e.g. via `\gdef`).
+// KaTeX writes macro definitions from `\gdef` straight into the `macros` object it is handed, so
+// give it a disposable copy: Trilium supplies the shared `KATEX_MACROS` constant by reference, and
+// one note's `\gdef` would otherwise leak into every later render. The copy also hands KaTeX an
+// ordinary-prototype object — CKEditor config values are prototype-less (`Object.create( null )`),
+// which crashed KaTeX <= 0.17 (#9523) before it switched to `Object.prototype.hasOwnProperty.call()`.
function normalizeKatexMacros( options: KatexOptions ): { macros?: object } {
const { macros } = options;
if ( macros && typeof macros === 'object' ) {
diff --git a/packages/ckeditor5-math/tests/utils.ts b/packages/ckeditor5-math/tests/utils.ts
index 09c31d9126..449c47dd87 100644
--- a/packages/ckeditor5-math/tests/utils.ts
+++ b/packages/ckeditor5-math/tests/utils.ts
@@ -20,21 +20,31 @@ describe( 'renderEquation (KaTeX)', () => {
} );
// Regression for #9523: CKEditor stores config objects with a null prototype
- // (`Object.create( null )`), and KaTeX calls `macros.hasOwnProperty()` directly while
- // expanding macros, which throws "hasOwnProperty is not a function" on such an object.
+ // (`Object.create( null )`). KaTeX <= 0.17 called `macros.hasOwnProperty()` directly while
+ // expanding macros and threw "hasOwnProperty is not a function" on such an object; 0.18 moved to
+ // `Object.prototype.hasOwnProperty.call()`, so the crash is gone upstream. Either way the
+ // normalization runs and the formula has to render.
it( 'renders with a prototype-less macros object (as produced by CKEditor config)', async () => {
const macros = Object.assign( Object.create( null ), { '\\differentialD': '\\mathrm{d}' } );
- // Confirms the root cause: passing the raw null-prototype object straight to KaTeX throws.
- expect( () => katex.renderToString( '\\differentialD', { macros, throwOnError: true } ) ).to.throw();
-
await renderEquation( '\\int f(x) \\differentialD x', element, 'katex', undefined, false, false, '', [], { macros } );
- // renderEquation normalizes the object, so the formula renders instead of erroring.
expect( element.querySelector( '.katex' ) ).to.not.be.null;
expect( element.textContent ?? '' ).to.not.contain( 'hasOwnProperty' );
} );
+ // KaTeX writes `\gdef` definitions straight into the `macros` object it is handed, and Trilium
+ // passes the shared `KATEX_MACROS` constant by reference, so one note's `\gdef` must not leak
+ // into every later render in the session. This is what keeps the normalization load-bearing now
+ // that KaTeX itself tolerates prototype-less macros.
+ it( 'keeps a \\gdef out of the caller\'s macros object', async () => {
+ const macros = { '\\differentialD': '\\mathrm{d}' };
+
+ await renderEquation( '\\gdef\\leaked{42}\\leaked', element, 'katex', undefined, false, false, '', [], { macros } );
+
+ expect( Object.keys( macros ) ).to.deep.equal( [ '\\differentialD' ] );
+ } );
+
it( 'renders without any custom macros', async () => {
await renderEquation( 'x^2', element, 'katex', undefined, false, false, '', [], {} );