NewcommandItems.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Items for TeX parsing of new environments.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import TexError from '../TexError.js';
  23. import {CheckType, BaseItem, StackItem} from '../StackItem.js';
  24. /**
  25. * Opening Item dealing with definitions of new environments. It's pushed onto
  26. * the stack whenever a user defined environment is encountered and remains
  27. * until a corresponding \\end collapses the stack.
  28. */
  29. export class BeginEnvItem extends BaseItem {
  30. /**
  31. * @override
  32. */
  33. public get kind() {
  34. return 'beginEnv';
  35. }
  36. /**
  37. * @override
  38. */
  39. get isOpen() {
  40. return true;
  41. }
  42. /**
  43. * @override
  44. */
  45. public checkItem(item: StackItem): CheckType {
  46. if (item.isKind('end')) {
  47. // @test Newenvironment Empty, Newenvironment Align
  48. if (item.getName() !== this.getName()) {
  49. // @test (missing) \newenvironment{env}{aa}{bb}\begin{env}cc\end{equation}
  50. throw new TexError('EnvBadEnd', '\\begin{%1} ended with \\end{%2}',
  51. this.getName(), item.getName());
  52. }
  53. return [[this.factory.create('mml', this.toMml())], true];
  54. }
  55. if (item.isKind('stop')) {
  56. // @test (missing) \newenvironment{env}{aa}{bb}\begin{env}cc
  57. throw new TexError('EnvMissingEnd', 'Missing \\end{%1}', this.getName());
  58. }
  59. // @test Newenvironment Empty, Newenvironment Align
  60. return super.checkItem(item);
  61. }
  62. }