Handler.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Handler objects
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {MathDocument, AbstractMathDocument, MathDocumentConstructor} from './MathDocument.js';
  23. import {OptionList} from '../util/Options.js';
  24. import {DOMAdaptor} from '../core/DOMAdaptor.js';
  25. /*****************************************************************/
  26. /**
  27. * The Handler interface
  28. *
  29. * @template N The HTMLElement node class
  30. * @template T The Text node class
  31. * @template D The Document class
  32. */
  33. export interface Handler<N, T, D> {
  34. /**
  35. * The name of the handler class
  36. */
  37. name: string;
  38. /**
  39. * The DOM Adaptor to use for managing HTML elements
  40. */
  41. adaptor: DOMAdaptor<N, T, D>;
  42. /**
  43. * The priority for the handler when handlers are polled
  44. * to see which one can process a given document.
  45. */
  46. priority: number;
  47. /**
  48. * The class implementing the MathDocument for this handler
  49. * (so it can be subclassed by extensions as needed)
  50. */
  51. documentClass: MathDocumentConstructor<AbstractMathDocument<N, T, D>>;
  52. /**
  53. * Checks to see if the handler can process a given document
  54. *
  55. * @param {any} document The document to be processed (string, window, etc.)
  56. * @return {boolean} True if this handler can process the given document
  57. */
  58. handlesDocument(document: any): boolean;
  59. /**
  60. * Creates a MathDocument for the given handler
  61. *
  62. * @param {any} document The document to be handled
  63. * @param {OptionList} options The options for the handling of the document
  64. * @return {MathDocument} The MathDocument object that manages the processing
  65. */
  66. create(document: any, options: OptionList): MathDocument<N, T, D>;
  67. }
  68. /*****************************************************************/
  69. /**
  70. * The default MathDocument class (subclasses use their own)
  71. *
  72. * @template N The HTMLElement node class
  73. * @template T The Text node class
  74. * @template D The Document class
  75. */
  76. class DefaultMathDocument<N, T, D> extends AbstractMathDocument<N, T, D> {}
  77. /*****************************************************************/
  78. /**
  79. * The Handler interface
  80. *
  81. * @template N The HTMLElement node class
  82. * @template T The Text node class
  83. * @template D The Document class
  84. */
  85. export abstract class AbstractHandler<N, T, D> implements Handler<N, T, D> {
  86. /**
  87. * The name of this class
  88. */
  89. public static NAME: string = 'generic';
  90. /**
  91. * The DOM Adaptor to use for managing HTML elements
  92. */
  93. public adaptor: DOMAdaptor<N, T, D>;
  94. /**
  95. * The priority for this handler
  96. */
  97. public priority: number;
  98. /**
  99. * The class implementing the MathDocument for this handler
  100. * (so it can be subclassed by extensions as needed)
  101. */
  102. public documentClass: MathDocumentConstructor<AbstractMathDocument<N, T, D>> = DefaultMathDocument;
  103. /**
  104. * @param {number} priority The priority to use for this handler
  105. *
  106. * @constructor
  107. */
  108. constructor(adaptor: DOMAdaptor<N, T, D>, priority: number = 5) {
  109. this.adaptor = adaptor;
  110. this.priority = priority;
  111. }
  112. /**
  113. * @return {string} The name of this handler class
  114. */
  115. public get name(): string {
  116. return (this.constructor as typeof AbstractHandler).NAME;
  117. }
  118. /**
  119. * @override
  120. */
  121. public handlesDocument(_document: any) {
  122. return false;
  123. }
  124. /**
  125. * @override
  126. */
  127. public create(document: any, options: OptionList) {
  128. return new this.documentClass(document, this.adaptor, options) as MathDocument<N, T, D>;
  129. }
  130. }