semantic-enrich.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __values = (this && this.__values) || function(o) {
  29. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  30. if (m) return m.call(o);
  31. if (o && typeof o.length === "number") return {
  32. next: function () {
  33. if (o && i >= o.length) o = void 0;
  34. return { value: o && o[i++], done: !o };
  35. }
  36. };
  37. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  38. };
  39. var __read = (this && this.__read) || function (o, n) {
  40. var m = typeof Symbol === "function" && o[Symbol.iterator];
  41. if (!m) return o;
  42. var i = m.call(o), r, ar = [], e;
  43. try {
  44. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  45. }
  46. catch (error) { e = { error: error }; }
  47. finally {
  48. try {
  49. if (r && !r.done && (m = i["return"])) m.call(i);
  50. }
  51. finally { if (e) throw e.error; }
  52. }
  53. return ar;
  54. };
  55. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  56. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  57. if (ar || !(i in from)) {
  58. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  59. ar[i] = from[i];
  60. }
  61. }
  62. return to.concat(ar || Array.prototype.slice.call(from));
  63. };
  64. var __importDefault = (this && this.__importDefault) || function (mod) {
  65. return (mod && mod.__esModule) ? mod : { "default": mod };
  66. };
  67. Object.defineProperty(exports, "__esModule", { value: true });
  68. exports.EnrichHandler = exports.EnrichedMathDocumentMixin = exports.EnrichedMathItemMixin = void 0;
  69. var mathjax_js_1 = require("../mathjax.js");
  70. var MathItem_js_1 = require("../core/MathItem.js");
  71. var SerializedMmlVisitor_js_1 = require("../core/MmlTree/SerializedMmlVisitor.js");
  72. var Options_js_1 = require("../util/Options.js");
  73. var sre_js_1 = __importDefault(require("./sre.js"));
  74. var currentSpeech = 'none';
  75. (0, MathItem_js_1.newState)('ENRICHED', 30);
  76. (0, MathItem_js_1.newState)('ATTACHSPEECH', 155);
  77. function EnrichedMathItemMixin(BaseMathItem, MmlJax, toMathML) {
  78. return (function (_super) {
  79. __extends(class_1, _super);
  80. function class_1() {
  81. return _super !== null && _super.apply(this, arguments) || this;
  82. }
  83. class_1.prototype.serializeMml = function (node) {
  84. if ('outerHTML' in node) {
  85. return node.outerHTML;
  86. }
  87. if (typeof Element !== 'undefined' && typeof window !== 'undefined' && node instanceof Element) {
  88. var div = window.document.createElement('div');
  89. div.appendChild(node);
  90. return div.innerHTML;
  91. }
  92. return node.toString();
  93. };
  94. class_1.prototype.enrich = function (document, force) {
  95. if (force === void 0) { force = false; }
  96. if (this.state() >= MathItem_js_1.STATE.ENRICHED)
  97. return;
  98. if (!this.isEscaped && (document.options.enableEnrichment || force)) {
  99. if (document.options.sre.speech !== currentSpeech) {
  100. currentSpeech = document.options.sre.speech;
  101. mathjax_js_1.mathjax.retryAfter(sre_js_1.default.setupEngine(document.options.sre).then(function () { return sre_js_1.default.sreReady(); }));
  102. }
  103. var math = new document.options.MathItem('', MmlJax);
  104. try {
  105. var mml = this.inputData.originalMml = toMathML(this.root);
  106. math.math = this.serializeMml(sre_js_1.default.toEnriched(mml));
  107. math.display = this.display;
  108. math.compile(document);
  109. this.root = math.root;
  110. this.inputData.enrichedMml = math.math;
  111. }
  112. catch (err) {
  113. document.options.enrichError(document, this, err);
  114. }
  115. }
  116. this.state(MathItem_js_1.STATE.ENRICHED);
  117. };
  118. class_1.prototype.attachSpeech = function (document) {
  119. var e_1, _a;
  120. if (this.state() >= MathItem_js_1.STATE.ATTACHSPEECH)
  121. return;
  122. var attributes = this.root.attributes;
  123. var speech = (attributes.get('aria-label') ||
  124. this.getSpeech(this.root));
  125. if (speech) {
  126. var adaptor = document.adaptor;
  127. var node = this.typesetRoot;
  128. adaptor.setAttribute(node, 'aria-label', speech);
  129. try {
  130. for (var _b = __values(adaptor.childNodes(node)), _c = _b.next(); !_c.done; _c = _b.next()) {
  131. var child = _c.value;
  132. adaptor.setAttribute(child, 'aria-hidden', 'true');
  133. }
  134. }
  135. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  136. finally {
  137. try {
  138. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  139. }
  140. finally { if (e_1) throw e_1.error; }
  141. }
  142. }
  143. this.state(MathItem_js_1.STATE.ATTACHSPEECH);
  144. };
  145. class_1.prototype.getSpeech = function (node) {
  146. var e_2, _a;
  147. var attributes = node.attributes;
  148. if (!attributes)
  149. return '';
  150. var speech = attributes.getExplicit('data-semantic-speech');
  151. if (!attributes.getExplicit('data-semantic-parent') && speech) {
  152. return speech;
  153. }
  154. try {
  155. for (var _b = __values(node.childNodes), _c = _b.next(); !_c.done; _c = _b.next()) {
  156. var child = _c.value;
  157. var value = this.getSpeech(child);
  158. if (value != null) {
  159. return value;
  160. }
  161. }
  162. }
  163. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  164. finally {
  165. try {
  166. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  167. }
  168. finally { if (e_2) throw e_2.error; }
  169. }
  170. return '';
  171. };
  172. return class_1;
  173. }(BaseMathItem));
  174. }
  175. exports.EnrichedMathItemMixin = EnrichedMathItemMixin;
  176. function EnrichedMathDocumentMixin(BaseDocument, MmlJax) {
  177. var _a;
  178. return _a = (function (_super) {
  179. __extends(class_2, _super);
  180. function class_2() {
  181. var args = [];
  182. for (var _i = 0; _i < arguments.length; _i++) {
  183. args[_i] = arguments[_i];
  184. }
  185. var _this = _super.apply(this, __spreadArray([], __read(args), false)) || this;
  186. MmlJax.setMmlFactory(_this.mmlFactory);
  187. var ProcessBits = _this.constructor.ProcessBits;
  188. if (!ProcessBits.has('enriched')) {
  189. ProcessBits.allocate('enriched');
  190. ProcessBits.allocate('attach-speech');
  191. }
  192. var visitor = new SerializedMmlVisitor_js_1.SerializedMmlVisitor(_this.mmlFactory);
  193. var toMathML = (function (node) { return visitor.visitTree(node); });
  194. _this.options.MathItem =
  195. EnrichedMathItemMixin(_this.options.MathItem, MmlJax, toMathML);
  196. return _this;
  197. }
  198. class_2.prototype.attachSpeech = function () {
  199. var e_3, _a;
  200. if (!this.processed.isSet('attach-speech')) {
  201. try {
  202. for (var _b = __values(this.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  203. var math = _c.value;
  204. math.attachSpeech(this);
  205. }
  206. }
  207. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  208. finally {
  209. try {
  210. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  211. }
  212. finally { if (e_3) throw e_3.error; }
  213. }
  214. this.processed.set('attach-speech');
  215. }
  216. return this;
  217. };
  218. class_2.prototype.enrich = function () {
  219. var e_4, _a;
  220. if (!this.processed.isSet('enriched')) {
  221. if (this.options.enableEnrichment) {
  222. try {
  223. for (var _b = __values(this.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  224. var math = _c.value;
  225. math.enrich(this);
  226. }
  227. }
  228. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  229. finally {
  230. try {
  231. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  232. }
  233. finally { if (e_4) throw e_4.error; }
  234. }
  235. }
  236. this.processed.set('enriched');
  237. }
  238. return this;
  239. };
  240. class_2.prototype.enrichError = function (_doc, _math, err) {
  241. console.warn('Enrichment error:', err);
  242. };
  243. class_2.prototype.state = function (state, restore) {
  244. if (restore === void 0) { restore = false; }
  245. _super.prototype.state.call(this, state, restore);
  246. if (state < MathItem_js_1.STATE.ENRICHED) {
  247. this.processed.clear('enriched');
  248. }
  249. return this;
  250. };
  251. return class_2;
  252. }(BaseDocument)),
  253. _a.OPTIONS = __assign(__assign({}, BaseDocument.OPTIONS), { enableEnrichment: true, enrichError: function (doc, math, err) { return doc.enrichError(doc, math, err); }, renderActions: (0, Options_js_1.expandable)(__assign(__assign({}, BaseDocument.OPTIONS.renderActions), { enrich: [MathItem_js_1.STATE.ENRICHED], attachSpeech: [MathItem_js_1.STATE.ATTACHSPEECH] })), sre: (0, Options_js_1.expandable)({
  254. speech: 'none',
  255. domain: 'mathspeak',
  256. style: 'default',
  257. locale: 'en'
  258. }) }),
  259. _a;
  260. }
  261. exports.EnrichedMathDocumentMixin = EnrichedMathDocumentMixin;
  262. function EnrichHandler(handler, MmlJax) {
  263. MmlJax.setAdaptor(handler.adaptor);
  264. handler.documentClass =
  265. EnrichedMathDocumentMixin(handler.documentClass, MmlJax);
  266. return handler;
  267. }
  268. exports.EnrichHandler = EnrichHandler;
  269. //# sourceMappingURL=semantic-enrich.js.map