mathml.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Implements the MathML InputJax object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AbstractInputJax} from '../core/InputJax.js';
  23. import {defaultOptions, separateOptions, OptionList} from '../util/Options.js';
  24. import {FunctionList} from '../util/FunctionList.js';
  25. import {MathDocument} from '../core/MathDocument.js';
  26. import {MathItem} from '../core/MathItem.js';
  27. import {DOMAdaptor} from '../core/DOMAdaptor.js';
  28. import {MmlFactory} from '../core/MmlTree/MmlFactory.js';
  29. import {FindMathML} from './mathml/FindMathML.js';
  30. import {MathMLCompile} from './mathml/MathMLCompile.js';
  31. /*****************************************************************/
  32. /**
  33. * Implements the MathML class (extends AbstractInputJax)
  34. *
  35. * @template N The HTMLElement node class
  36. * @template T The Text node class
  37. * @template D The Document class
  38. */
  39. export class MathML<N, T, D> extends AbstractInputJax<N, T, D> {
  40. /**
  41. * The name of this input jax
  42. */
  43. public static NAME: string = 'MathML';
  44. /**
  45. * @override
  46. */
  47. public static OPTIONS: OptionList = defaultOptions({
  48. parseAs: 'html', // Whether to use HTML or XML parsing for the MathML string
  49. forceReparse: false, // Whether to force the string to be reparsed, or use the one from the document DOM
  50. FindMathML: null, // The FindMathML instance to override the default one
  51. MathMLCompile: null, // The MathMLCompile instance to override the default one
  52. /*
  53. * The function to use to handle a parsing error (throw an error by default)
  54. */
  55. parseError: function (node: Node) {
  56. this.error(this.adaptor.textContent(node).replace(/\n.*/g, ''));
  57. }
  58. }, AbstractInputJax.OPTIONS);
  59. /**
  60. * The FindMathML instance used to locate MathML in the document
  61. */
  62. protected findMathML: FindMathML<N, T, D>;
  63. /**
  64. * The MathMLCompile instance used to convert the MathML tree to internal format
  65. */
  66. protected mathml: MathMLCompile<N, T, D>;
  67. /**
  68. * A list of functions to call on the parsed MathML DOM before conversion to internal structure
  69. */
  70. public mmlFilters: FunctionList;
  71. /**
  72. * @override
  73. */
  74. constructor(options: OptionList = {}) {
  75. let [mml, find, compile] = separateOptions(options, FindMathML.OPTIONS, MathMLCompile.OPTIONS);
  76. super(mml);
  77. this.findMathML = this.options['FindMathML'] || new FindMathML<N, T, D>(find);
  78. this.mathml = this.options['MathMLCompile'] || new MathMLCompile<N, T, D>(compile);
  79. this.mmlFilters = new FunctionList();
  80. }
  81. /**
  82. * Set the adaptor in any of the objects that need it
  83. *
  84. * @override
  85. */
  86. public setAdaptor(adaptor: DOMAdaptor<N, T, D>) {
  87. super.setAdaptor(adaptor);
  88. this.findMathML.adaptor = adaptor;
  89. this.mathml.adaptor = adaptor;
  90. }
  91. /**
  92. * @param {MmlFactory} mmlFactory The MmlFactory to use for this MathML input jax
  93. */
  94. public setMmlFactory(mmlFactory: MmlFactory) {
  95. super.setMmlFactory(mmlFactory);
  96. this.mathml.setMmlFactory(mmlFactory);
  97. }
  98. /**
  99. * Don't process strings (process nodes)
  100. *
  101. * @override
  102. */
  103. public get processStrings() {
  104. return false;
  105. }
  106. /**
  107. * Convert a MathItem to internal format:
  108. * If there is no existing MathML node, or we are asked to reparse everything
  109. * Execute the preFilters on the math
  110. * Parse the MathML string in the desired format, and check the result for errors
  111. * If we got an HTML document:
  112. * Check that it has only one child (the <math> element), and use it
  113. * Otherwise
  114. * Use the root element from the XML document
  115. * If the node is not a <math> node, report the error.
  116. * Execute the mmlFilters on the parsed MathML
  117. * Compile the MathML to internal format, and execute the postFilters
  118. * Return the resulting internal format
  119. *
  120. * @override
  121. */
  122. public compile(math: MathItem<N, T, D>, document: MathDocument<N, T, D>) {
  123. let mml = math.start.node;
  124. if (!mml || !math.end.node || this.options['forceReparse'] || this.adaptor.kind(mml) === '#text') {
  125. let mathml = this.executeFilters(this.preFilters, math, document, (math.math || '<math></math>').trim());
  126. let doc = this.checkForErrors(this.adaptor.parse(mathml, 'text/' + this.options['parseAs']));
  127. let body = this.adaptor.body(doc);
  128. if (this.adaptor.childNodes(body).length !== 1) {
  129. this.error('MathML must consist of a single element');
  130. }
  131. mml = this.adaptor.remove(this.adaptor.firstChild(body)) as N;
  132. if (this.adaptor.kind(mml).replace(/^[a-z]+:/, '') !== 'math') {
  133. this.error('MathML must be formed by a <math> element, not <' + this.adaptor.kind(mml) + '>');
  134. }
  135. }
  136. mml = this.executeFilters(this.mmlFilters, math, document, mml);
  137. return this.executeFilters(this.postFilters, math, document, this.mathml.compile(mml as N));
  138. }
  139. /**
  140. * Check a parsed MathML string for errors.
  141. *
  142. * @param {D} doc The document returns from the DOMParser
  143. * @return {D} The document
  144. */
  145. protected checkForErrors(doc: D): D {
  146. let err = this.adaptor.tags(this.adaptor.body(doc), 'parsererror')[0];
  147. if (err) {
  148. if (this.adaptor.textContent(err) === '') {
  149. this.error('Error processing MathML');
  150. }
  151. this.options['parseError'].call(this, err);
  152. }
  153. return doc;
  154. }
  155. /**
  156. * Throw an error
  157. *
  158. * @param {string} message The error message to produce
  159. */
  160. protected error(message: string) {
  161. throw new Error(message);
  162. }
  163. /**
  164. * @override
  165. */
  166. public findMath(node: N) {
  167. return this.findMathML.findMath(node);
  168. }
  169. }