| import * as mathmlHandlers from './mathml/index.js' |
| import { addScriptlevel } from './ooml/index.js' |
|
|
| export function walker( |
| element, |
| targetParent, |
| previousSibling = false, |
| nextSibling = false, |
| ancestors = [] |
| ) { |
| if ( |
| !previousSibling && |
| ['m:deg', 'm:den', 'm:e', 'm:fName', 'm:lim', 'm:num', 'm:sub', 'm:sup'].includes( |
| targetParent.name |
| ) |
| ) { |
| |
| |
| |
| |
| |
| |
| |
| addScriptlevel(targetParent, ancestors) |
| } |
| let targetElement |
| const nameOrType = element.name || element.type |
| if (mathmlHandlers[nameOrType]) { |
| targetElement = mathmlHandlers[nameOrType]( |
| element, |
| targetParent, |
| previousSibling, |
| nextSibling, |
| ancestors |
| ) |
| } else { |
| if (nameOrType && nameOrType !== 'root') { |
| console.warn(`Type not supported: ${nameOrType}`) |
| } |
|
|
| targetElement = targetParent |
| } |
|
|
| if (!targetElement) { |
| |
| return |
| } |
| if (element.children?.length) { |
| ancestors = [...ancestors] |
| ancestors.unshift(element) |
| |
| |
| |
| |
| let naryBodyTarget = null |
| |
| |
| let prescriptTarget = null |
| for (let i = 0; i < element.children.length; i++) { |
| const child = element.children[i] |
| if (child.skipInWalker) continue |
|
|
| |
| |
| |
| |
| |
| if (naryBodyTarget) { |
| if (child.name === 'mo') { |
| const txt = child.children?.[0]?.data |
| if (txt && /^[=<>≤≥≠≈≡∼≲≳≪≫∈∉⊂⊃⊆⊇⊄⊅≺≻⪯⪰∝≅≃≍≎∥⊥⊢⊣⊨⊩,;:∣]$/.test(txt)) { |
| naryBodyTarget = null |
| } |
| } else if (child.name === 'mtext') { |
| naryBodyTarget = null |
| } |
| } |
|
|
| const effectiveTarget = prescriptTarget || naryBodyTarget || targetElement |
| walker( |
| child, |
| effectiveTarget, |
| element.children[i - 1], |
| element.children[i + 1], |
| ancestors |
| ) |
| if (child.isNary) { |
| |
| const naryNode = effectiveTarget.children[effectiveTarget.children.length - 1] |
| naryBodyTarget = naryNode.children[naryNode.children.length - 1] |
| } |
| if (child.isPrescript) { |
| |
| const preNode = effectiveTarget.children[effectiveTarget.children.length - 1] |
| prescriptTarget = preNode.children[preNode.children.length - 1] |
| } else if (prescriptTarget) { |
| |
| prescriptTarget = null |
| } |
| } |
| } |
| } |
|
|