case_empheq.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { MMLTAGS } from '../semantic_tree/semantic_util.js';
  2. import { AbstractEnrichCase } from './abstract_enrich_case.js';
  3. import * as EnrichMathml from './enrich_mathml.js';
  4. import { addMrow, setAttributes } from './enrich_attr.js';
  5. import * as DomUtil from '../common/dom_util.js';
  6. export class CaseEmpheq extends AbstractEnrichCase {
  7. static test(semantic) {
  8. return !!semantic.mathmlTree && semantic.hasAnnotation('Emph', 'top');
  9. }
  10. constructor(semantic) {
  11. super(semantic);
  12. this.mrows = [];
  13. this.mml = semantic.mathmlTree;
  14. }
  15. getMathml() {
  16. this.recurseToTable(this.semantic);
  17. if (this.mrows.length) {
  18. const newRow = addMrow();
  19. const parent = this.mml.parentNode;
  20. parent.insertBefore(newRow, this.mml);
  21. for (const mrow of this.mrows) {
  22. newRow.appendChild(mrow);
  23. }
  24. newRow.appendChild(this.mml);
  25. }
  26. return this.mml;
  27. }
  28. recurseToTable(node) {
  29. var _a, _b;
  30. if (!(node.hasAnnotation('Emph', 'top') || node.hasAnnotation('Emph', 'fence')) &&
  31. (node.hasAnnotation('Emph', 'left') ||
  32. node.hasAnnotation('Emph', 'right'))) {
  33. EnrichMathml.walkTree(node);
  34. return;
  35. }
  36. if (!node.mathmlTree ||
  37. (DomUtil.tagName(node.mathmlTree) === MMLTAGS.MTABLE &&
  38. ((_a = node.annotation['Emph']) === null || _a === void 0 ? void 0 : _a.length) &&
  39. node.annotation['Emph'][0] !== 'table')) {
  40. const newNode = addMrow();
  41. setAttributes(newNode, node);
  42. this.mrows.unshift(newNode);
  43. }
  44. else {
  45. if (DomUtil.tagName(node.mathmlTree) === MMLTAGS.MTABLE &&
  46. ((_b = node.annotation['Emph']) === null || _b === void 0 ? void 0 : _b.length) &&
  47. node.annotation['Emph'][0] === 'table') {
  48. this.finalizeTable(node);
  49. return;
  50. }
  51. setAttributes(node.mathmlTree, node);
  52. }
  53. node.childNodes.forEach(this.recurseToTable.bind(this));
  54. if (node.textContent || node.type === 'punctuated') {
  55. const newContent = node.contentNodes.map((x) => {
  56. const newNode = EnrichMathml.cloneContentNode(x);
  57. if (newNode.hasAttribute('data-semantic-added')) {
  58. this.mrows.unshift(newNode);
  59. }
  60. else {
  61. this.recurseToTable(x);
  62. }
  63. return newNode;
  64. });
  65. EnrichMathml.setOperatorAttribute(node, newContent);
  66. return;
  67. }
  68. node.contentNodes.forEach(this.recurseToTable.bind(this));
  69. }
  70. finalizeTable(node) {
  71. setAttributes(node.mathmlTree, node);
  72. node.contentNodes.forEach((x) => {
  73. EnrichMathml.walkTree(x);
  74. });
  75. node.childNodes.forEach((x) => {
  76. EnrichMathml.walkTree(x);
  77. });
  78. }
  79. }