Skip to content

Commit a056dbe

Browse files
authored
handle generated assistive mml (#88)
* handle generated assistive mml * version bump
1 parent 9ff9e3f commit a056dbe

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openstax/highlighter",
3-
"version": "1.16.2",
3+
"version": "1.16.3",
44
"main": "dist/index.js",
55
"license": "MIT",
66
"files": [

src/injectHighlightWrappers.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,21 @@ function refineRangeBoundaries(range: Range) {
277277
return mathjax;
278278
}
279279

280+
// MathJax 4: <mjx-assistive-mml> is inside <mjx-container>
281+
const assistiveMml = dom(node).farthest('mjx-assistive-mml');
282+
if (assistiveMml) {
283+
const container = dom(assistiveMml).farthest('mjx-container');
284+
if (container) {
285+
return container;
286+
}
287+
}
288+
289+
// Legacy: <script type="math/mml"> after the MathJax container
280290
const mml = dom(node).farthest('script[type="math/mml"]');
281-
if (mml && mml.previousSibling.matches('.MathJax,mjx-container')) {
291+
if (mml && mml.previousSibling && mml.previousSibling.matches('.MathJax')) {
282292
return mml.previousSibling;
283293
}
284-
if (mml && mml.previousSibling.matches('.MathJax_Display')) {
294+
if (mml && mml.previousSibling && mml.previousSibling.matches('.MathJax_Display')) {
285295
return mml.previousSibling.querySelector('.MathJax');
286296
}
287297

src/rangeContents.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ export const rangeContentsString = (range: Range): string => {
77
const removeAll = (nodes: NodeListOf<Element>) => nodes.forEach((element: Element) => element.remove());
88

99
container.appendChild(fragment);
10-
// REX creates math/mml script tags inside mjx-container elements for MathJax 4.
11-
// Timing issues prevent placing them outside like MathJax 2 used to do,
12-
// so we need to move them to match the MathJax 2 location.
10+
11+
// handle mml math nodes from mathjax 4
1312
const mjxContainers = container.querySelectorAll('mjx-container');
1413
mjxContainers.forEach((mjxContainer: Element) => {
15-
const scriptTag = mjxContainer.querySelector('script[type="math/mml"]');
16-
if (scriptTag && mjxContainer.parentElement) {
17-
mjxContainer.parentElement.insertBefore(scriptTag, mjxContainer);
14+
const mathTag = mjxContainer.querySelector('mjx-assistive-mml math');
15+
if (mathTag && mjxContainer.parentElement) {
16+
mjxContainer.parentElement.insertBefore(mathTag, mjxContainer);
1817
}
1918
});
2019

21-
removeAll(container.querySelectorAll('.MathJax'));
20+
removeAll(container.querySelectorAll('.MathJax,mjx-container'));
2221
removeAll(container.querySelectorAll('.MathJax_Display'));
2322
removeAll(container.querySelectorAll('.MathJax_Preview'));
2423
removeAll(container.querySelectorAll('.MJX_Assistive_MathML'));

0 commit comments

Comments
 (0)