-
Notifications
You must be signed in to change notification settings - Fork 21
/
TeXZilla.jison
1508 lines (1407 loc) · 43.6 KB
/
TeXZilla.jison
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
%{
var MathMLNameSpace = "http://www.w3.org/1998/Math/MathML",
SVGNameSpace = "http://www.w3.org/2000/svg",
TeXMimeTypes = ["TeX", "LaTeX", "text/x-tex", "text/x-latex",
"application/x-tex", "application/x-latex"];
function escapeText(aString) {
/* Escape reserved XML characters for use as text nodes. */
return aString.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
function escapeQuote(aString) {
/* Escape the double quote characters for use as attribute. */
return aString.replace(/"/g, """);
}
function namedSpaceToEm(aString) {
var index = [
"negativeveryverythinmathspace",
"negativeverythinmathspace",
"negativemediummathspace",
"negativethickmathspace",
"negativeverythickmathspace",
"negativeveryverythickmathspace",
"",
"veryverythinmathspace",
"verythinmathspace",
"thinmathspace",
"mediummathspace",
"thickmathspace",
"verythickmathspace",
"veryverythickmathspace"
].indexOf(aString);
return (index === -1 ? 0 : index - 6) / 18.0;
}
function parseLength(aString) {
/* See http://www.w3.org/TR/MathML3/appendixa.html#parsing_length */
aString = aString.trim();
var lengthRegexp = /(-?[0-9]*(?:[0-9]\.?|\.[0-9])[0-9]*)(e[mx]|in|cm|mm|p[xtc]|%)?/, result = lengthRegexp.exec(aString);
if (result) {
result[1] = parseFloat(result[1]);
if (!result[2]) {
/* Unitless values are treated as a percent */
result[1] *= 100;
result[2] = "%";
}
return { l: result[1], u: result[2] };
}
return { l: namedSpaceToEm(aString), u: "em" };
}
function serializeTree(aTree) {
var output = "<" + aTree["tag"];
for (var name in aTree["attributes"]) {
if (aTree["attributes"][name] !== undefined)
output += " " + name + "=\"" + aTree["attributes"][name] + "\"";
}
if (aTree["content"]) {
output += ">";
if (Array.isArray(aTree["content"])) {
aTree["content"].forEach(function(child) {
output += serializeTree(child);
});
} else
output += aTree["content"];
output += "</" + aTree["tag"] + ">";
} else {
output += "/>";
}
return output;
}
function newTag(aTag, aChildren, aAttributes) {
return {
"tag": aTag,
"content": aChildren,
"attributes": aAttributes
};
}
function isEmptyMrow(aTree) {
return aTree["tag"] === "mrow" && !aTree["content"] && !aTree["attributes"];
}
function newMo(aContent, aLeftSpace, aRightSpace) {
return newTag("mo", escapeText(aContent), {
"lspace": aLeftSpace !== undefined ? aLeftSpace + "em" : undefined,
"rspace": aRightSpace !== undefined ? aRightSpace + "em" : undefined
});
}
function newMi(aContent, aNormal) {
return newTag("mi", escapeText(aContent),
aNormal ? { "mathvariant": "normal" } : undefined);
}
function newSpace(aWidth) {
return newTag("mspace", null, {"width": aWidth + "em"});
}
function applyMathVariantToCharacter(codePoint, aMathVariant) {
// FIXME: We should have LaTeX commmands for all these variants.
// See https://github.com/fred-wang/TeXZilla/issues/64
var mathvariant = [
"bold",
"italic",
"bold-italic",
"script",
"bold-script",
"fraktur",
"double-struck",
"bold-fraktur",
"sans-serif",
"bold-sans-serif",
"sans-serif-italic",
"sans-serif-bold-italic",
"monospace",
"initial",
"tailed",
"looped",
"stretched"
].indexOf(aMathVariant);
var Bold = 0;
var Italic = 1;
var BoldItalic = 2;
var Script = 3;
var BoldScript = 4;
var Fraktur = 5;
var DoubleStruck = 6;
var BoldFraktur = 7;
var SansSerif = 8;
var BoldSansSerif = 9;
var SansSerifItalic = 10;
var SansSerifBoldItalic = 11;
var Monospace = 12;
var Initial = 13;
var Tailed = 14;
var Looped = 15;
var Stretched = 16;
var greekUpperTheta = 0x03F4;
var holeGreekUpperTheta = 0x03A2;
var nabla = 0x2207;
var partialDifferential = 0x2202;
var greekUpperAlpha = 0x0391;
var greekUpperOmega = 0x03A9;
var greekLowerAlpha = 0x03B1;
var greekLowerOmega = 0x03C9;
var greekLunateEpsilonSymbol = 0x03F5;
var greekThetaSymbol = 0x03D1;
var greekKappaSymbol = 0x03F0;
var greekPhiSymbol = 0x03D5;
var greekRhoSymbol = 0x03F1;
var greekPiSymbol = 0x03D6;
var greekLetterDigamma = 0x03DC;
var greekSmallLetterDigamma = 0x03DD;
var mathBoldCapitalDigamma = 0x1D7CA;
var mathBoldSmallDigamma = 0x1D7CB;
var latinSmallLetterDotlessI = 0x0131;
var latinSmallLetterDotlessJ = 0x0237;
var mathItalicSmallDotlessI = 0x1D6A4;
var mathItalicSmallDotlessJ = 0x1D6A5;
var digit0 = 0x30;
var digit9 = 0x39;
var upperA = 0x41;
var upperZ = 0x5A;
var smallA = 0x61;
var smallZ = 0x7A;
var mathBoldUpperA = 0x1D400;
var mathItalicUpperA = 0x1D434;
var mathBoldSmallA = 0x1D41A;
var mathBoldUpperAlpha = 0x1D6A8;
var mathBoldSmallAlpha = 0x1D6C2;
var mathItalicUpperAlpha = 0x1D6E2;
var mathBoldDigitZero = 0x1D7CE;
var mathDoubleStruckZero = 0x1D7D8;
var mathBoldUpperTheta = 0x1D6B9;
var mathBoldNabla = 0x1D6C1;
var mathBoldPartialDifferential = 0x1D6DB;
var mathBoldEpsilonSymbol = 0x1D6DC;
var mathBoldThetaSymbol = 0x1D6DD;
var mathBoldKappaSymbol = 0x1D6DE;
var mathBoldPhiSymbol = 0x1D6DF;
var mathBoldRhoSymbol = 0x1D6E0;
var mathBoldPiSymbol = 0x1D6E1;
/* Exceptional characters with at most one possible transformation. */
if (codePoint == holeGreekUpperTheta)
return codePoint;
if (codePoint == greekLetterDigamma) {
if (mathvariant == Bold)
return mathBoldCapitalDigamma;
return codePoint;
}
if (codePoint == greekSmallLetterDigamma) {
if (mathvariant == Bold)
return mathBoldSmallDigamma;
return codePoint;
}
if (codePoint == latinSmallLetterDotlessI) {
if (mathvariant == Italic)
return mathItalicSmallDotlessI;
return codePoint;
}
if (codePoint == latinSmallLetterDotlessJ) {
if (mathvariant == Italic)
return mathItalicSmallDotlessJ;
return codePoint;
}
var baseChar, multiplier, map;
/* Latin */
if (upperA <= codePoint && codePoint <= upperZ ||
smallA <= codePoint && codePoint <= smallZ) {
baseChar = codePoint <= upperZ ? codePoint - upperA :
mathBoldSmallA - mathBoldUpperA + codePoint - smallA;
multiplier = mathvariant;
if (mathvariant > Monospace)
return codePoint; // Latin doesn't support the Arabic mathvariants
var transformedChar = baseChar + mathBoldUpperA +
multiplier * (mathItalicUpperA - mathBoldUpperA);
map = {
0x1D455: 0x210E,
0x1D49D: 0x212C,
0x1D4A0: 0x2130,
0x1D4A1: 0x2131,
0x1D4A3: 0x210B,
0x1D4A4: 0x2110,
0x1D4A7: 0x2112,
0x1D4A8: 0x2133,
0x1D4AD: 0x211B,
0x1D4BA: 0x212F,
0x1D4BC: 0x210A,
0x1D4C4: 0x2134,
0x1D506: 0x212D,
0x1D50B: 0x210C,
0x1D50C: 0x2111,
0x1D515: 0x211C,
0x1D51D: 0x2128,
0x1D53A: 0x2102,
0x1D53F: 0x210D,
0x1D545: 0x2115,
0x1D547: 0x2119,
0x1D548: 0x211A,
0x1D549: 0x211D,
0x1D551: 0x2124
};
return map[transformedChar] ? map[transformedChar] : transformedChar;
}
/* Digits */
if (digit0 <= codePoint && codePoint <= digit9) {
baseChar = codePoint - digit0;
switch (mathvariant) {
case Bold:
multiplier = 0;
break;
case DoubleStruck:
multiplier = 1;
break;
case SansSerif:
multiplier = 2;
break;
case BoldSansSerif:
multiplier = 3;
break;
case Monospace:
multiplier = 4;
break;
default:
return codePoint;
}
return baseChar + multiplier * (mathDoubleStruckZero - mathBoldDigitZero) +
mathBoldDigitZero;
}
// Arabic characters are defined within this range
if (0x0600 <= codePoint && codePoint <= 0x06FF) {
// The Arabic mathematical block is not continuous, nor does it have a
// monotonic mapping to the unencoded characters, requiring the use of a
// lookup table.
switch (mathvariant) {
case Initial:
map = {
0x628: 0x1EE21,
0x62A: 0x1EE35,
0x62B: 0x1EE36,
0x62C: 0x1EE22,
0x62D: 0x1EE27,
0x62E: 0x1EE37,
0x633: 0x1EE2E,
0x634: 0x1EE34,
0x635: 0x1EE31,
0x636: 0x1EE39,
0x639: 0x1EE2F,
0x63A: 0x1EE3B,
0x641: 0x1EE30,
0x642: 0x1EE32,
0x643: 0x1EE2A,
0x644: 0x1EE2B,
0x645: 0x1EE2C,
0x646: 0x1EE2D,
0x647: 0x1EE24,
0x64A: 0x1EE29
};
break;
case Tailed:
map = {
0x62C: 0x1EE42,
0x62D: 0x1EE47,
0x62E: 0x1EE57,
0x633: 0x1EE4E,
0x634: 0x1EE54,
0x635: 0x1EE51,
0x636: 0x1EE59,
0x639: 0x1EE4F,
0x63A: 0x1EE5B,
0x642: 0x1EE52,
0x644: 0x1EE4B,
0x646: 0x1EE4D,
0x64A: 0x1EE49,
0x66F: 0x1EE5F,
0x6BA: 0x1EE5D
};
break;
case Stretched:
map = {
0x628: 0x1EE61,
0x62A: 0x1EE75,
0x62B: 0x1EE76,
0x62C: 0x1EE62,
0x62D: 0x1EE67,
0x62E: 0x1EE77,
0x633: 0x1EE6E,
0x634: 0x1EE74,
0x635: 0x1EE71,
0x636: 0x1EE79,
0x637: 0x1EE68,
0x638: 0x1EE7A,
0x639: 0x1EE6F,
0x63A: 0x1EE7B,
0x641: 0x1EE70,
0x642: 0x1EE72,
0x643: 0x1EE6A,
0x645: 0x1EE6C,
0x646: 0x1EE6D,
0x647: 0x1EE64,
0x64A: 0x1EE69,
0x66E: 0x1EE7C,
0x6A1: 0x1EE7E
};
break;
case Looped:
map = {
0x627: 0x1EE80,
0x628: 0x1EE81,
0x62A: 0x1EE95,
0x62B: 0x1EE96,
0x62C: 0x1EE82,
0x62D: 0x1EE87,
0x62E: 0x1EE97,
0x62F: 0x1EE83,
0x630: 0x1EE98,
0x631: 0x1EE93,
0x632: 0x1EE86,
0x633: 0x1EE8E,
0x634: 0x1EE94,
0x635: 0x1EE91,
0x636: 0x1EE99,
0x637: 0x1EE88,
0x638: 0x1EE9A,
0x639: 0x1EE8F,
0x63A: 0x1EE9B,
0x641: 0x1EE90,
0x642: 0x1EE92,
0x644: 0x1EE8B,
0x645: 0x1EE8C,
0x646: 0x1EE8D,
0x647: 0x1EE84,
0x648: 0x1EE85,
0x64A: 0x1EE89
};
break;
case DoubleStruck:
map = {
0x628: 0x1EEA1,
0x62A: 0x1EEB5,
0x62B: 0x1EEB6,
0x62C: 0x1EEA2,
0x62D: 0x1EEA7,
0x62E: 0x1EEB7,
0x62F: 0x1EEA3,
0x630: 0x1EEB8,
0x631: 0x1EEB3,
0x632: 0x1EEA6,
0x633: 0x1EEAE,
0x634: 0x1EEB4,
0x635: 0x1EEB1,
0x636: 0x1EEB9,
0x637: 0x1EEA8,
0x638: 0x1EEBA,
0x639: 0x1EEAF,
0x63A: 0x1EEBB,
0x641: 0x1EEB0,
0x642: 0x1EEB2,
0x644: 0x1EEAB,
0x645: 0x1EEAC,
0x646: 0x1EEAD,
0x648: 0x1EEA5,
0x64A: 0x1EEA9
};
break;
default:
return codePoint;
}
return map[codePoint] ? map[codePoint] : codePoint;
}
// Greek
if (greekUpperAlpha <= codePoint && codePoint <= greekUpperOmega) {
baseChar = codePoint - greekUpperAlpha;
} else if (greekLowerAlpha <= codePoint && codePoint <= greekLowerOmega) {
baseChar = mathBoldSmallAlpha - mathBoldUpperAlpha + codePoint - greekLowerAlpha;
} else {
switch (codePoint) {
case greekUpperTheta:
baseChar = mathBoldUpperTheta - mathBoldUpperAlpha;
break;
case nabla:
baseChar = mathBoldNabla - mathBoldUpperAlpha;
break;
case partialDifferential:
baseChar = mathBoldPartialDifferential - mathBoldUpperAlpha;
break;
case greekLunateEpsilonSymbol:
baseChar = mathBoldEpsilonSymbol - mathBoldUpperAlpha;
break;
case greekThetaSymbol:
baseChar = mathBoldThetaSymbol - mathBoldUpperAlpha;
break;
case greekKappaSymbol:
baseChar = mathBoldKappaSymbol - mathBoldUpperAlpha;
break;
case greekPhiSymbol:
baseChar = mathBoldPhiSymbol - mathBoldUpperAlpha;
break;
case greekRhoSymbol:
baseChar = mathBoldRhoSymbol - mathBoldUpperAlpha;
break;
case greekPiSymbol:
baseChar = mathBoldPiSymbol - mathBoldUpperAlpha;
break;
default:
return codePoint;
}
}
switch (mathvariant) {
case Bold:
multiplier = 0;
break;
case Italic:
multiplier = 1;
break;
case BoldItalic:
multiplier = 2;
break;
case BoldSansSerif:
multiplier = 3;
break;
case SansSerifBoldItalic:
multiplier = 4;
break;
default:
// This mathvariant isn't defined for Greek or is otherwise normal.
return codePoint;
}
return baseChar + mathBoldUpperAlpha + multiplier * (mathItalicUpperAlpha - mathBoldUpperAlpha);
}
function applyMathVariant(aToken, aMathVariant) {
if (aMathVariant === "normal")
return;
var content = aToken["content"];
var transformedText = "";
for (var i = 0; i < content.length; i++) {
var c = content["codePointAt"](i);
if (c > 0xFFFF) {
transformedText += content[i]; i++;
transformedText += content[i];
} else {
transformedText += String["fromCodePoint"](
applyMathVariantToCharacter(c, aMathVariant)
);
}
}
aToken["content"] = transformedText;
}
function isToken(aTree) {
return ["mi", "mn", "mo", "mtext", "ms"].indexOf(aTree["tag"]) !== -1;
}
function isSingleCharMi(aTree) {
if (aTree["tag"] !== "mi")
return false;
var content = aTree["content"];
var c = content["codePointAt"](0);
return content.length === 1 && c <= 0xFFFF ||
content.length === 2 && c > 0xFFFF;
}
function isTokenAttribute(aAttribute) {
return ["mathcolor", "mathbackground", "mathvariant"].indexOf(aAttribute) !== -1;
}
function applyTokenAttributes(aChildren, aAttributes) {
var allAttributesAppliedToAllChildren = true;
for (var name in aAttributes) {
// Only consider mstyle attributes that apply to token elements.
if (!isTokenAttribute(name)) {
allAttributesAppliedToAllChildren = false;
continue;
}
// In general, keep mstyle element if there are multiple children.
if (name !== "mathvariant" && aChildren.length != 1) {
allAttributesAppliedToAllChildren = false;
continue;
}
aChildren.forEach(function(child) {
if (!isToken(child)) {
allAttributesAppliedToAllChildren = false;
return;
}
if (!child["attributes"])
child["attributes"] = {};
if (child["attributes"][name])
return;
if (name === "mathvariant") {
// Transform the text instead of using a mathvariant attribute.
// Explicit "normal" attribute is only needed on single-char <mi>'s.
if (aAttributes[name] !== "normal" || !isSingleCharMi(child))
applyMathVariant(child, aAttributes[name])
else
child["attributes"][name] = aAttributes[name];
} else {
// Apply the token attribute to the child.
child["attributes"][name] = aAttributes[name];
}
});
}
return allAttributesAppliedToAllChildren;
}
/* FIXME: try to restore the operator grouping when compoundTermList does not
contain any fences.
https://github.com/fred-wang/TeXZilla/issues/9 */
function newMrow(aChildren, aTag, aAttributes) {
aTag = aTag || "mrow";
if (aTag === "mstyle") {
// Mstyle with one mrow child that does not have any attribute.
if (aChildren.length == 1 &&
aChildren[0]["tag"] === "mrow" && !aChildren[0]["attributes"])
return newMrow(aChildren[0]["content"], aTag, aAttributes);
// Try an apply all the attributes to chidren and replace mstyle with mrow.
if (applyTokenAttributes(aChildren, aAttributes))
return newMrow(aChildren);
}
// Mrow with one child and no attributes: return the child.
if (aChildren.length == 1 && aTag === "mrow" && !aAttributes)
return aChildren[0];
return newTag(aTag, aChildren, aAttributes);
}
function newMath(aChildren, aDisplay, aRTL, aTeX)
{
return newTag("math", [
newTag("semantics", [
newMrow(aChildren),
newTag("annotation", escapeText(aTeX), {"encoding": "TeX"})
])
], {
"xmlns": MathMLNameSpace,
"display": aDisplay ? "block" : undefined,
"dir": aRTL ? "rtl" : undefined
});
}
function getTeXSourceInternal(aMathMLElement) {
var child;
if (!aMathMLElement ||
aMathMLElement.namespaceURI !== MathMLNameSpace) {
return null;
}
if (aMathMLElement.tagName === "semantics") {
// Note: we can't use aMathMLElement.children on WebKit/Blink because of
// https://bugs.webkit.org/show_bug.cgi?id=109556.
for (child = aMathMLElement.firstElementChild; child;
child = child.nextElementSibling) {
if (child.namespaceURI === MathMLNameSpace &&
child.localName === "annotation" &&
TeXMimeTypes.indexOf(child.getAttribute("encoding")) !== -1) {
return child.textContent;
}
}
} else if (aMathMLElement.childElementCount === 1) {
return getTeXSourceInternal(aMathMLElement.firstElementChild);
}
return null;
}
try {
// Try to create a DOM Parser object if it exists (e.g. in a Web page,
// in a chrome script running in a window etc)
parser.mDOMParser = new DOMParser();
} catch (e) {
// Make the DOMParser throw an exception if used.
parser.mDOMParser = {
parseFromString: function() {
throw "DOMParser undefined. Did you call TeXZilla.setDOMParser?";
}
};
}
parser.setDOMParser = function(aDOMParser)
{
this.mDOMParser = aDOMParser;
}
try {
// Try to create a XMLSerializer object if it exists (e.g. in a Web page,
// in a chrome script running in a window etc)
parser.mXMLSerializer = new XMLSerializer();
} catch (e) {
// Make the XMLSerializer throw an exception if used.
parser.mXMLSerializer = {
serializeToString: function() {
throw "XMLSerializer undefined. Did you call TeXZilla.setXMLSerializer?";
}
};
}
parser.setXMLSerializer = function(aXMLSerializer)
{
this.mXMLSerializer = aXMLSerializer;
}
parser.parseMathMLDocument = function (aString) {
// Parse the string into a MathML document and return the <math> root.
return this.mDOMParser.
parseFromString(aString, "application/xml").documentElement;
}
parser.setSafeMode = function(aEnable)
{
this.yy.mSafeMode = aEnable;
}
parser.setItexIdentifierMode = function(aEnable)
{
this.yy.mItexIdentifierMode = aEnable;
}
parser.getTeXSource = function(aMathMLElement) {
if (typeof aMathMLElement === "string") {
aMathMLElement = this.parseMathMLDocument(aMathMLElement);
}
return getTeXSourceInternal(aMathMLElement);
}
parser.toMathMLString = function(aTeX, aDisplay, aRTL, aThrowExceptionOnError) {
var output, mathml;
/* Parse the TeX source and get the main MathML node. */
try {
output = this.parse("\\(" + aTeX + "\\)");
if (aRTL) {
/* Set the RTL mode if specified. */
output = output.replace(/^<math/, "<math dir=\"rtl\"");
}
if (aDisplay) {
/* Set the display mode if it is specified. */
output = output.replace(/^<math/, "<math display=\"block\"");
}
} catch (e) {
if (aThrowExceptionOnError) {
throw e;
}
output = serializeTree(newMath(
[newTag("merror",
[newTag("mtext", escapeText(e.message))]
)],
aDisplay, aRTL, aTeX));
}
return output;
}
parser.toMathML = function(aTeX, aDisplay, aRTL, aThrowExceptionOnError) {
/* Parse the TeX string into a <math> element. */
return this.parseMathMLDocument(this.toMathMLString(aTeX, aDisplay, aRTL, aThrowExceptionOnError));
}
function escapeHTML(aString)
{
var rv = "", code1, code2;
for (var i = 0; i < aString.length; i++) {
var code1 = aString.charCodeAt(i);
if (code1 < 0x80) {
rv += aString.charAt(i);
continue;
}
if (0xD800 <= code1 && code1 <= 0xDBFF) {
i++;
code2 = aString.charCodeAt(i);
rv += "&#x" +
((code1-0xD800)*0x400 + code2-0xDC00 + 0x10000).toString(16) + ";";
continue;
}
rv += "&#x" + code1.toString(16) + ";";
}
return rv;
}
parser.toImage = function(aTeX, aRTL, aRoundToPowerOfTwo, aSize, aDocument) {
var math, el, box, svgWidth, svgHeight, svg, image;
// Set default values.
if (aSize === undefined) {
aSize = 64;
}
if (aDocument === undefined) {
aDocument = window.document;
}
// Create the MathML element.
math = this.toMathML(aTeX, true, aRTL);
math.setAttribute("mathsize", aSize + "px");
// Temporarily insert the MathML element in the document to measure it.
el = document.createElement("div");
el.style.visibility = "hidden";
el.style.position = "absolute";
el.appendChild(math);
aDocument.body.appendChild(el);
box = math.getBoundingClientRect();
aDocument.body.removeChild(el);
el.removeChild(math);
// Round up the computed sizes.
if (aRoundToPowerOfTwo) {
// Harmony's Math.log2() is not supported by all rendering engines and is
// removed by closure-compiler, so we use Math.log() / Math.LN2 instead.
svgWidth = Math.pow(2, Math.ceil(Math.log(box.width) / Math.LN2));
svgHeight = Math.pow(2, Math.ceil(Math.log(box.height) / Math.LN2));
} else {
svgWidth = Math.ceil(box.width);
svgHeight = Math.ceil(box.height);
}
// Embed the MathML in an SVG element.
svg = document.createElementNS(SVGNameSpace, "svg");
svg.setAttribute("width", svgWidth + "px");
svg.setAttribute("height", svgHeight + "px");
el = document.createElementNS(SVGNameSpace, "g");
el.setAttribute("transform", "translate(" +
(svgWidth - box.width) / 2.0 + "," + (svgHeight - box.height) / 2.0 + ")");
svg.appendChild(el);
el = document.createElementNS(SVGNameSpace, "foreignObject");
el.setAttribute("width", box.width);
el.setAttribute("height", box.height);
el.appendChild(math);
svg.firstChild.appendChild(el);
// Create the image element.
image = new Image();
image.src = "data:image/svg+xml;base64," +
window.btoa(escapeHTML(this.mXMLSerializer.serializeToString(svg)));
image.width = svgWidth;
image.height = svgHeight;
image.alt = escapeText(aTeX);
return image;
}
parser.filterString = function(aString, aThrowExceptionOnError) {
try {
return this.parse(aString);
} catch (e) {
if (aThrowExceptionOnError) {
throw e;
}
return aString;
}
}
parser.filterElement = function(aElement, aThrowExceptionOnError) {
var root, child, node;
for (var node = aElement.firstChild; node; node = node.nextSibling) {
switch(node.nodeType) {
case 1: // Node.ELEMENT_NODE
this.filterElement(node, aThrowExceptionOnError);
break;
case 3: // Node.TEXT_NODE
this.yy.escapeXML = true;
root = this.mDOMParser.parseFromString("<root>" +
TeXZilla.filterString(node.data, aThrowExceptionOnError) +
"</root>", "application/xml").documentElement;
this.yy.escapeXML = false;
while (child = root.firstChild) {
aElement.insertBefore(root.removeChild(child), node);
}
child = node.previousSibling;
aElement.removeChild(node); node = child;
break;
default:
}
}
}
function parseError(aString, aHash) {
// We delete the last line, which contains token names that are obscure
// to the users. See issue #16
throw new Error(aString.replace(/\nExpecting [^\n]*$/, "\n"));
}
%}
/* Operator associations and precedence. */
%left TEXOVER TEXATOP TEXCHOOSE
%right "^" "_" "OPP"
%start document
%%
/* text option argument */
textOptArg
: "[" TEXTOPTARG "]" {
/* Unescape \] and \\. */
$$ = $2.replace(/\\[\\\]]/g, function(match) { return match.slice(1); });
/* Escape some XML characters. */
$$ = escapeText($$);
}
;
/* text argument */
textArg
: "{" TEXTARG "}" {
/* Unescape \} and \\. */
$$ = $2.replace(/\\[\\\}]/g, function(match) { return match.slice(1); });
/* Escape some XML characters. */
$$ = escapeText($$);
}
;
/* length optional argument */
lengthOptArg
: "[" TEXTOPTARG "]" {
$$ = parseLength($2);
}
;
/* length argument */
lengthArg
: "{" TEXTARG "}" {
$$ = parseLength($2);
}
;
/* attribute optional argument */
attrOptArg
: textOptArg { $$ = escapeQuote($1); }
;
/* attribute argument */
attrArg
: textArg { $$ = escapeQuote($1); }
;
/* MathML token content */
tokenContent
: textArg {
/* The MathML specification indicates that trailing/leading whitespaces
should be removed and that inner whitespace should be collapsed. Let's
replace trailing/leading whitespace by no-break space so that people can
write e.g. \text{ if }. We also collapse internal whitespace here.
See https://github.com/fred-wang/TeXZilla/issues/25. */
$$ = $1.replace(/\s+/g, " ").replace(/^ | $/g, "\u00A0");
}
;
/* array alignment */
arrayAlign
: textOptArg {
$1 = $1.trim();
if ($1 === "t") {
$$ = "axis 1";
} else if ($1 === "c") {
$$ = "center";
} else if ($1 === "b") {
$$ = "axis -1";
} else {
throw "Unknown array alignment";
}
}
;
/* array column alignment */
columnAlign
: textArg {
$$ = "";
$1 = $1.replace(/\s+/g, "");;
for (var i = 0; i < $1.length; i++) {
if ($1[i] === "c") {
$$ += " center";
} else if ($1[i] === "l") {
$$ += " left";
} else if ($1[i] === "r") {
$$ += " right";
}
}
if ($$.length) {
$$ = $$.slice(1);
} else {
throw "Invalid column alignments";
}
}
;
/* table attributes */
collayout: COLLAYOUT attrArg { $$ = {"columnalign": $2}; };
colalign: COLALIGN attrArg { $$ = {"columnalign": $2}; };
rowalign: ROWALIGN attrArg { $$ = {"rowalign": $2}; };
rowspan: ROWSPAN attrArg { $$ = {"rowspan": $2}; };
colspan: COLSPAN attrArg { $$ = {"colspan": $2}; };
align: ALIGN attrArg { $$ = {"align": $2}; };
eqrows: EQROWS attrArg { $$ = {"equalrows": $2}; };
eqcols: EQCOLS attrArg { $$ = {"equalcolumns": $2}; };
rowlines: ROWLINES attrArg { $$ = {"rowlines": $2}; };
collines: COLLINES attrArg { $$ = {"columnlines": $2}; };
frame: FRAME attrArg { $$ = {"frame": $2}; };
padding: PADDING attrArg { $$ = {"rowspacing": $2, "columnspacing": $2}; };
/* cell option */
cellopt
: colalign { $$ = $1; }
| rowalign { $$ = $1; }
| rowspan { $$ = $1; }
| colspan { $$ = $1; }
;
/* list of cell options */
celloptList
: cellopt { $$ = $1; }
| celloptList cellopt { $$ = Object.assign($1, $2); }
;
/* row option */
rowopt
: colalign { $$ = $1; }
| rowalign { $$ = $1; }
;
/* array option */
arrayopt
: collayout { $$ = $1; }
| colalign { $$ = $1; }
| rowalign { $$ = $1; }
| align { $$ = $1; }
| eqrows { $$ = $1; }
| eqcols { $$ = $1; }
| rowlines { $$ = $1; }
| collines { $$ = $1; }
| frame { $$ = $1; }
| padding { $$ = $1; }
;
/* list of array options */
arrayoptList
: arrayopt { $$ = $1; }
| arrayoptList arrayopt { $$ = Object.assign($1, $2); }
;
/* list of row options */