mfenced.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements the CommonMfenced wrapper mixin for the MmlMfenced object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
  23. import {CommonInferredMrow} from './mrow.js';
  24. import {MmlNode, AbstractMmlNode} from '../../../core/MmlTree/MmlNode.js';
  25. import {MmlMfenced} from '../../../core/MmlTree/MmlNodes/mfenced.js';
  26. import {BBox} from '../../../util/BBox.js';
  27. /*****************************************************************/
  28. /**
  29. * The CommonMfenced interface
  30. */
  31. export interface CommonMfenced extends AnyWrapper {
  32. /**
  33. * An mrow to use for the layout of the mfenced
  34. */
  35. mrow: CommonInferredMrow;
  36. /**
  37. * Creates the mrow wrapper to use for the layout
  38. */
  39. createMrow(): void;
  40. /**
  41. * Populate the mrow with wrapped mo elements interleaved
  42. * with the mfenced children (the mo's are already created
  43. * in the mfenced object)
  44. */
  45. addMrowChildren(): void;
  46. /**
  47. * Wrap an mo element and push it onto the mrow
  48. *
  49. * @param {MmlNode} node The mo element to push on the mrow
  50. */
  51. addMo(node: MmlNode): void;
  52. }
  53. /**
  54. * Shorthand for the CommonMfenced constructor
  55. */
  56. export type MfencedConstructor = Constructor<CommonMfenced>;
  57. /*****************************************************************/
  58. /**
  59. * The CommonMfenced wrapper mixin for the MmlMfenced object
  60. *
  61. * @template T The Wrapper class constructor type
  62. */
  63. export function CommonMfencedMixin<T extends WrapperConstructor>(Base: T): MfencedConstructor & T {
  64. return class extends Base {
  65. /**
  66. * An mrow to use for the layout of the mfenced
  67. */
  68. public mrow: CommonInferredMrow = null;
  69. /**
  70. * @override
  71. * @constructor
  72. */
  73. constructor(...args: any[]) {
  74. super(...args);
  75. this.createMrow();
  76. this.addMrowChildren();
  77. }
  78. /**
  79. * Creates the mrow wrapper to use for the layout
  80. */
  81. public createMrow() {
  82. const mmlFactory = (this.node as AbstractMmlNode).factory;
  83. const mrow = mmlFactory.create('inferredMrow');
  84. mrow.inheritAttributesFrom(this.node);
  85. this.mrow = this.wrap(mrow) as CommonInferredMrow;
  86. this.mrow.parent = this;
  87. }
  88. /**
  89. * Populate the mrow with wrapped mo elements interleaved
  90. * with the mfenced children (the mo's are already created
  91. * in the mfenced object)
  92. */
  93. public addMrowChildren() {
  94. const mfenced = this.node as MmlMfenced;
  95. const mrow = this.mrow;
  96. this.addMo(mfenced.open);
  97. if (this.childNodes.length) {
  98. mrow.childNodes.push(this.childNodes[0]);
  99. }
  100. let i = 0;
  101. for (const child of this.childNodes.slice(1)) {
  102. this.addMo(mfenced.separators[i++]);
  103. mrow.childNodes.push(child);
  104. }
  105. this.addMo(mfenced.close);
  106. mrow.stretchChildren();
  107. }
  108. /**
  109. * Wrap an mo element and push it onto the mrow
  110. *
  111. * @param {MmlNode} node The mo element to push on the mrow
  112. */
  113. public addMo(node: MmlNode) {
  114. if (!node) return;
  115. const mo = this.wrap(node);
  116. this.mrow.childNodes.push(mo);
  117. mo.parent = this.mrow;
  118. }
  119. /**
  120. * @override
  121. */
  122. public computeBBox(bbox: BBox, recompute: boolean = false) {
  123. bbox.updateFrom(this.mrow.getOuterBBox());
  124. this.setChildPWidths(recompute);
  125. }
  126. };
  127. }