diff --git a/demos/html/viewer/index.html b/demos/html/viewer/index.html index a8255a667..e97a8cd9d 100644 --- a/demos/html/viewer/index.html +++ b/demos/html/viewer/index.html @@ -46,22 +46,22 @@ MathType Viewer version:
-
-
-
- @@ -70,23 +70,23 @@
-
-
- 96
- 1
diff --git a/packages/viewer/src/mathml.ts b/packages/viewer/src/mathml.ts index 623632cfd..b5e927e50 100644 --- a/packages/viewer/src/mathml.ts +++ b/packages/viewer/src/mathml.ts @@ -1,6 +1,7 @@ import { Properties } from "./properties"; import { showImage, createImage, mathml2accessible, processJsonResponse } from './services'; import { htmlEntitiesToXmlEntities, corruptMathML } from './utils'; +import MathML from '@wiris/mathtype-html-integration-devkit/src/mathml'; /** * Data obtained when rendering image. Data needed to set the formula image parameters. @@ -12,6 +13,43 @@ interface FormulaData { width: string, } +/** + * Look for safe MathML «math» formulas. + * @param {HTMLElement} root - Any DOM element that can contain MathML. + */ +function findSafeMathMLTextNodes(root: HTMLElement): Node[] { + const nodeIterator: NodeIterator = document.createNodeIterator( + root, + NodeFilter.SHOW_TEXT, + node => /«math(.*?)«\/math»/g.test(node.nodeValue || '') ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT + ); + const safeNodes : Node[] = []; + + let currentNode: Node | null; + while (currentNode = nodeIterator.nextNode()) { + safeNodes.push(currentNode); + } + + return safeNodes; +} + +/** + * Parse the DOM looking for «math» formulas and replace them with the corresponding rendered images within the given element. + * @param {HTMLElement} root - Any DOM element that can contain MathML. + */ +function decodeSafeMathML(root: HTMLElement) { + const safeNodes = findSafeMathMLTextNodes(root); + + for (const safeNode of safeNodes) { + const mathml = MathML.safeXmlDecode(safeNode.textContent); + // Insert mathml node. + const fragment = document.createRange().createContextualFragment(mathml); + + safeNode.parentNode?.insertBefore(fragment, safeNode); + safeNode.parentNode?.removeChild(safeNode); + } +} + /** * Parse the DOM looking for elements and replace them with the corresponding rendered images within the given element. * @param {Properties} properties - Properties of the viewer. @@ -23,6 +61,8 @@ export async function renderMathML(properties: Properties, root: HTMLElement): P return; } + decodeSafeMathML(root); + for(const mathElement of [...root.getElementsByTagName('math')]) { const mml = htmlEntitiesToXmlEntities(mathElement.outerHTML);