Skip to content

Commit 2f3d1e7

Browse files
committed
Fix some problems with MathItem.updateDocument() and MathItem.removeFromDocument(). Add MathItem.rerender(). Adjust labs to use updateDocument() rather than inserting the HTML directly. Update maction to use rerender(), and to move the event handlers out of the CHTMLmaction wrapper, so that the wrapper can be freed even though the math (wtih event handlers) is still in the page.
1 parent 4ed3044 commit 2f3d1e7

File tree

10 files changed

+205
-191
lines changed

10 files changed

+205
-191
lines changed

lib/Mml-lab.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import {MathML} from "mathjax3/input/mathml.js";
22
import {CHTML} from "mathjax3/output/chtml.js";
3-
import {AbstractMathItem} from "mathjax3/core/MathItem.js";
4-
import {AbstractMathDocument} from "mathjax3/core/MathDocument.js";
3+
import {HTMLMathItem} from "mathjax3/handlers/html/HTMLMathItem.js";
4+
import {HTMLDocument} from "mathjax3/handlers/html/HTMLDocument.js";
55
import {handleRetriesFor} from "mathjax3/util/Retries.js";
66
import {browserAdaptor} from "mathjax3/adaptors/browserAdaptor.js";
77

8-
class LabMathItem extends AbstractMathItem {};
9-
class LabMathDocument extends AbstractMathDocument {};
10-
11-
let mml = new MathML();
8+
let mml = new MathML({forceReparse: true});
129
let chtml = new CHTML();
1310

14-
let doc = new LabMathDocument(document, browserAdaptor(), {InputJax: mml, OutputJax: chtml});
11+
let doc = new HTMLDocument(document, browserAdaptor(), {InputJax: mml, OutputJax: chtml});
1512
document.head.appendChild(chtml.styleSheet(doc));
1613

1714
const Lab = window.Lab = {
@@ -20,28 +17,28 @@ const Lab = window.Lab = {
2017
display: true,
2118

2219
Typeset() {
20+
this.output.innerHTML = '';
21+
let text = this.output.appendChild(document.createTextNode(''));
22+
2323
let MML = this.mml.value;
24-
let math = new LabMathItem(MML,mml);
24+
let math = new HTMLMathItem(MML,mml);
2525
math.setMetrics(16,8,16*20,100000,1);
2626
math.display = this.display;
27+
math.start = {node: text, n: 0, delim: ''};
28+
math.end = {node: text, n: 0, delim: ''};
2729
this.jax = math;
2830

2931
handleRetriesFor(function () {
3032
math.compile();
3133
math.typeset(doc);
32-
}).then(() => Lab.Update(math.typesetRoot))
33-
.catch(err => {console.log("Error: "+err.message); console.log(err.stack)});
34+
math.updateDocument(doc);
35+
}).catch(err => {console.log("Error: "+err.message); console.log(err.stack)});
3436
},
3537

3638
Keep() {
3739
window.location.search = "?" + encodeURIComponent(this.mml.value);
3840
},
3941

40-
Update(html) {
41-
this.output.innerHTML = '';
42-
this.output.appendChild(html);
43-
},
44-
4542
checkKey: function (textarea, event) {
4643
if (!event) event = window.event;
4744
var code = event.which || event.keyCode;

lib/TeX-lab.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import {TeX} from "mathjax3/input/tex.js";
22
import {CHTML} from "mathjax3/output/chtml.js";
3-
import {AbstractMathItem} from "mathjax3/core/MathItem.js";
4-
import {AbstractMathDocument} from "mathjax3/core/MathDocument.js";
3+
import {HTMLMathItem} from "mathjax3/handlers/html/HTMLMathItem.js";
4+
import {HTMLDocument} from "mathjax3/handlers/html/HTMLDocument.js";
55
import {handleRetriesFor} from "mathjax3/util/Retries.js";
66
import {browserAdaptor} from "mathjax3/adaptors/browserAdaptor.js";
77

8-
class LabMathItem extends AbstractMathItem {};
9-
class LabMathDocument extends AbstractMathDocument {};
10-
118
let tex = new TeX();
129
let chtml = new CHTML();
1310

14-
let doc = new LabMathDocument(document, browserAdaptor(), {InputJax: tex, OutputJax: chtml});
11+
let doc = new HTMLDocument(document, browserAdaptor(), {InputJax: tex, OutputJax: chtml});
1512
document.head.appendChild(chtml.styleSheet(doc));
1613

1714
const Lab = window.Lab = {
@@ -20,28 +17,28 @@ const Lab = window.Lab = {
2017
display: true,
2118

2219
Typeset() {
20+
this.output.innerHTML = '';
21+
let text = this.output.appendChild(document.createTextNode(''));
22+
2323
let TeX = this.tex.value;
24-
let math = new LabMathItem(TeX,tex);
24+
let math = new HTMLMathItem(TeX,tex);
2525
math.setMetrics(16,8,16*20,100000,1);
2626
math.display = this.display;
27+
math.start = {node: text, n: 0, delim: ''};
28+
math.end = {node: text, n: 0, delim: ''};
2729
this.jax = math;
2830

2931
handleRetriesFor(function () {
3032
math.compile();
3133
math.typeset(doc);
32-
}).then(() => Lab.Update(math.typesetRoot))
33-
.catch(err => {console.log("Error: "+err.message); console.log(err.stack)});
34+
math.updateDocument(doc);
35+
}).catch(err => {console.log("Error: "+err.message); console.log(err.stack)});
3436
},
3537

3638
Keep() {
3739
window.location.search = "?" + (this.display ? 1 : 0) + encodeURIComponent(this.tex.value);
3840
},
3941

40-
Update(html) {
41-
this.output.innerHTML = '';
42-
this.output.appendChild(html);
43-
},
44-
4542
setDisplay(checked) {
4643
this.display = checked;
4744
this.Typeset();

mathjax3-ts/adaptors/HTMLAdaptor.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface MinDocument<N, T> {
3737
documentElement: N;
3838
head: N;
3939
body: N;
40+
title: string;
4041
createElement(type: string): N;
4142
createTextNode(text: string): T;
4243
querySelectorAll(selector: string): N[];
@@ -227,6 +228,20 @@ extends AbstractDOMAdaptor<N, T, D> implements MinHTMLAdaptor<N, T, D> {
227228
return doc.documentElement;
228229
}
229230

231+
/*
232+
* @override
233+
*/
234+
public getStatus(doc: D) {
235+
return doc.title;
236+
}
237+
238+
/*
239+
* @override
240+
*/
241+
public setStatus(doc: D, status: string) {
242+
doc.title = status;
243+
}
244+
230245
/*
231246
* @override
232247
*/

mathjax3-ts/adaptors/liteAdaptor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ export class LiteAdaptor extends AbstractDOMAdaptor<LiteElement, LiteText, LiteD
112112
return doc.root;
113113
}
114114

115+
/*
116+
* @override
117+
*/
118+
public getStatus(doc: LiteDocument) {
119+
return ''; // statusline is not maintained
120+
}
121+
122+
/*
123+
* @override
124+
*/
125+
public setStatus(doc: LiteDocument, status: string) {
126+
// statusline is not maintained
127+
}
128+
115129
/*
116130
* @override
117131
*/

mathjax3-ts/core/DOMAdaptor.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ export interface DOMAdaptor<N, T, D> {
8787
*/
8888
root(doc: D): N;
8989

90+
/*
91+
* @param{D} doc The document whose statusline is to be obtained
92+
* @return{string} The document's status line (title)
93+
*/
94+
getStatus(doc: D): string;
95+
96+
/*
97+
* @param{D} doc The document whose statusline is to be set
98+
* @param{string} status The document's new status line (title)
99+
*/
100+
setStatus(doc: D, status: string): void;
101+
90102
/*
91103
* @param{N} node The node to search for tags
92104
* @param{string} name The name of the tag to search for
@@ -385,6 +397,16 @@ export abstract class AbstractDOMAdaptor<N, T, D> implements DOMAdaptor<N, T, D>
385397
*/
386398
public abstract root(doc: D): N;
387399

400+
/*
401+
* @override
402+
*/
403+
public abstract getStatus(doc: D): string;
404+
405+
/*
406+
* @override
407+
*/
408+
public abstract setStatus(doc: D, status: string): void;
409+
388410
/*
389411
* @override
390412
*/

mathjax3-ts/core/MathItem.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ export interface MathItem<N, T, D> {
149149
*/
150150
typeset(document: MathDocument<N, T, D>): void;
151151

152+
/*
153+
* Rerenders an already rendered item and re-inserts it into the document
154+
*
155+
* @param{MathDocument} document The MathDocument in which the math resides
156+
*/
157+
rerender(document: MathDocument<N, T, D>): void;
158+
152159
/*
153160
* Inserts the typeset version in place of the original form in the document
154161
*
@@ -303,6 +310,15 @@ export abstract class AbstractMathItem<N, T, D> implements MathItem<N, T, D> {
303310
}
304311
}
305312

313+
/*
314+
* @override
315+
*/
316+
public rerender(document: MathDocument<N, T, D>) {
317+
this.state(AbstractMathItem.STATE.COMPILED);
318+
this.typeset(document);
319+
this.updateDocument(document);
320+
}
321+
306322
/*
307323
* @override
308324
*/

mathjax3-ts/core/MmlTree/MmlNodes/maction.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,16 @@ export class MmlMaction extends AbstractMmlNode {
114114
this.updateTeXclass(selected);
115115
return prev;
116116
}
117+
118+
/*
119+
* Select the next child for a toggle action
120+
*/
121+
public nextToggleSelection() {
122+
let selection = Math.max(1, (this.attributes.get('selection') as number) + 1);
123+
if (selection > this.childNodes.length) {
124+
selection = 1;
125+
}
126+
this.attributes.set('selection', selection);
127+
}
128+
117129
}

mathjax3-ts/handlers/html/HTMLMathItem.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,19 @@ export class HTMLMathItem<N, T, D> extends AbstractMathItem<N, T, D> {
115115
public removeFromDocument(restore: boolean = false) {
116116
if (this.state() >= STATE.TYPESET) {
117117
let node = this.start.node;
118+
let math: N | T = this.adaptor.text('');
118119
if (restore) {
119120
let text = this.start.delim + this.math + this.end.delim;
120-
let math;
121121
if (this.inputJax.processStrings) {
122122
math = this.adaptor.text(text);
123123
} else {
124124
const doc = this.adaptor.parse(text, 'text/html');
125125
math = this.adaptor.firstChild(this.adaptor.body(doc));
126126
}
127-
this.adaptor.insert(math, node);
128127
}
129-
this.adaptor.remove(node);
128+
this.adaptor.replace(math, node);
129+
this.start.node = this.end.node = math;
130+
this.start.n = this.end.n = 0;
130131
}
131132
}
132133

mathjax3-ts/output/chtml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import {AbstractOutputJax} from '../core/OutputJax.js';
2525
import {OptionList, separateOptions} from '../util/Options.js';
2626
import {MathDocument} from '../core/MathDocument.js';
27-
import {MathItem} from '../core/MathItem.js';
27+
import {MathItem, AbstractMathItem} from '../core/MathItem.js';
2828
import {MmlNode} from '../core/MmlTree/MmlNode.js';
2929
import {CHTMLWrapper} from './chtml/Wrapper.js';
3030
import {CHTMLWrapperFactory} from './chtml/WrapperFactory.js';

0 commit comments

Comments
 (0)