mathml.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __read = (this && this.__read) || function (o, n) {
  18. var m = typeof Symbol === "function" && o[Symbol.iterator];
  19. if (!m) return o;
  20. var i = m.call(o), r, ar = [], e;
  21. try {
  22. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  23. }
  24. catch (error) { e = { error: error }; }
  25. finally {
  26. try {
  27. if (r && !r.done && (m = i["return"])) m.call(i);
  28. }
  29. finally { if (e) throw e.error; }
  30. }
  31. return ar;
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. exports.MathML = void 0;
  35. var InputJax_js_1 = require("../core/InputJax.js");
  36. var Options_js_1 = require("../util/Options.js");
  37. var FunctionList_js_1 = require("../util/FunctionList.js");
  38. var FindMathML_js_1 = require("./mathml/FindMathML.js");
  39. var MathMLCompile_js_1 = require("./mathml/MathMLCompile.js");
  40. var MathML = (function (_super) {
  41. __extends(MathML, _super);
  42. function MathML(options) {
  43. if (options === void 0) { options = {}; }
  44. var _this = this;
  45. var _a = __read((0, Options_js_1.separateOptions)(options, FindMathML_js_1.FindMathML.OPTIONS, MathMLCompile_js_1.MathMLCompile.OPTIONS), 3), mml = _a[0], find = _a[1], compile = _a[2];
  46. _this = _super.call(this, mml) || this;
  47. _this.findMathML = _this.options['FindMathML'] || new FindMathML_js_1.FindMathML(find);
  48. _this.mathml = _this.options['MathMLCompile'] || new MathMLCompile_js_1.MathMLCompile(compile);
  49. _this.mmlFilters = new FunctionList_js_1.FunctionList();
  50. return _this;
  51. }
  52. MathML.prototype.setAdaptor = function (adaptor) {
  53. _super.prototype.setAdaptor.call(this, adaptor);
  54. this.findMathML.adaptor = adaptor;
  55. this.mathml.adaptor = adaptor;
  56. };
  57. MathML.prototype.setMmlFactory = function (mmlFactory) {
  58. _super.prototype.setMmlFactory.call(this, mmlFactory);
  59. this.mathml.setMmlFactory(mmlFactory);
  60. };
  61. Object.defineProperty(MathML.prototype, "processStrings", {
  62. get: function () {
  63. return false;
  64. },
  65. enumerable: false,
  66. configurable: true
  67. });
  68. MathML.prototype.compile = function (math, document) {
  69. var mml = math.start.node;
  70. if (!mml || !math.end.node || this.options['forceReparse'] || this.adaptor.kind(mml) === '#text') {
  71. var mathml = this.executeFilters(this.preFilters, math, document, (math.math || '<math></math>').trim());
  72. var doc = this.checkForErrors(this.adaptor.parse(mathml, 'text/' + this.options['parseAs']));
  73. var body = this.adaptor.body(doc);
  74. if (this.adaptor.childNodes(body).length !== 1) {
  75. this.error('MathML must consist of a single element');
  76. }
  77. mml = this.adaptor.remove(this.adaptor.firstChild(body));
  78. if (this.adaptor.kind(mml).replace(/^[a-z]+:/, '') !== 'math') {
  79. this.error('MathML must be formed by a <math> element, not <' + this.adaptor.kind(mml) + '>');
  80. }
  81. }
  82. mml = this.executeFilters(this.mmlFilters, math, document, mml);
  83. return this.executeFilters(this.postFilters, math, document, this.mathml.compile(mml));
  84. };
  85. MathML.prototype.checkForErrors = function (doc) {
  86. var err = this.adaptor.tags(this.adaptor.body(doc), 'parsererror')[0];
  87. if (err) {
  88. if (this.adaptor.textContent(err) === '') {
  89. this.error('Error processing MathML');
  90. }
  91. this.options['parseError'].call(this, err);
  92. }
  93. return doc;
  94. };
  95. MathML.prototype.error = function (message) {
  96. throw new Error(message);
  97. };
  98. MathML.prototype.findMath = function (node) {
  99. return this.findMathML.findMath(node);
  100. };
  101. MathML.NAME = 'MathML';
  102. MathML.OPTIONS = (0, Options_js_1.defaultOptions)({
  103. parseAs: 'html',
  104. forceReparse: false,
  105. FindMathML: null,
  106. MathMLCompile: null,
  107. parseError: function (node) {
  108. this.error(this.adaptor.textContent(node).replace(/\n.*/g, ''));
  109. }
  110. }, InputJax_js_1.AbstractInputJax.OPTIONS);
  111. return MathML;
  112. }(InputJax_js_1.AbstractInputJax));
  113. exports.MathML = MathML;
  114. //# sourceMappingURL=mathml.js.map