case_double_script.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { SemanticRole } 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 { makeIdList, setAttributes, Attribute } from './enrich_attr.js';
  7. export class CaseDoubleScript extends AbstractEnrichCase {
  8. static test(semantic) {
  9. if (!semantic.mathmlTree || !semantic.childNodes.length) {
  10. return false;
  11. }
  12. const mmlTag = DomUtil.tagName(semantic.mathmlTree);
  13. const role = semantic.childNodes[0].role;
  14. return ((mmlTag === MMLTAGS.MSUBSUP && role === SemanticRole.SUBSUP) ||
  15. (mmlTag === MMLTAGS.MUNDEROVER && role === SemanticRole.UNDEROVER));
  16. }
  17. constructor(semantic) {
  18. super(semantic);
  19. this.mml = semantic.mathmlTree;
  20. }
  21. getMathml() {
  22. const ignore = this.semantic.childNodes[0];
  23. const baseSem = ignore.childNodes[0];
  24. const supSem = this.semantic.childNodes[1];
  25. const subSem = ignore.childNodes[1];
  26. const supMml = EnrichMathml.walkTree(supSem);
  27. const baseMml = EnrichMathml.walkTree(baseSem);
  28. const subMml = EnrichMathml.walkTree(subSem);
  29. setAttributes(this.mml, this.semantic);
  30. this.mml.setAttribute(Attribute.CHILDREN, makeIdList([baseSem, subSem, supSem]));
  31. [baseMml, subMml, supMml].forEach((child) => EnrichMathml.getInnerNode(child).setAttribute(Attribute.PARENT, this.mml.getAttribute(Attribute.ID)));
  32. this.mml.setAttribute(Attribute.TYPE, ignore.role);
  33. EnrichMathml.addCollapsedAttribute(this.mml, [
  34. this.semantic.id,
  35. [ignore.id, baseSem.id, subSem.id],
  36. supSem.id
  37. ]);
  38. return this.mml;
  39. }
  40. }