-
Notifications
You must be signed in to change notification settings - Fork 2
/
content-script.js
591 lines (534 loc) · 21.4 KB
/
content-script.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
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
NodeList.prototype.toArray = function () {
return Array.prototype.slice.call(this);
}
///// [Data Structures]
function Node(_type, _value, _children) {
this.type = _type || "";
this.value = _value || "";
this.children = _children || [];
}
Node.prototype.stringify = function (level) {
level = level || 0;
return " ".repeat(level) + this.type + ":" + this.value + "\n" + this.children.map(function (child) {
return child.stringify(level + 1);
}).reduce(function (string, sum) {
return string + sum;
}, "");
};
///// [End Data Structures]
///// [Templates]
// Actually, this could be a text node also -- code checks
function GetTreeFromElement(element) {
if (element.nodeName === "SPAN" && element.getAttribute("style") === "display:none") {
return new Node("row", "");
}
if (element.nodeType === 1 ) { // ELEMENT_NODEfor ba
// these check the className, and hence need to be elements
if (CheckSqrtTemplate(element))
return GetTreeFromSqrtElement(element);
if (CheckRadicTemplate(element))
return GetTreeFromRadicElement(element);
if (CheckFractionTemplate(element))
return GetTreeFromFractionTemplate(element);
if (CheckFractionTemplate2(element))
return GetTreeFromFractionTemplate2(element);
if (CheckDelimTemplate(element))
return GetTreeFromDelimTemplate(element);
// all of these assume we have an element (SPAN, SUB, SUP)
if (CheckIntegralTemplate(element))
return GetTreeFromIntegralTemplate(element);
if (CheckSubTemplate(element))
return GetTreeFromSubTemplate(element);
if (CheckSupTemplate(element))
return GetTreeFromSupTemplate(element);
if (CheckSuTemplate(element))
return GetTreeFromSuTemplate(element);
if (CheckOverTemplate(element))
return GetTreeFromOverTemplate(element);
if (CheckUnderTemplate(element))
return GetTreeFromUnderTemplate(element);
if (CheckVariableTemplate(element))
return GetTreeFromVariableTemplate(element);
// There are a few nodes such as the inline dy/dx in https://en.wikipedia.org/wiki/Calculus#Leibniz_notation
// that don't start with a span (in this case <style> or <link>) -- we don't know what they are so skip them
if (element.nodeName !== "SPAN") {
return new Node("row", "");
}
if (element.childNodes.length > 0) {
return CreateRowNode(element);
}
}
let pseudoScripts = String.fromCharCode(0x0022, 0x0027, 0x002A, 0x0060, 0x00AA,
0x00B0, 0x00B2, 0x00B3, 0x00B4, 0x00B9, 0x00BA, 0x2018, 0x2019, 0x201A, 0x201B,
0x201D, 0x201E, 0x201F, 0x2032, 0x2033, 0x2034, 0x2035, 0x2036, 0x2037, 0x2057);
// Parse text
let re = XRegExp("\\s*([0-9,\\.]+|\\pL+|[" + pseudoScripts + "]+|[^\\s])\\s*");
let tokens = [];
let m;
let pos = 0;
while (m = XRegExp.exec(element.textContent, re, pos, "sticky")) {
tokens.push(m[1]);
pos = m.index + m[0].length;
}
if (tokens.length === 1) {
if (XRegExp.test(tokens[0], XRegExp("[" + pseudoScripts + "]+"))) {
return new Node("sup", "", [new Node("text", tokens[0])]);
} else {
return new Node("text", tokens[0]);
}
} else {
return new Node("row", "", tokens.map(function (token) {
if (XRegExp.test(token, XRegExp("[" + pseudoScripts + "]+"))) {
return new Node("sup", "", [new Node("text", token)]);
} else {
return new Node("text", token);
}
}));
}
}
function CheckSqrtTemplate(element) {
return element.nodeName === "SPAN"
&& element.className === "nowrap"
&& element.childNodes.length === 2
&& element.childNodes[0].nodeName === "#text"
&& element.childNodes[0].data === String.fromCharCode(8730); //8730 is √
}
function CheckRadicTemplate(element) {
return element.nodeName === "SPAN"
&& element.className === "nowrap"
&& element.childNodes.length === 3
&& element.childNodes[1].nodeName === "#text"
&& element.childNodes[1].data === String.fromCharCode(8730); //8730 is √
}
function CheckFractionTemplate(element) {
return element.nodeName === "SPAN"
&& element.classList.contains("sfrac")
&& element.childNodes.length === 3;
}
function CheckFractionTemplate2(element) {
return element.nodeName === "SPAN"
&& element.classList.contains("frac")
&& element.childNodes.length === 3;
}
function CheckIntegralTemplate(element) {
let integralSymbols = [String.fromCharCode(8747), //8747 is ∫
String.fromCharCode(8748), //8748 is ∬
String.fromCharCode(8749), //8749 is ∭
String.fromCharCode(8750), //8750 is ∮
String.fromCharCode(8751), //8751 is ∯
String.fromCharCode(8752), //8752 is ∰
String.fromCharCode(8754), //8754 is ∲
String.fromCharCode(8755)]; //8755 is ∳
return element.nodeName === "SPAN"
&& element.childNodes.length === 2
&& element.childNodes[0].childNodes.length === 1
&& integralSymbols.includes(element.childNodes[0].childNodes[0].data);
}
function CheckSubTemplate(element) {
return element.nodeName === "SUB";
}
function CheckSupTemplate(element) {
return element.nodeName === "SUP";
}
function CheckSuTemplate(element) {
return element.nodeName === "SPAN" && (element.getAttribute("style") ===
"display:inline-block;margin-bottom:-0.3em;vertical-align:-0.4em;line-height:1.2em;font-size:80%;text-align:left"
|| element.getAttribute("style") ===
"display:inline-block;margin-bottom:-0.3em;vertical-align:-0.4em;line-height:1.2em;font-size:85%;text-align:right");
}
function CheckOverTemplate(element) {
return element.nodeName === "SPAN" && element.getAttribute("style") ===
"position:relative; margin-right:-0.75em; right:0.75em; bottom:0.75em;;";
}
function CheckUnderTemplate(element) {
return element.nodeName === "SPAN" && element.getAttribute("style") ===
"position:relative; margin-right:-0.75em; right:0.75em; top:0.45em;;";
}
function CheckVariableTemplate(element) {
return (element.nodeName === "I"
|| (element.nodeName === "SPAN"
&& element.getAttribute("style") === "font-style:italic;"))
&& element.childNodes.length === 1
&& element.childNodes[0].nodeName === "#text";
}
function CheckDelimTemplate(element) {
return element.classList.contains("sfrac")
&& element.childNodes.length >= 4;
}
function GetTreeFromSqrtElement(element) {
return new Node("sqrt", "", [GetTreeFromElement(element.childNodes[1])]);
}
function GetTreeFromRadicElement(element) {
return new Node("radic", "", [GetTreeFromElement(element.childNodes[2]), GetTreeFromElement(element.childNodes[0].childNodes[0])]);
}
function GetTreeFromFractionTemplate(element) {
if (element.childNodes[1].childNodes[0].data === "/") {
return new Node("frac", "", [GetTreeFromElement(element.childNodes[0]),
GetTreeFromElement(element.childNodes[2])]);
} else {
return new Node("row", "", [GetTreeFromElement(element.childNodes[0]), new Node("text", "+"), new Node("frac", "", [
GetTreeFromElement(element.childNodes[2].childNodes[0]), GetTreeFromElement(element.childNodes[2].childNodes[2])])]);
}
}
function GetTreeFromFractionTemplate2(element) {
return new Node("frac", "", [GetTreeFromElement(element.childNodes[0].childNodes[0]),
GetTreeFromElement(element.childNodes[2].childNodes[0])]);
}
function GetTreeFromIntegralTemplate(element) {
let elementData = element.childNodes[1].childNodes.toArray();
let brIndex = elementData.length;
for (let i = 0; i < elementData.length; ++i) {
if (elementData[i].nodeName === "BR") {
brIndex = i;
break;
}
}
let upper = elementData.slice(0, brIndex);
let lower = elementData.slice(brIndex + 1);
let upperElement = new Node("row", "");
if (upper.length > 1) {
upperElement = new Node("row", "", upper.map(GetTreeFromElement));
} else if (upper.length === 1) {
upperElement = GetTreeFromElement(upper[0]);
}
let lowerElement = new Node("row", "");
if (lower.length > 1) {
lowerElement = new Node("row", "", lower.map(GetTreeFromElement));
} else if (lower.length === 1) {
lowerElement = GetTreeFromElement(lower[0]);
}
let children = [];
if (lowerElement)
children.push(lowerElement);
if (upperElement)
children.push(upperElement);
return new Node("integral", element.childNodes[0].childNodes[0].data, children);
}
function GetTreeFromSubTemplate(element) {
return new Node("sub", "", [CreateRowNode(element)]);
}
function GetTreeFromSupTemplate(element) {
return new Node("sup", "", [CreateRowNode(element)]);
}
function GetTreeFromSuTemplate(element) {
return new Node("su", "", [GetTreeFromElement(element.childNodes[2]), GetTreeFromElement(element.childNodes[0])]);
}
function GetTreeFromOverTemplate(element) {
return new Node("sup", "2", [CreateRowNode(element)]);
}
function GetTreeFromUnderTemplate(element) {
return new Node("sub", "2", [CreateRowNode(element)]);
}
function GetTreeFromVariableTemplate(element) {
if (element.childNodes[0].data.length === 1) {
return new Node("text", element.childNodes[0].data);
} else {
return new Node("row", "", element.childNodes[0].data.split("").map(function (char) {
return new Node("text", char);
}));
}
}
function GetTreeFromDelimTemplate(element) {
let leftType = "(", rightType = ")";
switch (element.childNodes[1].childNodes[0].childNodes[0].childNodes[0].data) {
case String.fromCharCode(9121): // 9121 is ⎡ (left square bracket upper corner)
leftType = "[";
break;
case String.fromCharCode(9474): // 9474 is │ (box drawings light vertical)
leftType = "|";
break;
case String.fromCharCode(9553): // 9553 is ║ (box drawings double vertical)
leftType = String.fromCharCode(8214); // 8214 is ‖ (double vertical line)
break;
}
switch (element.childNodes[element.childNodes.length - 2].childNodes[0].childNodes[0].childNodes[0].data) {
case String.fromCharCode(9124): // 9124 is (right square bracket upper corner)
rightType = "]";
break;
case String.fromCharCode(9474): // 9474 is │ (box drawings light vertical)
rightType = "|";
break;
case String.fromCharCode(9553): // 9553 is ║ (box drawings double vertical)
rightType = String.fromCharCode(8214); // 8214 is ‖ (double vertical line)
break;
}
let newNode = new Node("fence", leftType + rightType);
for (let i = 2; i < element.childNodes.length - 2; i++) {
newNode.children.push(GetTreeFromElement(element.childNodes[i]));
}
return newNode;
}
function CreateRowNode(element) {
if (element.childNodes.length === 1) {
return GetTreeFromElement(element.childNodes[0]);
} else {
return new Node("row", "", element.childNodes.toArray().map(GetTreeFromElement));
}
}
///// [End Templates]
///// [Post Processing]
function TreePostProcessing(node) {
RemoveNestedRows(node);
MatchFences(node);
FixSubSup(node);
}
function RemoveNestedRows(node) {
if (node.type === "row") {
while (node.children.some(function (child) {
return child.type === "row";
})) {
node.children = [].concat.apply([], node.children.map(function (child) {
if (child.type === "row") {
return child.children;
} else {
return [child];
}
}));
}
}
node.children.forEach(RemoveNestedRows);
}
function MatchFences(node) {
let openFences = ["(", "[", "{"];
let closeFences = [")", "]", "}"];
while (node.children.some(function (child) {
return child.type === "text" && openFences.includes(child.value);
})) {
let lastFencePos = -1;
let lastFenceType = "";
for (let i = 0; i < node.children.length; i++) {
let child = node.children[i];
if (child.type === "text" && openFences.includes(child.value)) {
lastFencePos = i;
lastFenceType = child.value;
} else if (child.type === "text" && closeFences.includes(child.value) && lastFencePos !== -1) {
let newNode = new Node("fence", lastFenceType + child.value, node.children.splice(lastFencePos, i - lastFencePos + 1).slice(1, -1));
node.children.splice(lastFencePos, 0, newNode);
break;
}
}
}
node.children.forEach(MatchFences);
}
function FixSubSup(node) {
if (node.type === "sub" || node.type === "sup") {
if (node.children.length < 2) {
node.type = "row";
}
} else if (node.type === "su") {
if (node.children.length < 3) {
node.type = "row";
}
} else {
for (let i = 1; i < node.children.length; i++) {
let child = node.children[i];
if (child.type === "sub" || child.type === "sup") {
if (child.children.length === 1) {
i--;
child.children.unshift(node.children.splice(i, 1)[0]);
}
} else if (child.type === "su") {
if (child.children.length === 2) {
i--;
child.children.unshift(node.children.splice(i, 1)[0]);
}
}
}
}
node.children.forEach(FixSubSup);
}
///// [End Post Processing]
function TreeToMathML(node) {
let children = node.children.map(TreeToMathML);
let elem = undefined;
switch (node.type) {
case "row":
elem = document.createElement("mrow");
for (let c of children) {
elem.appendChild(c);
}
return elem;
case "sqrt":
elem = document.createElement("msqrt");
for (let c of children) {
elem.appendChild(c);
}
return elem;
case "radic":
elem = document.createElement("mroot");
for (let c of children) {
elem.appendChild(c);
}
return elem;
case "frac":
elem = document.createElement("mfrac");
for (let c of children) {
elem.appendChild(c);
}
return elem;
case "integral":
elem = document.createElement("msubsup");
let mo = document.createElement("mo");
mo.appendChild(document.createTextNode(node.value));
elem.appendChild(mo);
for (let c of children) {
elem.appendChild(c);
}
return elem;
case "sub":
if (node.value === "2") {
elem = document.createElement("munder");
for (let c of children) {
elem.appendChild(c);
}
return elem;
} else {
elem = document.createElement("msub");
for (let c of children) {
elem.appendChild(c);
}
return elem;
}
case "sup":
if (node.value === "2") {
elem = document.createElement("mover");
for (let c of children) {
elem.appendChild(c);
}
return elem;
} else {
elem = document.createElement("msup");
for (let c of children) {
elem.appendChild(c);
}
return elem;
}
case "su":
elem = document.createElement("msubsup");
for (let c of children) {
elem.appendChild(c);
}
return elem;
case "fence":
if (node.children.length === 1) {
elem = document.createElement("mrow");
let mo1 = document.createElement("mo");
mo1.appendChild(document.createTextNode(node.value.charAt(0)));
let mo2 = document.createElement("mo");
mo2.appendChild(document.createTextNode(node.value.charAt(1)));
elem.appendChild(mo1);
for (let c of children) {
elem.appendChild(c);
}
elem.appendChild(mo2);
return elem;
} else {
let elem1 = document.createElement("mrow");
let elem2 = document.createElement("mrow");
let mo1 = document.createElement("mo");
mo1.appendChild(document.createTextNode(node.value.charAt(0)));
let mo2 = document.createElement("mo");
mo2.appendChild(document.createTextNode(node.value.charAt(1)));
elem1.appendChild(mo1);
for (let c of children) {
elem2.appendChild(c);
}
elem1.appendChild(elem2);
elem1.appendChild(mo2);
return elem1;
}
case "text":
if (XRegExp.test(node.value, XRegExp("\\pL+"))) {
elem = document.createElement("mi");
elem.appendChild(document.createTextNode(node.value));
return elem;
} else if ( !(node.value.length===1 && /[,\.]/.test(node.value)) && /[0-9,\.]+/.test(node.value) ) {
elem = document.createElement("mn");
elem.appendChild(document.createTextNode(node.value));
return elem;
} else {
elem = document.createElement("mo");
elem.appendChild(document.createTextNode(node.value));
return elem;
}
}
}
function GetMathMLFromElement(element) {
// Converts the span and its children into a MathML string
// Returns a string representing the MathML or an empty string
// if it can’t do the conversion
let node = GetTreeFromElement(element);
TreePostProcessing(node);
let mathml = TreeToMathML(node);
if (element.className.includes("mathcal")) {
style = document.createElement("mstyle");
style.setAttribute("mathvariant", "script");
style.appendChild(mathml);
mathml = style;
}
let math = document.createElement("math");
math.setAttribute("xmlns", "http://www.w3.org/1998/Math/MathML");
// setting the role shouldn't be needed, but for some reason Chrome is picking up that this is math
math.setAttribute("role", "math");
math.appendChild(mathml);
return math;
}
function CreateInvisibleMathMLNode(mathmlText) {
// Creates a span node with attrs
// class="mwe-math-mathml-inline mwe-math-mathml-a11y"
// style="display: none;"}
// The child of the span is mathml string converted to XML
// Returns a span element
let spanElement = document.createElement("SPAN");
spanElement.setAttribute("class", "mwe-math-mathml-inline mwe-math-mathml-a11y");
spanElement.setAttribute("style", "display: none;");
spanElement.appendChild(mathmlText);
return spanElement;
}
function FindMathElements(node) {
// Find the inline math items.
// Most seem to be inside of nodes with a class list that includes "texhtml"
if (node.nodeType !== 1) { // '1' is element
return;
}
let element = node; // just being clear that we are dealing with an element
let className = element.className;
if (element.getAttribute("role") == "math" ||
className && (
className.includes("texhtml") ||
className.includes("sfrac") ||
className.includes("frac") ||
className.includes("mathcal")
) ) {
let mathmlText = GetMathMLFromElement(element);
if (mathmlText) {
// replace element with a new element that have as the children the new MathML element and this element
// this mimics what is done for display math in Wikipedia
let wrapper = document.createElement("SPAN");
wrapper.setAttribute("class", "mwe-math-element");
wrapper.appendChild( CreateInvisibleMathMLNode(mathmlText) );
element.setAttribute("aria-hidden", "true");
element.replaceWith(wrapper);
wrapper.appendChild(element);
}
} else {
element.childNodes.toArray().forEach(FindMathElements);
}
}
function FindDisplayMath(doc) {
// Display math is (apparently for presentational purposes) inside of dl/dd
// This results in screen readers saying "list with one item" ... "out of list"
// We add role="presentation" which doesn't change display but tells AT this isn't a list.
let displayMath = doc.querySelectorAll("dl>dd>span.mwe-math-element");
displayMath.forEach((span) => {
let dd = span.parentElement;
dd.setAttribute("role", "presentation");
dd.parentElement.setAttribute("role", "presentation");
})
}
try {
FindMathElements(document.body);
FindDisplayMath(document.body);
} catch (e) {
console.error(e);
}