BraketItems.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 parsing the braket package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {CheckType, BaseItem, StackItem} from '../StackItem.js';
  23. import {TEXCLASS} from '../../../core/MmlTree/MmlNode.js';
  24. import ParseUtil from '../ParseUtil.js';
  25. /**
  26. * A bra-ket command. Collates elements from the opening brace to the closing
  27. * brace, adding bars to a given maximal number (e.g., only one in case of
  28. * set). To finalise it adds the surrounding angle brackets or braces.
  29. */
  30. export class BraketItem extends BaseItem {
  31. /**
  32. * @override
  33. */
  34. get kind() {
  35. return 'braket';
  36. }
  37. /**
  38. * @override
  39. */
  40. get isOpen() {
  41. return true;
  42. }
  43. /**
  44. * @override
  45. */
  46. public checkItem(item: StackItem): CheckType {
  47. if (item.isKind('close')) {
  48. return [[this.factory.create('mml', this.toMml())], true];
  49. }
  50. if (item.isKind('mml')) {
  51. this.Push(item.toMml());
  52. if (this.getProperty('single')) {
  53. return [[this.toMml()], true];
  54. }
  55. return BaseItem.fail;
  56. }
  57. return super.checkItem(item);
  58. }
  59. /**
  60. * @override
  61. */
  62. public toMml() {
  63. let inner = super.toMml();
  64. let open = this.getProperty('open') as string;
  65. let close = this.getProperty('close') as string;
  66. if (this.getProperty('stretchy')) {
  67. return ParseUtil.fenced(this.factory.configuration, open, inner, close);
  68. }
  69. let attrs = {fence: true, stretchy: false, symmetric: true, texClass: TEXCLASS.OPEN};
  70. let openNode = this.create('token', 'mo', attrs, open);
  71. attrs.texClass = TEXCLASS.CLOSE;
  72. let closeNode = this.create('token', 'mo', attrs, close);
  73. let mrow = this.create('node', 'mrow', [openNode, inner, closeNode],
  74. {open: open, close: close, texClass: TEXCLASS.INNER});
  75. return mrow;
  76. }
  77. }