PhysicsItems.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 the physics package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {CheckType, BaseItem, StackItem} from '../StackItem.js';
  23. import ParseUtil from '../ParseUtil.js';
  24. import NodeUtil from '../NodeUtil.js';
  25. import TexParser from '../TexParser.js';
  26. import {AbstractMmlTokenNode} from '../../../core/MmlTree/MmlNode.js';
  27. export class AutoOpen extends BaseItem {
  28. /**
  29. * @override
  30. */
  31. protected static errors = Object.assign(Object.create(BaseItem.errors), {
  32. 'stop': ['ExtraOrMissingDelims', 'Extra open or missing close delimiter']
  33. });
  34. /**
  35. * The number of unpaired open delimiters that need to be matched before
  36. * a close delimiter will close this item. (#2831)
  37. */
  38. protected openCount: number = 0;
  39. /**
  40. * @override
  41. */
  42. public get kind() {
  43. return 'auto open';
  44. }
  45. /**
  46. * @override
  47. */
  48. get isOpen() {
  49. return true;
  50. }
  51. /**
  52. * @override
  53. */
  54. public toMml() {
  55. // Smash and right/left
  56. let parser = this.factory.configuration.parser;
  57. let right = this.getProperty('right') as string;
  58. if (this.getProperty('smash')) {
  59. let mml = super.toMml();
  60. const smash = parser.create('node', 'mpadded', [mml],
  61. {height: 0, depth: 0});
  62. this.Clear();
  63. this.Push(parser.create('node', 'TeXAtom', [smash]));
  64. }
  65. if (right) {
  66. this.Push(new TexParser(right, parser.stack.env,
  67. parser.configuration).mml());
  68. }
  69. let mml = ParseUtil.fenced(
  70. this.factory.configuration,
  71. this.getProperty('open') as string,
  72. super.toMml(),
  73. this.getProperty('close') as string,
  74. this.getProperty('big') as string
  75. );
  76. //
  77. // Remove fence markers that would cause it to be TeX class INNER,
  78. // so it is treated as a regular mrow when setting the tex class (#2760)
  79. //
  80. NodeUtil.removeProperties(mml, 'open', 'close', 'texClass');
  81. return mml;
  82. }
  83. /**
  84. * @override
  85. */
  86. public checkItem(item: StackItem): CheckType {
  87. //
  88. // Check for nested open delimiters (#2831)
  89. //
  90. if (item.isKind('mml') && item.Size() === 1) {
  91. const mml = item.toMml();
  92. if (mml.isKind('mo') && (mml as AbstractMmlTokenNode).getText() === this.getProperty('open')) {
  93. this.openCount++;
  94. }
  95. }
  96. let close = item.getProperty('autoclose');
  97. if (close && close === this.getProperty('close') && !this.openCount--) {
  98. if (this.getProperty('ignore')) {
  99. this.Clear();
  100. return [[], true];
  101. }
  102. return [[this.toMml()], true];
  103. }
  104. return super.checkItem(item);
  105. }
  106. }