-
Notifications
You must be signed in to change notification settings - Fork 54
/
mathml-fragments.js
181 lines (173 loc) · 5.62 KB
/
mathml-fragments.js
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
var MathMLFragments = {
"annotation": "\
<semantics>\
<mrow></mrow>\
<annotation class='element text-container'></annotation>\
</semantics>",
"annotation-xml": "\
<semantics>\
<mrow></mrow>\
<annotation-xml class='element text-container foreign-container'></annotation-xml>\
</semantics>",
"maction": "\
<maction class='element' actiontype='statusline'>\
<mrow class='mathml-container'></mrow>\
<mtext class='text-container'></mtext>\
</maction>",
"menclose": "<menclose class='element mathml-container'></menclose>",
"merror": "<merror class='element mathml-container'></merror>",
"mfrac": "\
<mfrac class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</mfrac>",
"mi": "<mi class='element text-container foreign-container'></mi>",
"mmultiscripts": "\
<mmultiscripts class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</mmultiscripts>",
"mn": "<mn class='element text-container foreign-container'></mn>",
"mo": "<mo class='element text-container foreign-container'></mo>",
"mover": "\
<mover class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</mover>",
"mpadded": "<mpadded class='element mathml-container'></mpadded>",
"mphantom": "<mphantom class='element mathml-container'></mphantom>",
"mprescripts": "\
<mmultiscripts>\
<mrow class='mathml-container'></mrow>\
<mprescripts class='element'/>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</mmultiscripts>",
"mroot": "\
<mroot class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</mroot>",
"mrow": "<mrow class='element mathml-container'></mrow>",
"ms": "<ms class='element text-container foreign-container'></ms>",
"mspace": "<mspace class='element'></mspace>",
"msqrt": "<msqrt class='element mathml-container'></msqrt>",
"mstyle": "<mstyle class='element mathml-container'></mstyle>",
"msub": "\
<msub class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</msub>",
"msubsup": "\
<msubsup class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</msubsup>",
"msup": "\
<msup class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</msup>",
"mtable": "\
<mtable class='element'>\
<mtr>\
<mtd class='mathml-container'>\
</mtd>\
</mtr>\
</mtable>",
"mtd": "\
<mtable>\
<mtr>\
<mtd class='element mathml-container'>\
</mtd>\
</mtr>\
</mtable>",
"mtext": "<mtext class='element text-container foreign-container'></mtext>",
"mtr": "\
<mtable>\
<mtr class='element'>\
<mtd class='mathml-container'>\
</mtd>\
</mtr>\
</mtable>",
"munder": "\
<munder class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</munder>",
"munderover": "\
<munderover class='element'>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
<mrow class='mathml-container'></mrow>\
</munderover>",
"none": "\
<mmultiscripts>\
<mrow class='mathml-container'></mrow>\
<none class='element'/>\
<mrow class='mathml-container'></mrow>\
</mmultiscripts>",
"semantics": "\
<semantics class='element'>\
<mrow class='mathml-container'></mrow>\
<annotation class='text-container'></annotation>\
</semantics>"
};
var FragmentHelper = {
mathml_namespace: "http://www.w3.org/1998/Math/MathML",
createElement: function(tag) {
return document.createElementNS(this.mathml_namespace, tag);
},
isValidChildOfMrow: function(tag) {
return !(tag == "annotation" ||
tag == "annotation-xml" ||
tag == "mprescripts" ||
tag == "none" ||
tag == "mtr" ||
tag == "mtd");
},
isEmpty: function(tag) {
return tag === "mspace" || tag == "mprescripts" || tag == "none";
},
element: function(fragment) {
return fragment.getElementsByClassName('element')[0];
},
appendChild: function(fragment, allowInvalid) {
var element = this.element(fragment) || fragment;
if (element.classList.contains("foreign-container")) {
var el = document.createElement("span");
el.textContent = "a";
return element.appendChild(el);
}
if (element.classList.contains("mathml-container") || allowInvalid) {
var el = this.createElement("mi");
el.textContent = "a";
return element.appendChild(el);
}
throw "Cannot append child to the element";
},
forceNonEmptyElement: function(fragment) {
var element = this.element(fragment) || fragment;
if (element.firstElementChild)
return element.firstElementChild;
return this.appendChild(fragment);
},
forceNonEmptyDescendants: function(fragment) {
var element = this.element(fragment) || fragment;
if (element.classList.contains("mathml-container") ||
element.classList.contains("foreign-container")) {
for (var i = 0; i < 10; i++)
this.appendChild(element);
return;
}
var child = element.firstElementChild;
if (child) {
for (; child; child = child.nextElementSibling) {
this.forceNonEmptyDescendants(child);
}
return;
}
},
}