Wrapper.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-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 Generic Wrapper class for adding methods to a Node class for visitors
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {Node} from './Node.js';
  23. import {WrapperFactory} from './WrapperFactory.js';
  24. /*********************************************************/
  25. /**
  26. * The Wrapper interface
  27. *
  28. * It points to a Node object. Subclasses add methods for the visitor to call.
  29. *
  30. * @template N The Node type being wrapped
  31. * @template W The Wrapper type being produced
  32. */
  33. export interface Wrapper<N extends Node, W extends Wrapper<N, W>> {
  34. node: N;
  35. readonly kind: string;
  36. /**
  37. * @param {Node} node A node to be wrapped
  38. * @param {any[]} args Any additional arguments needed when creating the wrapper
  39. * @return {Wrapper} The wrapped node
  40. */
  41. wrap(node: N, ...args: any[]): W;
  42. }
  43. /*********************************************************/
  44. /**
  45. * The Wrapper class interface
  46. *
  47. * @template N The Node type being wrapped
  48. * @template W The Wrapper type being produced
  49. */
  50. export interface WrapperClass<N extends Node, W extends Wrapper<N, W>> {
  51. /**
  52. * @param {WrapperFactory} factory The factory used to create more wrappers
  53. * @param {N} node The node to be wrapped
  54. * @param {any[]} args Any additional arguments needed when creating the wrapper
  55. * @return {W} The wrapped node
  56. */
  57. new(factory: WrapperFactory<N, W, WrapperClass<N, W>>, node: N, ...args: any[]): W;
  58. }
  59. /*********************************************************/
  60. /**
  61. * The abstract Wrapper class
  62. *
  63. * @template N The Node type being created by the factory
  64. * @template W The Wrapper type being produced
  65. */
  66. export class AbstractWrapper<N extends Node, W extends Wrapper<N, W>> implements Wrapper<N, W> {
  67. /**
  68. * The Node object associated with this instance
  69. */
  70. public node: N;
  71. /**
  72. * The WrapperFactory to use to wrap child nodes, as needed
  73. */
  74. protected factory: WrapperFactory<N, W, WrapperClass<N, W>>;
  75. /**
  76. * The kind of this wrapper
  77. */
  78. get kind() {
  79. return this.node.kind;
  80. }
  81. /**
  82. * @param {WrapperFactory} factory The WrapperFactory to use to wrap child nodes when needed
  83. * @param {Node} node The node to wrap
  84. *
  85. * @constructor
  86. * @implements {Wrapper}
  87. */
  88. constructor(factory: WrapperFactory<N, W, WrapperClass<N, W>>, node: N) {
  89. this.factory = factory;
  90. this.node = node;
  91. }
  92. /**
  93. * @override
  94. */
  95. public wrap(node: N) {
  96. return this.factory.wrap(node);
  97. }
  98. }