BaseItems.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2009-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 Stack items for basic Tex parsing.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {MapHandler} from '../MapHandler.js';
  23. import {CharacterMap} from '../SymbolMap.js';
  24. import {entities} from '../../../util/Entities.js';
  25. import {MmlNode, TextNode, TEXCLASS} from '../../../core/MmlTree/MmlNode.js';
  26. import {MmlMsubsup} from '../../../core/MmlTree/MmlNodes/msubsup.js';
  27. import TexError from '../TexError.js';
  28. import ParseUtil from '../ParseUtil.js';
  29. import NodeUtil from '../NodeUtil.js';
  30. import {Property} from '../../../core/Tree/Node.js';
  31. import StackItemFactory from '../StackItemFactory.js';
  32. import {CheckType, BaseItem, StackItem, EnvList} from '../StackItem.js';
  33. /**
  34. * Initial item on the stack. It's pushed when parsing begins.
  35. */
  36. export class StartItem extends BaseItem {
  37. /**
  38. * @override
  39. */
  40. constructor(factory: StackItemFactory, public global: EnvList) {
  41. super(factory);
  42. }
  43. /**
  44. * @override
  45. */
  46. public get kind() {
  47. return 'start';
  48. }
  49. /**
  50. * @override
  51. */
  52. get isOpen() {
  53. return true;
  54. }
  55. /**
  56. * @override
  57. */
  58. public checkItem(item: StackItem): CheckType {
  59. if (item.isKind('stop')) {
  60. let node = this.toMml();
  61. if (!this.global.isInner) {
  62. node = this.factory.configuration.tags.finalize(node, this.env);
  63. }
  64. return [[this.factory.create('mml', node)], true];
  65. }
  66. return super.checkItem(item);
  67. }
  68. }
  69. /**
  70. * Final item on the stack. Errors will be thrown if other items than the start
  71. * item are still on the stack.
  72. */
  73. export class StopItem extends BaseItem {
  74. /**
  75. * @override
  76. */
  77. public get kind() {
  78. return 'stop';
  79. }
  80. /**
  81. * @override
  82. */
  83. get isClose() {
  84. return true;
  85. }
  86. }
  87. /**
  88. * Item indicating an open brace.
  89. */
  90. export class OpenItem extends BaseItem {
  91. /**
  92. * @override
  93. */
  94. protected static errors = Object.assign(Object.create(BaseItem.errors), {
  95. // @test ExtraOpenMissingClose
  96. 'stop': ['ExtraOpenMissingClose',
  97. 'Extra open brace or missing close brace']
  98. });
  99. /**
  100. * @override
  101. */
  102. public get kind() {
  103. return 'open';
  104. }
  105. /**
  106. * @override
  107. */
  108. get isOpen() {
  109. return true;
  110. }
  111. /**
  112. * @override
  113. */
  114. public checkItem(item: StackItem): CheckType {
  115. if (item.isKind('close')) {
  116. // @test PrimeSup
  117. let mml = this.toMml();
  118. const node = this.create('node', 'TeXAtom', [mml]);
  119. return [[this.factory.create('mml', node)], true];
  120. }
  121. return super.checkItem(item);
  122. }
  123. }
  124. /**
  125. * Item indicating a close brace. Collapses stack until an OpenItem is found.
  126. */
  127. export class CloseItem extends BaseItem {
  128. /**
  129. * @override
  130. */
  131. public get kind() {
  132. return 'close';
  133. }
  134. /**
  135. * @override
  136. */
  137. get isClose() {
  138. return true;
  139. }
  140. }
  141. /**
  142. * Item indicating an we are currently dealing with a prime mark.
  143. */
  144. export class PrimeItem extends BaseItem {
  145. /**
  146. * @override
  147. */
  148. public get kind() {
  149. return 'prime';
  150. }
  151. /**
  152. * @override
  153. */
  154. public checkItem(item: StackItem): CheckType {
  155. let [top0, top1] = this.Peek(2);
  156. if (!NodeUtil.isType(top0, 'msubsup') || NodeUtil.isType(top0, 'msup')) {
  157. // @test Prime, Double Prime
  158. const node = this.create('node', 'msup', [top0, top1]);
  159. return [[node, item], true];
  160. }
  161. NodeUtil.setChild(top0, (top0 as MmlMsubsup).sup, top1);
  162. return [[top0, item], true];
  163. }
  164. }
  165. /**
  166. * Item indicating an we are currently dealing with a sub/superscript
  167. * expression.
  168. */
  169. export class SubsupItem extends BaseItem {
  170. /**
  171. * @override
  172. */
  173. protected static errors = Object.assign(Object.create(BaseItem.errors), {
  174. // @test MissingScript Sub, MissingScript Sup
  175. 'stop': ['MissingScript',
  176. 'Missing superscript or subscript argument'],
  177. // @test MissingOpenForSup
  178. 'sup': ['MissingOpenForSup',
  179. 'Missing open brace for superscript'],
  180. // @test MissingOpenForSub
  181. 'sub': ['MissingOpenForSub',
  182. 'Missing open brace for subscript']
  183. });
  184. /**
  185. * @override
  186. */
  187. public get kind() {
  188. return 'subsup';
  189. }
  190. /**
  191. * @override
  192. */
  193. public checkItem(item: StackItem): CheckType | null {
  194. if (item.isKind('open') || item.isKind('left')) {
  195. return BaseItem.success;
  196. }
  197. const top = this.First;
  198. const position = this.getProperty('position') as number;
  199. if (item.isKind('mml')) {
  200. if (this.getProperty('primes')) {
  201. if (position !== 2) {
  202. // @test Prime on Sub
  203. NodeUtil.setChild(top, 2, this.getProperty('primes') as MmlNode);
  204. } else {
  205. // @test Prime on Prime
  206. NodeUtil.setProperty(this.getProperty('primes') as MmlNode, 'variantForm', true);
  207. const node = this.create('node', 'mrow', [this.getProperty('primes') as MmlNode, item.First]);
  208. item.First = node;
  209. }
  210. }
  211. NodeUtil.setChild(top, position, item.First);
  212. if (this.getProperty('movesupsub') != null) {
  213. // @test Limits Subsup (currently does not work! Check again!)
  214. NodeUtil.setProperty(top, 'movesupsub', this.getProperty('movesupsub') as Property);
  215. }
  216. const result = this.factory.create('mml', top);
  217. return [[result], true];
  218. }
  219. if (super.checkItem(item)[1]) {
  220. // @test Brace Superscript Error, MissingOpenForSup, MissingOpenForSub
  221. const error = this.getErrors(['', 'sub', 'sup'][position]);
  222. throw new TexError(error[0], error[1], ...error.splice(2));
  223. }
  224. return null;
  225. }
  226. }
  227. /**
  228. * Item indicating an we are currently dealing with an \\over command.
  229. */
  230. export class OverItem extends BaseItem {
  231. /**
  232. * @override
  233. */
  234. constructor(factory: StackItemFactory) {
  235. super(factory);
  236. this.setProperty('name', '\\over');
  237. }
  238. /**
  239. * @override
  240. */
  241. public get kind() {
  242. return 'over';
  243. }
  244. /**
  245. * @override
  246. */
  247. get isClose() {
  248. return true;
  249. }
  250. /**
  251. * @override
  252. */
  253. public checkItem(item: StackItem): CheckType {
  254. if (item.isKind('over')) {
  255. // @test Double Over
  256. throw new TexError(
  257. 'AmbiguousUseOf', 'Ambiguous use of %1', item.getName());
  258. }
  259. if (item.isClose) {
  260. // @test Over
  261. let mml = this.create('node',
  262. 'mfrac', [this.getProperty('num') as MmlNode, this.toMml(false)]);
  263. if (this.getProperty('thickness') != null) {
  264. // @test Choose, Above, Above with Delims
  265. NodeUtil.setAttribute(mml, 'linethickness',
  266. this.getProperty('thickness') as string);
  267. }
  268. if (this.getProperty('open') || this.getProperty('close')) {
  269. // @test Choose
  270. NodeUtil.setProperty(mml, 'withDelims', true);
  271. mml = ParseUtil.fixedFence(this.factory.configuration,
  272. this.getProperty('open') as string, mml,
  273. this.getProperty('close') as string);
  274. }
  275. return [[this.factory.create('mml', mml), item], true];
  276. }
  277. return super.checkItem(item);
  278. }
  279. /**
  280. * @override
  281. */
  282. public toString() {
  283. return 'over[' + this.getProperty('num') +
  284. ' / ' + this.nodes.join('; ') + ']';
  285. }
  286. }
  287. /**
  288. * Item pushed when a \\left opening delimiter has been found.
  289. */
  290. export class LeftItem extends BaseItem {
  291. /**
  292. * @override
  293. */
  294. protected static errors = Object.assign(Object.create(BaseItem.errors), {
  295. // @test ExtraLeftMissingRight
  296. 'stop': ['ExtraLeftMissingRight',
  297. 'Extra \\left or missing \\right']
  298. });
  299. /**
  300. * @override
  301. */
  302. constructor(factory: StackItemFactory, delim: string) {
  303. super(factory);
  304. this.setProperty('delim', delim);
  305. }
  306. /**
  307. * @override
  308. */
  309. public get kind() {
  310. return 'left';
  311. }
  312. /**
  313. * @override
  314. */
  315. get isOpen() {
  316. return true;
  317. }
  318. /**
  319. * @override
  320. */
  321. public checkItem(item: StackItem): CheckType {
  322. // @test Missing Right
  323. if (item.isKind('right')) {
  324. //
  325. // Create the fenced structure as an mrow
  326. //
  327. return [[this.factory.create('mml', ParseUtil.fenced(
  328. this.factory.configuration,
  329. this.getProperty('delim') as string, this.toMml(),
  330. item.getProperty('delim') as string, '', item.getProperty('color') as string))], true];
  331. }
  332. if (item.isKind('middle')) {
  333. //
  334. // Add the middle delimiter, with empty open and close elements around it for spacing
  335. //
  336. const def = {stretchy: true} as any;
  337. if (item.getProperty('color')) {
  338. def.mathcolor = item.getProperty('color');
  339. }
  340. this.Push(
  341. this.create('node', 'TeXAtom', [], {texClass: TEXCLASS.CLOSE}),
  342. this.create('token', 'mo', def, item.getProperty('delim')),
  343. this.create('node', 'TeXAtom', [], {texClass: TEXCLASS.OPEN})
  344. );
  345. this.env = {}; // Since \middle closes the group, clear the environment
  346. return [[this], true]; // this will reset the environment to its initial state
  347. }
  348. return super.checkItem(item);
  349. }
  350. }
  351. /**
  352. * Item pushed when a \\middle delimiter has been found. Stack is
  353. * collapsed until a corresponding LeftItem is encountered.
  354. */
  355. export class Middle extends BaseItem {
  356. /**
  357. * @override
  358. */
  359. constructor(factory: StackItemFactory, delim: string, color: string) {
  360. super(factory);
  361. this.setProperty('delim', delim);
  362. color && this.setProperty('color', color);
  363. }
  364. /**
  365. * @override
  366. */
  367. public get kind() {
  368. return 'middle';
  369. }
  370. /**
  371. * @override
  372. */
  373. get isClose() {
  374. return true;
  375. }
  376. }
  377. /**
  378. * Item pushed when a \\right closing delimiter has been found. Stack is
  379. * collapsed until a corresponding LeftItem is encountered.
  380. */
  381. export class RightItem extends BaseItem {
  382. /**
  383. * @override
  384. */
  385. constructor(factory: StackItemFactory, delim: string, color: string) {
  386. super(factory);
  387. this.setProperty('delim', delim);
  388. color && this.setProperty('color', color);
  389. }
  390. /**
  391. * @override
  392. */
  393. public get kind() {
  394. return 'right';
  395. }
  396. /**
  397. * @override
  398. */
  399. get isClose() {
  400. return true;
  401. }
  402. }
  403. /**
  404. * Item pushed for opening an environment with \\begin{env}.
  405. */
  406. export class BeginItem extends BaseItem {
  407. /**
  408. * @override
  409. */
  410. public get kind() {
  411. return 'begin';
  412. }
  413. /**
  414. * @override
  415. */
  416. get isOpen() {
  417. return true;
  418. }
  419. /**
  420. * @override
  421. */
  422. public checkItem(item: StackItem): CheckType {
  423. if (item.isKind('end')) {
  424. if (item.getName() !== this.getName()) {
  425. // @test EnvBadEnd
  426. throw new TexError('EnvBadEnd', '\\begin{%1} ended with \\end{%2}',
  427. this.getName(), item.getName());
  428. }
  429. if (!this.getProperty('end')) {
  430. // @test Hfill
  431. return [[this.factory.create('mml', this.toMml())], true];
  432. }
  433. return BaseItem.fail; // TODO: This case could probably go!
  434. }
  435. if (item.isKind('stop')) {
  436. // @test EnvMissingEnd Array
  437. throw new TexError('EnvMissingEnd', 'Missing \\end{%1}', this.getName());
  438. }
  439. return super.checkItem(item);
  440. }
  441. }
  442. /**
  443. * Item pushed for closing an environment with \\end{env}. Stack is collapsed
  444. * until a corresponding BeginItem for 'env' is found. Error is thrown in case
  445. * other open environments interfere.
  446. */
  447. export class EndItem extends BaseItem {
  448. /**
  449. * @override
  450. */
  451. public get kind() {
  452. return 'end';
  453. }
  454. /**
  455. * @override
  456. */
  457. get isClose() {
  458. return true;
  459. }
  460. }
  461. /**
  462. * Item pushed for remembering styling information.
  463. */
  464. export class StyleItem extends BaseItem {
  465. /**
  466. * @override
  467. */
  468. public get kind() {
  469. return 'style';
  470. }
  471. /**
  472. * @override
  473. */
  474. public checkItem(item: StackItem): CheckType {
  475. if (!item.isClose) {
  476. return super.checkItem(item);
  477. }
  478. // @test Style
  479. const mml = this.create('node', 'mstyle', this.nodes, this.getProperty('styles'));
  480. return [[this.factory.create('mml', mml), item], true];
  481. }
  482. }
  483. /**
  484. * Item pushed for remembering positioning information.
  485. */
  486. export class PositionItem extends BaseItem {
  487. /**
  488. * @override
  489. */
  490. public get kind() {
  491. return 'position';
  492. }
  493. /**
  494. * @override
  495. */
  496. public checkItem(item: StackItem): CheckType {
  497. if (item.isClose) {
  498. // @test MissingBoxFor
  499. throw new TexError('MissingBoxFor', 'Missing box for %1', this.getName());
  500. }
  501. if (item.isFinal) {
  502. let mml = item.toMml();
  503. switch (this.getProperty('move')) {
  504. case 'vertical':
  505. // @test Raise, Lower, Raise Negative, Lower Negative
  506. mml = this.create('node', 'mpadded', [mml],
  507. {height: this.getProperty('dh'),
  508. depth: this.getProperty('dd'),
  509. voffset: this.getProperty('dh')});
  510. return [[this.factory.create('mml', mml)], true];
  511. case 'horizontal':
  512. // @test Move Left, Move Right, Move Left Negative, Move Right Negative
  513. return [[this.factory.create('mml', this.getProperty('left') as MmlNode), item,
  514. this.factory.create('mml', this.getProperty('right') as MmlNode)], true];
  515. }
  516. }
  517. return super.checkItem(item);
  518. }
  519. }
  520. /**
  521. * Item indicating a table cell.
  522. */
  523. export class CellItem extends BaseItem {
  524. /**
  525. * @override
  526. */
  527. public get kind() {
  528. return 'cell';
  529. }
  530. /**
  531. * @override
  532. */
  533. get isClose() {
  534. return true;
  535. }
  536. }
  537. /**
  538. * Final item for collating Nodes.
  539. */
  540. export class MmlItem extends BaseItem {
  541. /**
  542. * @override
  543. */
  544. public get isFinal() {
  545. return true;
  546. }
  547. /**
  548. * @override
  549. */
  550. public get kind() {
  551. return 'mml';
  552. }
  553. }
  554. /**
  555. * Item indicating a named function operator (e.g., \\sin) as been encountered.
  556. */
  557. export class FnItem extends BaseItem {
  558. /**
  559. * @override
  560. */
  561. public get kind() {
  562. return 'fn';
  563. }
  564. /**
  565. * @override
  566. */
  567. public checkItem(item: StackItem): CheckType {
  568. const top = this.First;
  569. if (top) {
  570. if (item.isOpen) {
  571. // @test Fn Stretchy
  572. return BaseItem.success;
  573. }
  574. if (!item.isKind('fn')) {
  575. // @test Named Function
  576. let mml = item.First;
  577. if (!item.isKind('mml') || !mml) {
  578. // @test Mathop Super
  579. return [[top, item], true];
  580. }
  581. if ((NodeUtil.isType(mml, 'mstyle') && mml.childNodes.length &&
  582. NodeUtil.isType(mml.childNodes[0].childNodes[0] as MmlNode, 'mspace')) ||
  583. NodeUtil.isType(mml, 'mspace')) {
  584. // @test Fn Pos Space, Fn Neg Space
  585. return [[top, item], true];
  586. }
  587. if (NodeUtil.isEmbellished(mml)) {
  588. // @test MultiInt with Limits
  589. mml = NodeUtil.getCoreMO(mml);
  590. }
  591. const form = NodeUtil.getForm(mml);
  592. if (form != null && [0, 0, 1, 1, 0, 1, 1, 0, 0, 0][form[2]]) {
  593. // @test Fn Operator
  594. return [[top, item], true];
  595. }
  596. }
  597. // @test Named Function, Named Function Arg
  598. const node = this.create('token', 'mo', {texClass: TEXCLASS.NONE},
  599. entities.ApplyFunction);
  600. return [[top, node, item], true];
  601. }
  602. // @test Mathop Super, Mathop Sub
  603. return super.checkItem.apply(this, arguments);
  604. }
  605. }
  606. /**
  607. * Item indicating a \\not has been encountered and needs to be applied to the
  608. * next operator.
  609. */
  610. export class NotItem extends BaseItem {
  611. private remap = MapHandler.getMap('not_remap') as CharacterMap;
  612. /**
  613. * @override
  614. */
  615. public get kind() {
  616. return 'not';
  617. }
  618. /**
  619. * @override
  620. */
  621. public checkItem(item: StackItem): CheckType {
  622. let mml: TextNode | MmlNode;
  623. let c: string;
  624. let textNode: TextNode;
  625. if (item.isKind('open') || item.isKind('left')) {
  626. // @test Negation Left Paren
  627. return BaseItem.success;
  628. }
  629. if (item.isKind('mml') &&
  630. (NodeUtil.isType(item.First, 'mo') || NodeUtil.isType(item.First, 'mi') ||
  631. NodeUtil.isType(item.First, 'mtext'))) {
  632. mml = item.First;
  633. c = NodeUtil.getText(mml as TextNode);
  634. if (c.length === 1 && !NodeUtil.getProperty(mml, 'movesupsub') &&
  635. NodeUtil.getChildren(mml).length === 1) {
  636. if (this.remap.contains(c)) {
  637. // @test Negation Simple, Negation Complex
  638. textNode = this.create('text', this.remap.lookup(c).char) as TextNode;
  639. NodeUtil.setChild(mml, 0, textNode);
  640. } else {
  641. // @test Negation Explicit
  642. textNode = this.create('text', '\u0338') as TextNode;
  643. NodeUtil.appendChildren(mml, [textNode]);
  644. }
  645. return [[item], true];
  646. }
  647. }
  648. // @test Negation Large
  649. textNode = this.create('text', '\u29F8') as TextNode;
  650. const mtextNode = this.create('node', 'mtext', [], {}, textNode);
  651. const paddedNode = this.create('node', 'mpadded', [mtextNode], {width: 0});
  652. mml = this.create('node', 'TeXAtom', [paddedNode], {texClass: TEXCLASS.REL});
  653. return [[mml, item], true];
  654. }
  655. }
  656. /**
  657. * A StackItem that removes an mspace that follows it (for \nonscript).
  658. */
  659. export class NonscriptItem extends BaseItem {
  660. /**
  661. * @override
  662. */
  663. public get kind() {
  664. return 'nonscript';
  665. }
  666. /**
  667. * @override
  668. */
  669. public checkItem(item: StackItem): CheckType {
  670. //
  671. // Check if the next item is an mspace (or an mspace in an mstyle) and remove it.
  672. //
  673. if (item.isKind('mml') && item.Size() === 1) {
  674. let mml = item.First;
  675. //
  676. // Space macros like \, are wrapped with an mstyle to set scriptlevel="0"
  677. // (so size is independent of level), we look at the contents of the mstyle for the mspace.
  678. //
  679. if (mml.isKind('mstyle') && mml.notParent) {
  680. mml = NodeUtil.getChildren(NodeUtil.getChildren(mml)[0])[0];
  681. }
  682. if (mml.isKind('mspace')) {
  683. //
  684. // If the space is in an mstyle, wrap it in an mrow so we can test its scriptlevel
  685. // in the post-filter (the mrow will be removed in the filter). We can't test
  686. // the mstyle's scriptlevel, since it is ecxplicitly setting it to 0.
  687. //
  688. if (mml !== item.First) {
  689. const mrow = this.create('node', 'mrow', [item.Pop()]);
  690. item.Push(mrow);
  691. }
  692. //
  693. // Save the mspace for later post-processing.
  694. //
  695. this.factory.configuration.addNode('nonscript', item.First);
  696. }
  697. }
  698. return [[item], true];
  699. }
  700. }
  701. /**
  702. * Item indicating a dots command has been encountered.
  703. */
  704. export class DotsItem extends BaseItem {
  705. /**
  706. * @override
  707. */
  708. public get kind() {
  709. return 'dots';
  710. }
  711. /**
  712. * @override
  713. */
  714. public checkItem(item: StackItem): CheckType {
  715. if (item.isKind('open') || item.isKind('left')) {
  716. return BaseItem.success;
  717. }
  718. let dots = this.getProperty('ldots') as MmlNode;
  719. let top = item.First;
  720. // @test Operator Dots
  721. if (item.isKind('mml') && NodeUtil.isEmbellished(top)) {
  722. const tclass = NodeUtil.getTexClass(NodeUtil.getCoreMO(top));
  723. if (tclass === TEXCLASS.BIN || tclass === TEXCLASS.REL) {
  724. dots = this.getProperty('cdots') as MmlNode;
  725. }
  726. }
  727. return [[dots, item], true];
  728. }
  729. }
  730. /**
  731. * Item indicating an array is assembled. It collates cells, rows and
  732. * information about column/row separator and framing lines.
  733. */
  734. export class ArrayItem extends BaseItem {
  735. /**
  736. * The table as a list of rows.
  737. * @type {MmlNode[]}
  738. */
  739. public table: MmlNode[] = [];
  740. /**
  741. * The current row as a list of cells.
  742. * @type {MmlNode[]}
  743. */
  744. public row: MmlNode[] = [];
  745. /**
  746. * Frame specification as a list of strings.
  747. * @type {string[]}
  748. */
  749. public frame: string[] = [];
  750. /**
  751. * Hfill value.
  752. * @type {number[]}
  753. */
  754. public hfill: number[] = [];
  755. /**
  756. * Properties for special array definitions.
  757. * @type {{[key: string]: string|number|boolean}}
  758. */
  759. public arraydef: {[key: string]: string | number | boolean} = {};
  760. /**
  761. * True if separators are dashed.
  762. * @type {boolean}
  763. */
  764. public dashed: boolean = false;
  765. /**
  766. * @override
  767. */
  768. public get kind() {
  769. return 'array';
  770. }
  771. /**
  772. * @override
  773. */
  774. get isOpen() {
  775. return true;
  776. }
  777. /**
  778. * @override
  779. */
  780. get copyEnv() {
  781. return false;
  782. }
  783. /**
  784. * @override
  785. */
  786. public checkItem(item: StackItem): CheckType {
  787. // @test Array Single
  788. if (item.isClose && !item.isKind('over')) {
  789. // @test Array Single
  790. if (item.getProperty('isEntry')) {
  791. // @test Array dashed column, Array solid column
  792. this.EndEntry();
  793. this.clearEnv();
  794. return BaseItem.fail;
  795. }
  796. if (item.getProperty('isCR')) {
  797. // @test Enclosed bottom
  798. this.EndEntry();
  799. this.EndRow();
  800. this.clearEnv();
  801. return BaseItem.fail;
  802. }
  803. this.EndTable();
  804. this.clearEnv();
  805. let newItem = this.factory.create('mml', this.createMml());
  806. if (this.getProperty('requireClose')) {
  807. // @test: Label
  808. if (item.isKind('close')) {
  809. // @test: Label
  810. return [[newItem], true];
  811. }
  812. // @test MissingCloseBrace2
  813. throw new TexError('MissingCloseBrace', 'Missing close brace');
  814. }
  815. return [[newItem, item], true];
  816. }
  817. return super.checkItem(item);
  818. }
  819. /**
  820. * Create the MathML representation of the table.
  821. *
  822. * @return {MmlNode}
  823. */
  824. public createMml(): MmlNode {
  825. const scriptlevel = this.arraydef['scriptlevel'];
  826. delete this.arraydef['scriptlevel'];
  827. let mml = this.create('node', 'mtable', this.table, this.arraydef);
  828. if (scriptlevel) {
  829. mml.setProperty('scriptlevel', scriptlevel);
  830. }
  831. if (this.frame.length === 4) {
  832. // @test Enclosed frame solid, Enclosed frame dashed
  833. NodeUtil.setAttribute(mml, 'frame', this.dashed ? 'dashed' : 'solid');
  834. } else if (this.frame.length) {
  835. // @test Enclosed left right
  836. if (this.arraydef['rowlines']) {
  837. // @test Enclosed dashed row, Enclosed solid row,
  838. this.arraydef['rowlines'] =
  839. (this.arraydef['rowlines'] as string).replace(/none( none)+$/, 'none');
  840. }
  841. // @test Enclosed left right
  842. NodeUtil.setAttribute(mml, 'frame', '');
  843. mml = this.create('node', 'menclose', [mml], {notation: this.frame.join(' ')});
  844. if ((this.arraydef['columnlines'] || 'none') !== 'none' ||
  845. (this.arraydef['rowlines'] || 'none') !== 'none') {
  846. // @test Enclosed dashed row, Enclosed solid row
  847. // @test Enclosed dashed column, Enclosed solid column
  848. NodeUtil.setAttribute(mml, 'data-padding', 0);
  849. }
  850. }
  851. if (this.getProperty('open') || this.getProperty('close')) {
  852. // @test Cross Product Formula
  853. mml = ParseUtil.fenced(this.factory.configuration,
  854. this.getProperty('open') as string, mml,
  855. this.getProperty('close') as string);
  856. }
  857. return mml;
  858. }
  859. /**
  860. * Finishes a single cell of the array.
  861. */
  862. public EndEntry() {
  863. // @test Array1, Array2
  864. const mtd = this.create('node', 'mtd', this.nodes);
  865. if (this.hfill.length) {
  866. if (this.hfill[0] === 0) {
  867. NodeUtil.setAttribute(mtd, 'columnalign', 'right');
  868. }
  869. if (this.hfill[this.hfill.length - 1] === this.Size()) {
  870. NodeUtil.setAttribute(
  871. mtd, 'columnalign',
  872. NodeUtil.getAttribute(mtd, 'columnalign') ? 'center' : 'left');
  873. }
  874. }
  875. this.row.push(mtd);
  876. this.Clear();
  877. this.hfill = [];
  878. }
  879. /**
  880. * Finishes a single row of the array.
  881. */
  882. public EndRow() {
  883. let node: MmlNode;
  884. if (this.getProperty('isNumbered') && this.row.length === 3) {
  885. // @test Label, Matrix Numbered
  886. this.row.unshift(this.row.pop()); // move equation number to first
  887. // position
  888. node = this.create('node', 'mlabeledtr', this.row);
  889. } else {
  890. // @test Array1, Array2
  891. node = this.create('node', 'mtr', this.row);
  892. }
  893. this.table.push(node);
  894. this.row = [];
  895. }
  896. /**
  897. * Finishes the table layout.
  898. */
  899. public EndTable() {
  900. if (this.Size() || this.row.length) {
  901. this.EndEntry();
  902. this.EndRow();
  903. }
  904. this.checkLines();
  905. }
  906. /**
  907. * Finishes line layout if not already given.
  908. */
  909. public checkLines() {
  910. if (this.arraydef['rowlines']) {
  911. const lines = (this.arraydef['rowlines'] as string).split(/ /);
  912. if (lines.length === this.table.length) {
  913. this.frame.push('bottom');
  914. lines.pop();
  915. this.arraydef['rowlines'] = lines.join(' ');
  916. } else if (lines.length < this.table.length - 1) {
  917. this.arraydef['rowlines'] += ' none';
  918. }
  919. }
  920. if (this.getProperty('rowspacing')) {
  921. const rows = (this.arraydef['rowspacing'] as string).split(/ /);
  922. while (rows.length < this.table.length) {
  923. rows.push(this.getProperty('rowspacing') + 'em');
  924. }
  925. this.arraydef['rowspacing'] = rows.join(' ');
  926. }
  927. }
  928. /**
  929. * Adds a row-spacing to the current row (padding out the rowspacing if needed to get there).
  930. *
  931. * @param {string} spacing The rowspacing to use for the current row.
  932. */
  933. public addRowSpacing(spacing: string) {
  934. if (this.arraydef['rowspacing']) {
  935. const rows = (this.arraydef['rowspacing'] as string).split(/ /);
  936. if (!this.getProperty('rowspacing')) {
  937. // @test Array Custom Linebreak
  938. let dimem = ParseUtil.dimen2em(rows[0]);
  939. this.setProperty('rowspacing', dimem);
  940. }
  941. const rowspacing = this.getProperty('rowspacing') as number;
  942. while (rows.length < this.table.length) {
  943. rows.push(ParseUtil.Em(rowspacing));
  944. }
  945. rows[this.table.length - 1] = ParseUtil.Em(
  946. Math.max(0, rowspacing + ParseUtil.dimen2em(spacing)));
  947. this.arraydef['rowspacing'] = rows.join(' ');
  948. }
  949. }
  950. }
  951. /**
  952. * Item dealing with equation arrays as a special case of arrays. Handles
  953. * tagging information according to the given tagging style.
  954. */
  955. export class EqnArrayItem extends ArrayItem {
  956. /**
  957. * The length of the longest row.
  958. */
  959. public maxrow: number = 0;
  960. /**
  961. * @override
  962. */
  963. constructor(factory: any, ...args: any[]) {
  964. super(factory);
  965. this.factory.configuration.tags.start(args[0], args[2], args[1]);
  966. }
  967. /**
  968. * @override
  969. */
  970. get kind() {
  971. return 'eqnarray';
  972. }
  973. /**
  974. * @override
  975. */
  976. public EndEntry() {
  977. // @test Cubic Binomial
  978. if (this.row.length) {
  979. ParseUtil.fixInitialMO(this.factory.configuration, this.nodes);
  980. }
  981. const node = this.create('node', 'mtd', this.nodes);
  982. this.row.push(node);
  983. this.Clear();
  984. }
  985. /**
  986. * @override
  987. */
  988. public EndRow() {
  989. if (this.row.length > this.maxrow) {
  990. this.maxrow = this.row.length;
  991. }
  992. // @test Cubic Binomial
  993. let mtr = 'mtr';
  994. let tag = this.factory.configuration.tags.getTag();
  995. if (tag) {
  996. this.row = [tag].concat(this.row);
  997. mtr = 'mlabeledtr';
  998. }
  999. this.factory.configuration.tags.clearTag();
  1000. const node = this.create('node', mtr, this.row);
  1001. this.table.push(node);
  1002. this.row = [];
  1003. }
  1004. /**
  1005. * @override
  1006. */
  1007. public EndTable() {
  1008. // @test Cubic Binomial
  1009. super.EndTable();
  1010. this.factory.configuration.tags.end();
  1011. //
  1012. // Repeat the column align and width specifications
  1013. // to match the number of columns
  1014. //
  1015. this.extendArray('columnalign', this.maxrow);
  1016. this.extendArray('columnwidth', this.maxrow);
  1017. this.extendArray('columnspacing', this.maxrow - 1);
  1018. }
  1019. /**
  1020. * Extend a column specification to include a repeating set of values
  1021. * so that it has enough to match the maximum row length.
  1022. */
  1023. protected extendArray(name: string, max: number) {
  1024. if (!this.arraydef[name]) return;
  1025. const repeat = (this.arraydef[name] as string).split(/ /);
  1026. const columns = [...repeat];
  1027. if (columns.length > 1) {
  1028. while (columns.length < max) {
  1029. columns.push(...repeat);
  1030. }
  1031. this.arraydef[name] = columns.slice(0, max).join(' ');
  1032. }
  1033. }
  1034. }
  1035. /**
  1036. * Item dealing with simple equation environments. Handles tagging information
  1037. * according to the given tagging style.
  1038. */
  1039. export class EquationItem extends BaseItem {
  1040. /**
  1041. * @override
  1042. */
  1043. constructor(factory: any, ...args: any[]) {
  1044. super(factory);
  1045. this.factory.configuration.tags.start('equation', true, args[0]);
  1046. }
  1047. /**
  1048. * @override
  1049. */
  1050. get kind() {
  1051. return 'equation';
  1052. }
  1053. /**
  1054. * @override
  1055. */
  1056. get isOpen() {
  1057. return true;
  1058. }
  1059. /**
  1060. * @override
  1061. */
  1062. public checkItem(item: StackItem): CheckType {
  1063. if (item.isKind('end')) {
  1064. let mml = this.toMml();
  1065. let tag = this.factory.configuration.tags.getTag();
  1066. this.factory.configuration.tags.end();
  1067. return [[tag ? this.factory.configuration.tags.enTag(mml, tag) : mml, item], true];
  1068. }
  1069. if (item.isKind('stop')) {
  1070. // @test EnvMissingEnd Equation
  1071. throw new TexError('EnvMissingEnd', 'Missing \\end{%1}', this.getName());
  1072. }
  1073. return super.checkItem(item);
  1074. }
  1075. }