case_multiscripts.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { SemanticRole, SemanticType } from '../semantic_tree/semantic_meaning.js';
  3. import { SemanticSkeleton } from '../semantic_tree/semantic_skeleton.js';
  4. import { MMLTAGS } from '../semantic_tree/semantic_util.js';
  5. import { CaseMultiindex } from './case_multiindex.js';
  6. import * as EnrichMathml from './enrich_mathml.js';
  7. import { setAttributes, Attribute } from './enrich_attr.js';
  8. export class CaseMultiscripts extends CaseMultiindex {
  9. static test(semantic) {
  10. if (!semantic.mathmlTree) {
  11. return false;
  12. }
  13. const mmlTag = DomUtil.tagName(semantic.mathmlTree);
  14. return (mmlTag === MMLTAGS.MMULTISCRIPTS &&
  15. (semantic.type === SemanticType.SUPERSCRIPT ||
  16. semantic.type === SemanticType.SUBSCRIPT));
  17. }
  18. constructor(semantic) {
  19. super(semantic);
  20. }
  21. getMathml() {
  22. setAttributes(this.mml, this.semantic);
  23. let baseSem, rsup, rsub;
  24. if (this.semantic.childNodes[0] &&
  25. this.semantic.childNodes[0].role === SemanticRole.SUBSUP) {
  26. const ignore = this.semantic.childNodes[0];
  27. baseSem = ignore.childNodes[0];
  28. rsup = CaseMultiindex.multiscriptIndex(this.semantic.childNodes[1]);
  29. rsub = CaseMultiindex.multiscriptIndex(ignore.childNodes[1]);
  30. const collapsed = [this.semantic.id, [ignore.id, baseSem.id, rsub], rsup];
  31. EnrichMathml.addCollapsedAttribute(this.mml, collapsed);
  32. this.mml.setAttribute(Attribute.TYPE, ignore.role);
  33. this.completeMultiscript(SemanticSkeleton.interleaveIds(rsub, rsup), []);
  34. }
  35. else {
  36. baseSem = this.semantic.childNodes[0];
  37. rsup = CaseMultiindex.multiscriptIndex(this.semantic.childNodes[1]);
  38. const collapsed = [this.semantic.id, baseSem.id, rsup];
  39. EnrichMathml.addCollapsedAttribute(this.mml, collapsed);
  40. }
  41. const childIds = SemanticSkeleton.collapsedLeafs(rsub || [], rsup);
  42. const base = EnrichMathml.walkTree(baseSem);
  43. EnrichMathml.getInnerNode(base).setAttribute(Attribute.PARENT, this.semantic.id.toString());
  44. childIds.unshift(baseSem.id);
  45. this.mml.setAttribute(Attribute.CHILDREN, childIds.join(','));
  46. return this.mml;
  47. }
  48. }