case_table.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { SemanticType } from '../semantic_tree/semantic_meaning.js';
  3. import { MMLTAGS } from '../semantic_tree/semantic_util.js';
  4. import { AbstractEnrichCase } from './abstract_enrich_case.js';
  5. import * as EnrichMathml from './enrich_mathml.js';
  6. import { setAttributes } from './enrich_attr.js';
  7. export class CaseTable extends AbstractEnrichCase {
  8. static test(semantic) {
  9. return (semantic.type === SemanticType.MATRIX ||
  10. semantic.type === SemanticType.VECTOR ||
  11. semantic.type === SemanticType.CASES);
  12. }
  13. constructor(semantic) {
  14. super(semantic);
  15. this.inner = [];
  16. this.mml = semantic.mathmlTree;
  17. }
  18. getMathml() {
  19. const lfence = EnrichMathml.cloneContentNode(this.semantic.contentNodes[0]);
  20. const rfence = this.semantic.contentNodes[1]
  21. ? EnrichMathml.cloneContentNode(this.semantic.contentNodes[1])
  22. : null;
  23. this.inner = this.semantic.childNodes.map(EnrichMathml.walkTree);
  24. if (!this.mml) {
  25. this.mml = EnrichMathml.introduceNewLayer([lfence].concat(this.inner, [rfence]), this.semantic);
  26. }
  27. else if (DomUtil.tagName(this.mml) === MMLTAGS.MFENCED) {
  28. const children = this.mml.childNodes;
  29. this.mml.insertBefore(lfence, children[0] || null);
  30. if (rfence) {
  31. this.mml.appendChild(rfence);
  32. }
  33. this.mml = EnrichMathml.rewriteMfenced(this.mml);
  34. }
  35. else {
  36. const newChildren = [lfence, this.mml];
  37. if (rfence) {
  38. newChildren.push(rfence);
  39. }
  40. this.mml = EnrichMathml.introduceNewLayer(newChildren, this.semantic);
  41. }
  42. setAttributes(this.mml, this.semantic);
  43. return this.mml;
  44. }
  45. }