-
Notifications
You must be signed in to change notification settings - Fork 99
Clear user defined TeX macros from global state #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
f4f2296
cb238ef
d2f8491
8cacb49
5b549fe
b10feb6
540b5e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -741,6 +741,19 @@ function GetState(state) { | |
| MML.SUPER.ID = ID = 0; | ||
| MathJax.OutputJax.CommonHTML.ID = 0; | ||
| } | ||
|
|
||
| // Clear any existing user defined macros, then load macros from state | ||
| Object.keys(TEX.Definitions.macros).forEach(function(macroName){ | ||
| if (TEX.Definitions.macros[macroName].isUser) { | ||
| delete TEX.Definitions.macros[macroName]; | ||
| } | ||
| }); | ||
| if (state && state.macros) { | ||
| for (var macroName in state.macros) { | ||
|
||
| TEX.Definitions.macros[macroName] = state.macros[macroName]; | ||
| TEX.Definitions.macros[macroName].isUser = true; | ||
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| // | ||
|
|
@@ -765,6 +778,7 @@ function ReturnResult(result) { | |
| if (state) { | ||
| var AMS = MathJax.Extension["TeX/AMSmath"]; | ||
| var GLYPH = MathJax.OutputJax.SVG.BBOX.GLYPH; | ||
| var TEX = MathJax.InputJax.TeX; | ||
| state.AMS.startNumber = AMS.startNumber; | ||
| state.AMS.labels = AMS.labels; | ||
| state.AMS.IDs = AMS.IDs; | ||
|
|
@@ -773,6 +787,13 @@ function ReturnResult(result) { | |
| state.defs = GLYPH.defs; | ||
| state.n = GLYPH.n; | ||
| state.ID = ID; | ||
| state.macros = {}; | ||
| for (var macroName in TEX.Definitions.macros) { | ||
|
||
| var macro = TEX.Definitions.macros[macroName]; | ||
| if (macro.isUser) { | ||
| state.macros[macroName] = macro; | ||
| } | ||
| } | ||
| } | ||
| serverState = STATE.READY; | ||
| callback(result, originalData); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| var tape = require('tape'); | ||
| var mjAPI = require("../lib/main.js"); | ||
|
|
||
| tape('macros should be cleared from global state', function(t) { | ||
| t.plan(2); | ||
| mjAPI.start(); | ||
|
|
||
| mjAPI.typeset({ | ||
| math: "\\def\\mymacro{2\\pi} \\mymacro", | ||
| format: "TeX", | ||
| mml: true | ||
| }, function(data) { | ||
| t.false(data.errors, 'Defined and used macro'); | ||
| }); | ||
|
|
||
| mjAPI.typeset({ | ||
| math: "\\mymacro", | ||
| format: "TeX", | ||
| mml: true | ||
| }, function(data) { | ||
| t.true(data.errors, '\\mymacro should no longer be defined'); | ||
| }); | ||
| }); | ||
|
|
||
| tape('macros can be persisted using state option', function(t) { | ||
| t.plan(4); | ||
| mjAPI.start(); | ||
|
|
||
| var state = {}; | ||
|
|
||
| mjAPI.typeset({ | ||
| math: "\\def\\mymacro{2\\pi} \\mymacro", | ||
| format: "TeX", | ||
| mml: true, | ||
| state: state | ||
| }, function(data) { | ||
| t.false(data.errors, 'Defined and used macro'); | ||
| t.equal(Object.keys(state.macros).length, 1, 'Only stores the user defined macro in state'); | ||
|
|
||
| // Ensure state contains only serializable data | ||
| state = JSON.parse(JSON.stringify(state)); | ||
|
|
||
| mjAPI.typeset({ | ||
| math: "\\mymacro", | ||
| format: "TeX", | ||
| mml: true, | ||
| state: state | ||
| }, function(data) { | ||
| t.false(data.errors, '\\mymacro was not persisted in state'); | ||
| }); | ||
|
|
||
| mjAPI.typeset({ | ||
| math: "\\mymacro", | ||
| format: "TeX", | ||
| mml: true, | ||
| }, function(data) { | ||
| t.true(data.errors, '\\mymacro should no longer be defined if state is not provided'); | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you use
TeX.Definitions.macrosa number of times, here, perhapswould be in order.
(Also, there should be a space between the
)and the following{.)