MathMLCompile.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  14. if (k2 === undefined) k2 = k;
  15. var desc = Object.getOwnPropertyDescriptor(m, k);
  16. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  17. desc = { enumerable: true, get: function() { return m[k]; } };
  18. }
  19. Object.defineProperty(o, k2, desc);
  20. }) : (function(o, m, k, k2) {
  21. if (k2 === undefined) k2 = k;
  22. o[k2] = m[k];
  23. }));
  24. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  25. Object.defineProperty(o, "default", { enumerable: true, value: v });
  26. }) : function(o, v) {
  27. o["default"] = v;
  28. });
  29. var __importStar = (this && this.__importStar) || function (mod) {
  30. if (mod && mod.__esModule) return mod;
  31. var result = {};
  32. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  33. __setModuleDefault(result, mod);
  34. return result;
  35. };
  36. var __values = (this && this.__values) || function(o) {
  37. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  38. if (m) return m.call(o);
  39. if (o && typeof o.length === "number") return {
  40. next: function () {
  41. if (o && i >= o.length) o = void 0;
  42. return { value: o && o[i++], done: !o };
  43. }
  44. };
  45. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  46. };
  47. Object.defineProperty(exports, "__esModule", { value: true });
  48. exports.MathMLCompile = void 0;
  49. var MmlNode_js_1 = require("../../core/MmlTree/MmlNode.js");
  50. var Options_js_1 = require("../../util/Options.js");
  51. var Entities = __importStar(require("../../util/Entities.js"));
  52. var MathMLCompile = (function () {
  53. function MathMLCompile(options) {
  54. if (options === void 0) { options = {}; }
  55. var Class = this.constructor;
  56. this.options = (0, Options_js_1.userOptions)((0, Options_js_1.defaultOptions)({}, Class.OPTIONS), options);
  57. }
  58. MathMLCompile.prototype.setMmlFactory = function (mmlFactory) {
  59. this.factory = mmlFactory;
  60. };
  61. MathMLCompile.prototype.compile = function (node) {
  62. var mml = this.makeNode(node);
  63. mml.verifyTree(this.options['verify']);
  64. mml.setInheritedAttributes({}, false, 0, false);
  65. mml.walkTree(this.markMrows);
  66. return mml;
  67. };
  68. MathMLCompile.prototype.makeNode = function (node) {
  69. var e_1, _a;
  70. var adaptor = this.adaptor;
  71. var limits = false;
  72. var kind = adaptor.kind(node).replace(/^.*:/, '');
  73. var texClass = adaptor.getAttribute(node, 'data-mjx-texclass') || '';
  74. if (texClass) {
  75. texClass = this.filterAttribute('data-mjx-texclass', texClass) || '';
  76. }
  77. var type = texClass && kind === 'mrow' ? 'TeXAtom' : kind;
  78. try {
  79. for (var _b = __values(this.filterClassList(adaptor.allClasses(node))), _c = _b.next(); !_c.done; _c = _b.next()) {
  80. var name_1 = _c.value;
  81. if (name_1.match(/^MJX-TeXAtom-/) && kind === 'mrow') {
  82. texClass = name_1.substr(12);
  83. type = 'TeXAtom';
  84. }
  85. else if (name_1 === 'MJX-fixedlimits') {
  86. limits = true;
  87. }
  88. }
  89. }
  90. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  91. finally {
  92. try {
  93. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  94. }
  95. finally { if (e_1) throw e_1.error; }
  96. }
  97. this.factory.getNodeClass(type) || this.error('Unknown node type "' + type + '"');
  98. var mml = this.factory.create(type);
  99. if (type === 'TeXAtom' && texClass === 'OP' && !limits) {
  100. mml.setProperty('movesupsub', true);
  101. mml.attributes.setInherited('movablelimits', true);
  102. }
  103. if (texClass) {
  104. mml.texClass = MmlNode_js_1.TEXCLASS[texClass];
  105. mml.setProperty('texClass', mml.texClass);
  106. }
  107. this.addAttributes(mml, node);
  108. this.checkClass(mml, node);
  109. this.addChildren(mml, node);
  110. return mml;
  111. };
  112. MathMLCompile.prototype.addAttributes = function (mml, node) {
  113. var e_2, _a;
  114. var ignoreVariant = false;
  115. try {
  116. for (var _b = __values(this.adaptor.allAttributes(node)), _c = _b.next(); !_c.done; _c = _b.next()) {
  117. var attr = _c.value;
  118. var name_2 = attr.name;
  119. var value = this.filterAttribute(name_2, attr.value);
  120. if (value === null || name_2 === 'xmlns') {
  121. continue;
  122. }
  123. if (name_2.substr(0, 9) === 'data-mjx-') {
  124. switch (name_2.substr(9)) {
  125. case 'alternate':
  126. mml.setProperty('variantForm', true);
  127. break;
  128. case 'variant':
  129. mml.attributes.set('mathvariant', value);
  130. ignoreVariant = true;
  131. break;
  132. case 'smallmatrix':
  133. mml.setProperty('scriptlevel', 1);
  134. mml.setProperty('useHeight', false);
  135. break;
  136. case 'accent':
  137. mml.setProperty('mathaccent', value === 'true');
  138. break;
  139. case 'auto-op':
  140. mml.setProperty('autoOP', value === 'true');
  141. break;
  142. case 'script-align':
  143. mml.setProperty('scriptalign', value);
  144. break;
  145. }
  146. }
  147. else if (name_2 !== 'class') {
  148. var val = value.toLowerCase();
  149. if (val === 'true' || val === 'false') {
  150. mml.attributes.set(name_2, val === 'true');
  151. }
  152. else if (!ignoreVariant || name_2 !== 'mathvariant') {
  153. mml.attributes.set(name_2, value);
  154. }
  155. }
  156. }
  157. }
  158. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  159. finally {
  160. try {
  161. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  162. }
  163. finally { if (e_2) throw e_2.error; }
  164. }
  165. };
  166. MathMLCompile.prototype.filterAttribute = function (_name, value) {
  167. return value;
  168. };
  169. MathMLCompile.prototype.filterClassList = function (list) {
  170. return list;
  171. };
  172. MathMLCompile.prototype.addChildren = function (mml, node) {
  173. var e_3, _a;
  174. if (mml.arity === 0) {
  175. return;
  176. }
  177. var adaptor = this.adaptor;
  178. try {
  179. for (var _b = __values(adaptor.childNodes(node)), _c = _b.next(); !_c.done; _c = _b.next()) {
  180. var child = _c.value;
  181. var name_3 = adaptor.kind(child);
  182. if (name_3 === '#comment') {
  183. continue;
  184. }
  185. if (name_3 === '#text') {
  186. this.addText(mml, child);
  187. }
  188. else if (mml.isKind('annotation-xml')) {
  189. mml.appendChild(this.factory.create('XML').setXML(child, adaptor));
  190. }
  191. else {
  192. var childMml = mml.appendChild(this.makeNode(child));
  193. if (childMml.arity === 0 && adaptor.childNodes(child).length) {
  194. if (this.options['fixMisplacedChildren']) {
  195. this.addChildren(mml, child);
  196. }
  197. else {
  198. childMml.mError('There should not be children for ' + childMml.kind + ' nodes', this.options['verify'], true);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  205. finally {
  206. try {
  207. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  208. }
  209. finally { if (e_3) throw e_3.error; }
  210. }
  211. };
  212. MathMLCompile.prototype.addText = function (mml, child) {
  213. var text = this.adaptor.value(child);
  214. if ((mml.isToken || mml.getProperty('isChars')) && mml.arity) {
  215. if (mml.isToken) {
  216. text = Entities.translate(text);
  217. text = this.trimSpace(text);
  218. }
  219. mml.appendChild(this.factory.create('text').setText(text));
  220. }
  221. else if (text.match(/\S/)) {
  222. this.error('Unexpected text node "' + text + '"');
  223. }
  224. };
  225. MathMLCompile.prototype.checkClass = function (mml, node) {
  226. var e_4, _a;
  227. var classList = [];
  228. try {
  229. for (var _b = __values(this.filterClassList(this.adaptor.allClasses(node))), _c = _b.next(); !_c.done; _c = _b.next()) {
  230. var name_4 = _c.value;
  231. if (name_4.substr(0, 4) === 'MJX-') {
  232. if (name_4 === 'MJX-variant') {
  233. mml.setProperty('variantForm', true);
  234. }
  235. else if (name_4.substr(0, 11) !== 'MJX-TeXAtom') {
  236. mml.attributes.set('mathvariant', this.fixCalligraphic(name_4.substr(3)));
  237. }
  238. }
  239. else {
  240. classList.push(name_4);
  241. }
  242. }
  243. }
  244. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  245. finally {
  246. try {
  247. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  248. }
  249. finally { if (e_4) throw e_4.error; }
  250. }
  251. if (classList.length) {
  252. mml.attributes.set('class', classList.join(' '));
  253. }
  254. };
  255. MathMLCompile.prototype.fixCalligraphic = function (variant) {
  256. return variant.replace(/caligraphic/, 'calligraphic');
  257. };
  258. MathMLCompile.prototype.markMrows = function (mml) {
  259. if (mml.isKind('mrow') && !mml.isInferred && mml.childNodes.length >= 2) {
  260. var first = mml.childNodes[0];
  261. var last = mml.childNodes[mml.childNodes.length - 1];
  262. if (first.isKind('mo') && first.attributes.get('fence') && first.attributes.get('stretchy') &&
  263. last.isKind('mo') && last.attributes.get('fence') && last.attributes.get('stretchy')) {
  264. if (first.childNodes.length) {
  265. mml.setProperty('open', first.getText());
  266. }
  267. if (last.childNodes.length) {
  268. mml.setProperty('close', last.getText());
  269. }
  270. }
  271. }
  272. };
  273. MathMLCompile.prototype.trimSpace = function (text) {
  274. return text.replace(/[\t\n\r]/g, ' ')
  275. .replace(/^ +/, '')
  276. .replace(/ +$/, '')
  277. .replace(/ +/g, ' ');
  278. };
  279. MathMLCompile.prototype.error = function (message) {
  280. throw new Error(message);
  281. };
  282. MathMLCompile.OPTIONS = {
  283. MmlFactory: null,
  284. fixMisplacedChildren: true,
  285. verify: __assign({}, MmlNode_js_1.AbstractMmlNode.verifyDefaults),
  286. translateEntities: true
  287. };
  288. return MathMLCompile;
  289. }());
  290. exports.MathMLCompile = MathMLCompile;
  291. //# sourceMappingURL=MathMLCompile.js.map