case_multiindex.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { SemanticRole, 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, Attribute } from './enrich_attr.js';
  7. export class CaseMultiindex extends AbstractEnrichCase {
  8. static multiscriptIndex(index) {
  9. if (index.type === SemanticType.PUNCTUATED &&
  10. index.contentNodes[0].role === SemanticRole.DUMMY) {
  11. return EnrichMathml.collapsePunctuated(index);
  12. }
  13. EnrichMathml.walkTree(index);
  14. return index.id;
  15. }
  16. static createNone_(semantic) {
  17. const newNode = DomUtil.createElement('none');
  18. if (semantic) {
  19. setAttributes(newNode, semantic);
  20. }
  21. newNode.setAttribute(Attribute.ADDED, 'true');
  22. return newNode;
  23. }
  24. constructor(semantic) {
  25. super(semantic);
  26. this.mml = semantic.mathmlTree;
  27. }
  28. completeMultiscript(rightIndices, leftIndices) {
  29. const children = DomUtil.toArray(this.mml.childNodes).slice(1);
  30. let childCounter = 0;
  31. const completeIndices = (indices) => {
  32. for (const index of indices) {
  33. const child = children[childCounter];
  34. if (child && index === parseInt(child.getAttribute(Attribute.ID))) {
  35. child.setAttribute(Attribute.PARENT, this.semantic.id.toString());
  36. childCounter++;
  37. }
  38. else if (!child ||
  39. index !==
  40. parseInt(EnrichMathml.getInnerNode(child).getAttribute(Attribute.ID))) {
  41. const query = this.semantic.querySelectorAll((x) => x.id === index);
  42. this.mml.insertBefore(CaseMultiindex.createNone_(query[0]), child || null);
  43. }
  44. else {
  45. EnrichMathml.getInnerNode(child).setAttribute(Attribute.PARENT, this.semantic.id.toString());
  46. childCounter++;
  47. }
  48. }
  49. };
  50. completeIndices(rightIndices);
  51. if (children[childCounter] &&
  52. DomUtil.tagName(children[childCounter]) !== MMLTAGS.MPRESCRIPTS) {
  53. this.mml.insertBefore(children[childCounter], DomUtil.createElement('mprescripts'));
  54. }
  55. else {
  56. childCounter++;
  57. }
  58. completeIndices(leftIndices);
  59. }
  60. }