FindMath.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Interfaces and abstract classes for FindMath objects
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {userOptions, defaultOptions, OptionList} from '../util/Options.js';
  23. import {ProtoItem} from './MathItem.js';
  24. /*****************************************************************/
  25. /**
  26. * The FindMath interface
  27. *
  28. * @template N The HTMLElement node class
  29. * @template T The Text node class
  30. * @template _D The Document class
  31. */
  32. export interface FindMath<N, T, _D> {
  33. /**
  34. * One of two possibilities: Look through a DOM element,
  35. * or look through an array of strings for delimited math.
  36. *
  37. * @param {N} node The node to search for math
  38. * @return {ProtoItem<N, T>[]}
  39. */
  40. findMath(node: N): ProtoItem<N, T>[];
  41. /**
  42. *
  43. * @param {string[]} strings The strings to search for math
  44. * @return {ProtoItem<N, T>[]}
  45. */
  46. findMath(strings: string[]): ProtoItem<N, T>[];
  47. }
  48. /*****************************************************************/
  49. /**
  50. * The FindMath abstract class
  51. */
  52. /**
  53. * @template N The HTMLElement node class
  54. * @template T The Text node class
  55. * @template D The Document class
  56. */
  57. export abstract class AbstractFindMath<N, T, D> implements FindMath<N, T, D> {
  58. /**
  59. * The default options for FindMath
  60. */
  61. public static OPTIONS: OptionList = {};
  62. /**
  63. * The actual options for this instance
  64. */
  65. protected options: OptionList;
  66. /**
  67. * @param {OptionList} options The user options for this instance
  68. */
  69. constructor(options: OptionList) {
  70. let CLASS = this.constructor as typeof AbstractFindMath;
  71. this.options = userOptions(defaultOptions({}, CLASS.OPTIONS), options);
  72. }
  73. /**
  74. * Locate math in an Element or a string array;
  75. *
  76. * @param {Element | string[]} where The node or string array to search for math
  77. * @return {ProtoItem[]} The array of proto math items found
  78. */
  79. public abstract findMath(where: N | string[]): ProtoItem<N, T>[];
  80. }