|
| 1 | +/************************************************************* |
| 2 | + * |
| 3 | + * Copyright (c) 2018 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 | +/** |
| 19 | + * @fileoverview Implements the CHTMLmfenced wrapper for the MmlMfenced object |
| 20 | + * |
| 21 | + * @author [email protected] (Davide Cervone) |
| 22 | + */ |
| 23 | + |
| 24 | +import {CHTMLWrapper} from '../Wrapper.js'; |
| 25 | +import {CHTMLWrapperFactory} from '../WrapperFactory.js'; |
| 26 | +import {CHTMLinferredMrow} from './mrow.js'; |
| 27 | +import {MmlMfenced} from '../../../core/MmlTree/MmlNodes/mfenced.js'; |
| 28 | +import {MmlInferredMrow} from '../../../core/MmlTree/MmlNodes/mrow.js'; |
| 29 | +import {MmlNode, AbstractMmlNode, AttributeList} from '../../../core/MmlTree/MmlNode.js'; |
| 30 | +import {BBox} from '../BBox.js'; |
| 31 | + |
| 32 | +/*****************************************************************/ |
| 33 | +/* |
| 34 | + * The CHTMLmfenced wrapper for the MmlMfenced object |
| 35 | + * |
| 36 | + * @template N The HTMLElement node class |
| 37 | + * @template T The Text node class |
| 38 | + * @template D The Document class |
| 39 | + */ |
| 40 | +export class CHTMLmfenced<N, T, D> extends CHTMLWrapper<N, T, D> { |
| 41 | + public static kind = MmlMfenced.prototype.kind; |
| 42 | + |
| 43 | + // |
| 44 | + // An mrow to use for the layout of the mfenced |
| 45 | + // |
| 46 | + protected mrow: CHTMLinferredMrow<N, T, D> = null; |
| 47 | + |
| 48 | + /* |
| 49 | + * @override |
| 50 | + * @constructor |
| 51 | + */ |
| 52 | + constructor(factory: CHTMLWrapperFactory<N, T, D>, node: MmlNode, parent: CHTMLWrapper<N, T, D> = null) { |
| 53 | + super(factory, node, parent); |
| 54 | + this.createMrow(); |
| 55 | + this.addMrowChildren(); |
| 56 | + } |
| 57 | + |
| 58 | + /* |
| 59 | + * Creates the mrow wrapper to use for the layout |
| 60 | + */ |
| 61 | + protected createMrow() { |
| 62 | + const mmlFactory = (this.node as AbstractMmlNode).factory; |
| 63 | + const mrow = mmlFactory.create('inferredMrow'); |
| 64 | + const attributes = this.node.attributes; |
| 65 | + const display = attributes.get('display') as boolean; |
| 66 | + const scriptlevel = attributes.get('scriptlevel') as number; |
| 67 | + const defaults: AttributeList = { |
| 68 | + mathsize: ['math', attributes.get('mathsize')] |
| 69 | + }; |
| 70 | + mrow.setInheritedAttributes(defaults, display, scriptlevel, false); |
| 71 | + this.mrow = this.wrap(mrow) as CHTMLinferredMrow<N, T, D>; |
| 72 | + this.mrow.parent = this; |
| 73 | + } |
| 74 | + |
| 75 | + /* |
| 76 | + * Populate the mrow with wrapped mo elements interleaved |
| 77 | + * with the mfenced children (the mo's are already created |
| 78 | + * in the mfenced object) |
| 79 | + */ |
| 80 | + protected addMrowChildren() { |
| 81 | + const mfenced = this.node as MmlMfenced; |
| 82 | + const mrow = this.mrow; |
| 83 | + this.addMo(mfenced.open); |
| 84 | + if (this.childNodes.length) { |
| 85 | + mrow.childNodes.push(this.childNodes[0]); |
| 86 | + } |
| 87 | + let i = 0; |
| 88 | + for (const child of this.childNodes.slice(1)) { |
| 89 | + this.addMo(mfenced.separators[i++]); |
| 90 | + mrow.childNodes.push(child); |
| 91 | + } |
| 92 | + this.addMo(mfenced.close); |
| 93 | + mrow.stretchChildren(); |
| 94 | + } |
| 95 | + |
| 96 | + /* |
| 97 | + * Wrap an mo element and push it onto the mrow |
| 98 | + * |
| 99 | + * @param{MmlNode} node The mo element to push on the mrow |
| 100 | + */ |
| 101 | + protected addMo(node: MmlNode) { |
| 102 | + if (!node) return; |
| 103 | + const mo = this.wrap(node); |
| 104 | + this.mrow.childNodes.push(mo); |
| 105 | + mo.parent = this.mrow; |
| 106 | + } |
| 107 | + |
| 108 | + /* |
| 109 | + * @override |
| 110 | + */ |
| 111 | + public toCHTML(parent: N) { |
| 112 | + const chtml = this.standardCHTMLnode(parent); |
| 113 | + this.mrow.toCHTML(chtml); |
| 114 | + this.drawBBox(); |
| 115 | + } |
| 116 | + |
| 117 | + /* |
| 118 | + * @override |
| 119 | + */ |
| 120 | + public computeBBox(bbox: BBox) { |
| 121 | + bbox.updateFrom(this.mrow.getBBox()); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments