enrich_mathml.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.enrich = enrich;
  4. exports.walkTree = walkTree;
  5. exports.introduceNewLayer = introduceNewLayer;
  6. exports.ascendNewNode = ascendNewNode;
  7. exports.addCollapsedAttribute = addCollapsedAttribute;
  8. exports.cloneContentNode = cloneContentNode;
  9. exports.rewriteMfenced = rewriteMfenced;
  10. exports.setOperatorAttribute = setOperatorAttribute;
  11. exports.getInnerNode = getInnerNode;
  12. exports.collapsePunctuated = collapsePunctuated;
  13. const debugger_js_1 = require("../common/debugger.js");
  14. const DomUtil = require("../common/dom_util.js");
  15. const engine_js_1 = require("../common/engine.js");
  16. const semantic_attr_js_1 = require("../semantic_tree/semantic_attr.js");
  17. const semantic_meaning_js_1 = require("../semantic_tree/semantic_meaning.js");
  18. const semantic_heuristic_factory_js_1 = require("../semantic_tree/semantic_heuristic_factory.js");
  19. const semantic_skeleton_js_1 = require("../semantic_tree/semantic_skeleton.js");
  20. const SemanticUtil = require("../semantic_tree/semantic_util.js");
  21. const semantic_util_js_1 = require("../semantic_tree/semantic_util.js");
  22. const EnrichAttr = require("./enrich_attr.js");
  23. const enrich_case_js_1 = require("./enrich_case.js");
  24. const SETTINGS = {
  25. collapsed: true,
  26. implicit: true,
  27. wiki: true
  28. };
  29. const IDS = new Map();
  30. function enrich(mml, semantic) {
  31. IDS.clear();
  32. const oldMml = DomUtil.cloneNode(mml);
  33. walkTree(semantic.root);
  34. if (engine_js_1.Engine.getInstance().structure) {
  35. mml.setAttribute(EnrichAttr.Attribute.STRUCTURE, semantic_skeleton_js_1.SemanticSkeleton.fromStructure(mml, semantic).toString());
  36. }
  37. debugger_js_1.Debugger.getInstance().generateOutput(() => [
  38. formattedOutput(oldMml, 'Original MathML', SETTINGS.wiki),
  39. formattedOutput(semantic, 'Semantic Tree', SETTINGS.wiki),
  40. formattedOutput(mml, 'Semantically enriched MathML', SETTINGS.wiki)
  41. ]);
  42. return mml;
  43. }
  44. function walkTree(semantic) {
  45. debugger_js_1.Debugger.getInstance().output('WALKING START: ' + semantic.toString());
  46. const specialCase = (0, enrich_case_js_1.getCase)(semantic);
  47. let newNode;
  48. if (specialCase) {
  49. newNode = specialCase.getMathml();
  50. debugger_js_1.Debugger.getInstance().output('WALKING END: ' + semantic.toString());
  51. return ascendNewNode(newNode);
  52. }
  53. if (semantic.mathml.length === 1) {
  54. debugger_js_1.Debugger.getInstance().output('Walktree Case 0');
  55. if (!semantic.childNodes.length) {
  56. debugger_js_1.Debugger.getInstance().output('Walktree Case 0.1');
  57. newNode = semantic.mathml[0];
  58. EnrichAttr.setAttributes(newNode, semantic);
  59. debugger_js_1.Debugger.getInstance().output('WALKING END: ' + semantic.toString());
  60. return ascendNewNode(newNode);
  61. }
  62. const fchild = semantic.childNodes[0];
  63. if (semantic.childNodes.length === 1 &&
  64. fchild.type === semantic_meaning_js_1.SemanticType.EMPTY) {
  65. debugger_js_1.Debugger.getInstance().output('Walktree Case 0.2');
  66. newNode = semantic.mathml[0];
  67. EnrichAttr.setAttributes(newNode, semantic);
  68. newNode.appendChild(walkTree(fchild));
  69. debugger_js_1.Debugger.getInstance().output('WALKING END: ' + semantic.toString());
  70. return ascendNewNode(newNode);
  71. }
  72. semantic.childNodes.forEach((child) => {
  73. if (!child.mathml.length) {
  74. child.mathml = [createInvisibleOperator(child)];
  75. }
  76. });
  77. }
  78. const newContent = semantic.contentNodes.map(cloneContentNode);
  79. setOperatorAttribute(semantic, newContent);
  80. const newChildren = semantic.childNodes.map(walkTree);
  81. const childrenList = semantic_skeleton_js_1.SemanticSkeleton.combineContentChildren(semantic.type, semantic.role, newContent, newChildren);
  82. newNode = semantic.mathmlTree;
  83. if (newNode === null) {
  84. debugger_js_1.Debugger.getInstance().output('Walktree Case 1');
  85. newNode = introduceNewLayer(childrenList, semantic);
  86. }
  87. else {
  88. const attached = attachedElement(childrenList);
  89. debugger_js_1.Debugger.getInstance().output('Walktree Case 2');
  90. if (attached) {
  91. debugger_js_1.Debugger.getInstance().output('Walktree Case 2.1');
  92. newNode = parentNode(attached);
  93. }
  94. else {
  95. debugger_js_1.Debugger.getInstance().output('Walktree Case 2.2');
  96. newNode = getInnerNode(newNode);
  97. }
  98. }
  99. newNode = rewriteMfenced(newNode);
  100. mergeChildren(newNode, childrenList, semantic);
  101. if (!IDS.has(semantic.id)) {
  102. IDS.set(semantic.id, true);
  103. EnrichAttr.setAttributes(newNode, semantic);
  104. }
  105. debugger_js_1.Debugger.getInstance().output('WALKING END: ' + semantic.toString());
  106. return ascendNewNode(newNode);
  107. }
  108. function introduceNewLayer(children, semantic) {
  109. const lca = mathmlLca(children);
  110. let newNode = lca.node;
  111. const info = lca.type;
  112. if (info !== lcaType.VALID ||
  113. !SemanticUtil.hasEmptyTag(newNode) ||
  114. (!newNode.parentNode && semantic.parent)) {
  115. debugger_js_1.Debugger.getInstance().output('Walktree Case 1.1');
  116. newNode = EnrichAttr.addMrow();
  117. if (info === lcaType.PRUNED) {
  118. debugger_js_1.Debugger.getInstance().output('Walktree Case 1.1.0');
  119. newNode = introduceLayerAboveLca(newNode, lca.node, children);
  120. }
  121. else if (children[0]) {
  122. debugger_js_1.Debugger.getInstance().output('Walktree Case 1.1.1');
  123. const node = attachedElement(children);
  124. if (node) {
  125. const oldChildren = childrenSubset(parentNode(node), children);
  126. DomUtil.replaceNode(node, newNode);
  127. oldChildren.forEach(function (x) {
  128. newNode.appendChild(x);
  129. });
  130. }
  131. else {
  132. moveSemanticAttributes(newNode, children[0]);
  133. newNode = children[0];
  134. }
  135. }
  136. }
  137. if (!semantic.mathmlTree) {
  138. semantic.mathmlTree = newNode;
  139. }
  140. return newNode;
  141. }
  142. function introduceLayerAboveLca(mrow, lca, children) {
  143. let innerNode = descendNode(lca);
  144. if (SemanticUtil.hasMathTag(innerNode)) {
  145. debugger_js_1.Debugger.getInstance().output('Walktree Case 1.1.0.0');
  146. moveSemanticAttributes(innerNode, mrow);
  147. DomUtil.toArray(innerNode.childNodes).forEach(function (x) {
  148. mrow.appendChild(x);
  149. });
  150. const auxNode = mrow;
  151. mrow = innerNode;
  152. innerNode = auxNode;
  153. }
  154. const index = children.indexOf(lca);
  155. children[index] = innerNode;
  156. DomUtil.replaceNode(innerNode, mrow);
  157. mrow.appendChild(innerNode);
  158. children.forEach(function (x) {
  159. mrow.appendChild(x);
  160. });
  161. return mrow;
  162. }
  163. function moveSemanticAttributes(oldNode, newNode) {
  164. for (const attr of EnrichAttr.EnrichAttributes) {
  165. if (oldNode.hasAttribute(attr)) {
  166. newNode.setAttribute(attr, oldNode.getAttribute(attr));
  167. oldNode.removeAttribute(attr);
  168. }
  169. }
  170. }
  171. function childrenSubset(node, newChildren) {
  172. const oldChildren = DomUtil.toArray(node.childNodes);
  173. let leftIndex = +Infinity;
  174. let rightIndex = -Infinity;
  175. newChildren.forEach(function (child) {
  176. const index = oldChildren.indexOf(child);
  177. if (index !== -1) {
  178. leftIndex = Math.min(leftIndex, index);
  179. rightIndex = Math.max(rightIndex, index);
  180. }
  181. });
  182. return oldChildren.slice(leftIndex, rightIndex + 1);
  183. }
  184. function collateChildNodes(node, children, semantic) {
  185. const oldChildren = [];
  186. let newChildren = DomUtil.toArray(node.childNodes);
  187. let notFirst = false;
  188. while (newChildren.length) {
  189. const child = newChildren.shift();
  190. if (child.hasAttribute(EnrichAttr.Attribute.TYPE)) {
  191. oldChildren.push(child);
  192. continue;
  193. }
  194. const collect = collectChildNodes(child, children);
  195. if (collect.length === 0) {
  196. continue;
  197. }
  198. if (collect.length === 1) {
  199. oldChildren.push(child);
  200. continue;
  201. }
  202. if (notFirst) {
  203. child.setAttribute('AuxiliaryImplicit', true);
  204. }
  205. else {
  206. notFirst = true;
  207. }
  208. newChildren = collect.concat(newChildren);
  209. }
  210. const rear = [];
  211. const semChildren = semantic.childNodes.map(function (x) {
  212. return x.mathmlTree;
  213. });
  214. while (semChildren.length) {
  215. const schild = semChildren.pop();
  216. if (!schild) {
  217. continue;
  218. }
  219. if (oldChildren.indexOf(schild) !== -1) {
  220. break;
  221. }
  222. if (children.indexOf(schild) !== -1) {
  223. rear.unshift(schild);
  224. }
  225. }
  226. return oldChildren.concat(rear);
  227. }
  228. function collectChildNodes(node, children) {
  229. const collect = [];
  230. let newChildren = DomUtil.toArray(node.childNodes);
  231. while (newChildren.length) {
  232. const child = newChildren.shift();
  233. if (child.nodeType !== DomUtil.NodeType.ELEMENT_NODE) {
  234. continue;
  235. }
  236. if (child.hasAttribute(EnrichAttr.Attribute.TYPE) ||
  237. children.indexOf(child) !== -1) {
  238. collect.push(child);
  239. continue;
  240. }
  241. newChildren = DomUtil.toArray(child.childNodes).concat(newChildren);
  242. }
  243. return collect;
  244. }
  245. function mergeChildren(node, newChildren, semantic) {
  246. if (!newChildren.length)
  247. return;
  248. if (newChildren.length === 1 && node === newChildren[0])
  249. return;
  250. const oldChildren = semantic.role === semantic_meaning_js_1.SemanticRole.IMPLICIT &&
  251. semantic_heuristic_factory_js_1.SemanticHeuristics.flags.combine_juxtaposition
  252. ? collateChildNodes(node, newChildren, semantic)
  253. : DomUtil.toArray(node.childNodes);
  254. if (!oldChildren.length) {
  255. newChildren.forEach(function (x) {
  256. node.appendChild(x);
  257. });
  258. return;
  259. }
  260. let oldCounter = 0;
  261. while (newChildren.length) {
  262. const newChild = newChildren[0];
  263. if (oldChildren[oldCounter] === newChild ||
  264. functionApplication(oldChildren[oldCounter], newChild)) {
  265. newChildren.shift();
  266. oldCounter++;
  267. continue;
  268. }
  269. if (oldChildren[oldCounter] &&
  270. newChildren.indexOf(oldChildren[oldCounter]) === -1) {
  271. oldCounter++;
  272. continue;
  273. }
  274. if (isDescendant(newChild, node)) {
  275. newChildren.shift();
  276. continue;
  277. }
  278. const oldChild = oldChildren[oldCounter];
  279. if (!oldChild) {
  280. if (newChild.parentNode) {
  281. node = parentNode(newChild);
  282. newChildren.shift();
  283. continue;
  284. }
  285. const nextChild = newChildren[1];
  286. if (nextChild && nextChild.parentNode) {
  287. node = parentNode(nextChild);
  288. node.insertBefore(newChild, nextChild);
  289. newChildren.shift();
  290. newChildren.shift();
  291. continue;
  292. }
  293. node.insertBefore(newChild, null);
  294. newChildren.shift();
  295. continue;
  296. }
  297. insertNewChild(node, oldChild, newChild);
  298. newChildren.shift();
  299. }
  300. }
  301. function insertNewChild(node, oldChild, newChild) {
  302. let parent = oldChild;
  303. let next = parentNode(parent);
  304. while (next &&
  305. next.firstChild === parent &&
  306. !parent.hasAttribute('AuxiliaryImplicit') &&
  307. next !== node) {
  308. parent = next;
  309. next = parentNode(parent);
  310. }
  311. if (next) {
  312. next.insertBefore(newChild, parent);
  313. parent.removeAttribute('AuxiliaryImplicit');
  314. }
  315. }
  316. function isDescendant(child, node) {
  317. if (!child) {
  318. return false;
  319. }
  320. do {
  321. child = parentNode(child);
  322. if (child === node) {
  323. return true;
  324. }
  325. } while (child);
  326. return false;
  327. }
  328. function functionApplication(oldNode, newNode) {
  329. const appl = semantic_attr_js_1.NamedSymbol.functionApplication;
  330. if (oldNode &&
  331. newNode &&
  332. oldNode.textContent &&
  333. newNode.textContent &&
  334. oldNode.textContent === appl &&
  335. newNode.textContent === appl &&
  336. newNode.getAttribute(EnrichAttr.Attribute.ADDED) === 'true') {
  337. for (let i = 0, attr; (attr = oldNode.attributes[i]); i++) {
  338. if (!newNode.hasAttribute(attr.nodeName)) {
  339. newNode.setAttribute(attr.nodeName, attr.nodeValue);
  340. }
  341. }
  342. DomUtil.replaceNode(oldNode, newNode);
  343. return true;
  344. }
  345. return false;
  346. }
  347. var lcaType;
  348. (function (lcaType) {
  349. lcaType["VALID"] = "valid";
  350. lcaType["INVALID"] = "invalid";
  351. lcaType["PRUNED"] = "pruned";
  352. })(lcaType || (lcaType = {}));
  353. function mathmlLca(children) {
  354. const leftMost = attachedElement(children);
  355. if (!leftMost) {
  356. return { type: lcaType.INVALID, node: null };
  357. }
  358. const rightMost = attachedElement(children.slice().reverse());
  359. if (leftMost === rightMost) {
  360. return { type: lcaType.VALID, node: leftMost };
  361. }
  362. const leftPath = pathToRoot(leftMost);
  363. const newLeftPath = prunePath(leftPath, children);
  364. const rightPath = pathToRoot(rightMost, function (x) {
  365. return newLeftPath.indexOf(x) !== -1;
  366. });
  367. const lca = rightPath[0];
  368. const lIndex = newLeftPath.indexOf(lca);
  369. if (lIndex === -1) {
  370. return { type: lcaType.INVALID, node: null };
  371. }
  372. return {
  373. type: newLeftPath.length !== leftPath.length
  374. ? lcaType.PRUNED
  375. : validLca(newLeftPath[lIndex + 1], rightPath[1])
  376. ? lcaType.VALID
  377. : lcaType.INVALID,
  378. node: lca
  379. };
  380. }
  381. function prunePath(path, children) {
  382. let i = 0;
  383. while (path[i] && children.indexOf(path[i]) === -1) {
  384. i++;
  385. }
  386. return path.slice(0, i + 1);
  387. }
  388. function attachedElement(nodes) {
  389. let count = 0;
  390. let attached = null;
  391. while (!attached && count < nodes.length) {
  392. if (nodes[count].parentNode) {
  393. attached = nodes[count];
  394. }
  395. count++;
  396. }
  397. return attached;
  398. }
  399. function pathToRoot(node, opt_test) {
  400. const test = opt_test || ((_x) => false);
  401. const path = [node];
  402. while (!test(node) && !SemanticUtil.hasMathTag(node) && node.parentNode) {
  403. node = parentNode(node);
  404. path.unshift(node);
  405. }
  406. return path;
  407. }
  408. function validLca(left, right) {
  409. return !!(left && right && !left.previousSibling && !right.nextSibling);
  410. }
  411. function ascendNewNode(newNode) {
  412. while (!SemanticUtil.hasMathTag(newNode) && unitChild(newNode)) {
  413. newNode = parentNode(newNode);
  414. }
  415. return newNode;
  416. }
  417. function descendNode(node) {
  418. const children = DomUtil.toArray(node.childNodes);
  419. if (!children) {
  420. return node;
  421. }
  422. const remainder = children.filter(function (child) {
  423. return (child.nodeType === DomUtil.NodeType.ELEMENT_NODE &&
  424. !SemanticUtil.hasIgnoreTag(child));
  425. });
  426. if (remainder.length === 1 &&
  427. SemanticUtil.hasEmptyTag(remainder[0]) &&
  428. !remainder[0].hasAttribute(EnrichAttr.Attribute.TYPE)) {
  429. return descendNode(remainder[0]);
  430. }
  431. return node;
  432. }
  433. function unitChild(node) {
  434. const parent = parentNode(node);
  435. if (!parent || !SemanticUtil.hasEmptyTag(parent)) {
  436. return false;
  437. }
  438. return DomUtil.toArray(parent.childNodes).every(function (child) {
  439. return child === node || isIgnorable(child);
  440. });
  441. }
  442. function isIgnorable(node) {
  443. if (node.nodeType !== DomUtil.NodeType.ELEMENT_NODE) {
  444. return true;
  445. }
  446. if (!node || SemanticUtil.hasIgnoreTag(node)) {
  447. return true;
  448. }
  449. const children = DomUtil.toArray(node.childNodes);
  450. if ((!SemanticUtil.hasEmptyTag(node) && children.length) ||
  451. SemanticUtil.hasDisplayTag(node) ||
  452. node.hasAttribute(EnrichAttr.Attribute.TYPE) ||
  453. SemanticUtil.isOrphanedGlyph(node)) {
  454. return false;
  455. }
  456. return DomUtil.toArray(node.childNodes).every(isIgnorable);
  457. }
  458. function parentNode(element) {
  459. return element.parentNode;
  460. }
  461. function addCollapsedAttribute(node, collapsed) {
  462. const skeleton = new semantic_skeleton_js_1.SemanticSkeleton(collapsed);
  463. node.setAttribute(EnrichAttr.Attribute.COLLAPSED, skeleton.toString());
  464. }
  465. function cloneContentNode(content) {
  466. if (content.mathml.length) {
  467. return walkTree(content);
  468. }
  469. const clone = SETTINGS.implicit
  470. ? createInvisibleOperator(content)
  471. : EnrichAttr.addMrow();
  472. content.mathml = [clone];
  473. return clone;
  474. }
  475. function rewriteMfenced(mml) {
  476. if (DomUtil.tagName(mml) !== semantic_util_js_1.MMLTAGS.MFENCED) {
  477. return mml;
  478. }
  479. const newNode = EnrichAttr.addMrow();
  480. for (let i = 0, attr; (attr = mml.attributes[i]); i++) {
  481. if (['open', 'close', 'separators'].indexOf(attr.name) === -1) {
  482. newNode.setAttribute(attr.name, attr.value);
  483. }
  484. }
  485. DomUtil.toArray(mml.childNodes).forEach(function (x) {
  486. newNode.appendChild(x);
  487. });
  488. DomUtil.replaceNode(mml, newNode);
  489. return newNode;
  490. }
  491. function createInvisibleOperator(operator) {
  492. const moNode = DomUtil.createElement('mo');
  493. const text = DomUtil.createTextNode(operator.textContent);
  494. moNode.appendChild(text);
  495. EnrichAttr.setAttributes(moNode, operator);
  496. moNode.setAttribute(EnrichAttr.Attribute.ADDED, 'true');
  497. return moNode;
  498. }
  499. function setOperatorAttribute(semantic, content) {
  500. const operator = semantic.type + (semantic.textContent ? ',' + semantic.textContent : '');
  501. content.forEach(function (c) {
  502. getInnerNode(c).setAttribute(EnrichAttr.Attribute.OPERATOR, operator);
  503. });
  504. }
  505. function getInnerNode(node) {
  506. const children = DomUtil.toArray(node.childNodes);
  507. if (!children) {
  508. return node;
  509. }
  510. const remainder = children.filter(function (child) {
  511. return !isIgnorable(child);
  512. });
  513. const result = [];
  514. for (let i = 0, remain; (remain = remainder[i]); i++) {
  515. if (SemanticUtil.hasEmptyTag(remain) &&
  516. remain.getAttribute(EnrichAttr.Attribute.TYPE) !==
  517. semantic_meaning_js_1.SemanticType.PUNCTUATION) {
  518. const nextInner = getInnerNode(remain);
  519. if (nextInner && nextInner !== remain) {
  520. result.push(nextInner);
  521. }
  522. }
  523. else {
  524. result.push(remain);
  525. }
  526. }
  527. if (result.length === 1) {
  528. return result[0];
  529. }
  530. return node;
  531. }
  532. function formattedOutput(element, name, wiki = false) {
  533. const output = EnrichAttr.removeAttributePrefix(DomUtil.formatXml(element.toString()));
  534. return wiki ? name + ':\n```html\n' + output + '\n```\n' : output;
  535. }
  536. function collapsePunctuated(semantic, opt_children) {
  537. const optional = !!opt_children;
  538. const children = opt_children || [];
  539. const parent = semantic.parent;
  540. const contentIds = semantic.contentNodes.map(function (x) {
  541. return x.id;
  542. });
  543. contentIds.unshift('c');
  544. const childIds = [semantic.id, contentIds];
  545. for (let i = 0, child; (child = semantic.childNodes[i]); i++) {
  546. const mmlChild = walkTree(child);
  547. children.push(mmlChild);
  548. const innerNode = getInnerNode(mmlChild);
  549. if (parent && !optional) {
  550. innerNode.setAttribute(EnrichAttr.Attribute.PARENT, parent.id.toString());
  551. }
  552. childIds.push(child.id);
  553. }
  554. return childIds;
  555. }