assistive-mml.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 __read = (this && this.__read) || function (o, n) {
  29. var m = typeof Symbol === "function" && o[Symbol.iterator];
  30. if (!m) return o;
  31. var i = m.call(o), r, ar = [], e;
  32. try {
  33. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  34. }
  35. catch (error) { e = { error: error }; }
  36. finally {
  37. try {
  38. if (r && !r.done && (m = i["return"])) m.call(i);
  39. }
  40. finally { if (e) throw e.error; }
  41. }
  42. return ar;
  43. };
  44. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  45. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  46. if (ar || !(i in from)) {
  47. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  48. ar[i] = from[i];
  49. }
  50. }
  51. return to.concat(ar || Array.prototype.slice.call(from));
  52. };
  53. var __values = (this && this.__values) || function(o) {
  54. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  55. if (m) return m.call(o);
  56. if (o && typeof o.length === "number") return {
  57. next: function () {
  58. if (o && i >= o.length) o = void 0;
  59. return { value: o && o[i++], done: !o };
  60. }
  61. };
  62. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  63. };
  64. Object.defineProperty(exports, "__esModule", { value: true });
  65. exports.AssistiveMmlHandler = exports.AssistiveMmlMathDocumentMixin = exports.AssistiveMmlMathItemMixin = exports.LimitedMmlVisitor = void 0;
  66. var MathItem_js_1 = require("../core/MathItem.js");
  67. var SerializedMmlVisitor_js_1 = require("../core/MmlTree/SerializedMmlVisitor.js");
  68. var Options_js_1 = require("../util/Options.js");
  69. var LimitedMmlVisitor = (function (_super) {
  70. __extends(LimitedMmlVisitor, _super);
  71. function LimitedMmlVisitor() {
  72. return _super !== null && _super.apply(this, arguments) || this;
  73. }
  74. LimitedMmlVisitor.prototype.getAttributes = function (node) {
  75. return _super.prototype.getAttributes.call(this, node).replace(/ ?id=".*?"/, '');
  76. };
  77. return LimitedMmlVisitor;
  78. }(SerializedMmlVisitor_js_1.SerializedMmlVisitor));
  79. exports.LimitedMmlVisitor = LimitedMmlVisitor;
  80. (0, MathItem_js_1.newState)('ASSISTIVEMML', 153);
  81. function AssistiveMmlMathItemMixin(BaseMathItem) {
  82. return (function (_super) {
  83. __extends(class_1, _super);
  84. function class_1() {
  85. return _super !== null && _super.apply(this, arguments) || this;
  86. }
  87. class_1.prototype.assistiveMml = function (document, force) {
  88. if (force === void 0) { force = false; }
  89. if (this.state() >= MathItem_js_1.STATE.ASSISTIVEMML)
  90. return;
  91. if (!this.isEscaped && (document.options.enableAssistiveMml || force)) {
  92. var adaptor = document.adaptor;
  93. var mml = document.toMML(this.root).replace(/\n */g, '').replace(/<!--.*?-->/g, '');
  94. var mmlNodes = adaptor.firstChild(adaptor.body(adaptor.parse(mml, 'text/html')));
  95. var node = adaptor.node('mjx-assistive-mml', {
  96. unselectable: 'on', display: (this.display ? 'block' : 'inline')
  97. }, [mmlNodes]);
  98. adaptor.setAttribute(adaptor.firstChild(this.typesetRoot), 'aria-hidden', 'true');
  99. adaptor.setStyle(this.typesetRoot, 'position', 'relative');
  100. adaptor.append(this.typesetRoot, node);
  101. }
  102. this.state(MathItem_js_1.STATE.ASSISTIVEMML);
  103. };
  104. return class_1;
  105. }(BaseMathItem));
  106. }
  107. exports.AssistiveMmlMathItemMixin = AssistiveMmlMathItemMixin;
  108. function AssistiveMmlMathDocumentMixin(BaseDocument) {
  109. var _a;
  110. return _a = (function (_super) {
  111. __extends(BaseClass, _super);
  112. function BaseClass() {
  113. var args = [];
  114. for (var _i = 0; _i < arguments.length; _i++) {
  115. args[_i] = arguments[_i];
  116. }
  117. var _this = _super.apply(this, __spreadArray([], __read(args), false)) || this;
  118. var CLASS = _this.constructor;
  119. var ProcessBits = CLASS.ProcessBits;
  120. if (!ProcessBits.has('assistive-mml')) {
  121. ProcessBits.allocate('assistive-mml');
  122. }
  123. _this.visitor = new LimitedMmlVisitor(_this.mmlFactory);
  124. _this.options.MathItem =
  125. AssistiveMmlMathItemMixin(_this.options.MathItem);
  126. if ('addStyles' in _this) {
  127. _this.addStyles(CLASS.assistiveStyles);
  128. }
  129. return _this;
  130. }
  131. BaseClass.prototype.toMML = function (node) {
  132. return this.visitor.visitTree(node);
  133. };
  134. BaseClass.prototype.assistiveMml = function () {
  135. var e_1, _a;
  136. if (!this.processed.isSet('assistive-mml')) {
  137. try {
  138. for (var _b = __values(this.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  139. var math = _c.value;
  140. math.assistiveMml(this);
  141. }
  142. }
  143. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  144. finally {
  145. try {
  146. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  147. }
  148. finally { if (e_1) throw e_1.error; }
  149. }
  150. this.processed.set('assistive-mml');
  151. }
  152. return this;
  153. };
  154. BaseClass.prototype.state = function (state, restore) {
  155. if (restore === void 0) { restore = false; }
  156. _super.prototype.state.call(this, state, restore);
  157. if (state < MathItem_js_1.STATE.ASSISTIVEMML) {
  158. this.processed.clear('assistive-mml');
  159. }
  160. return this;
  161. };
  162. return BaseClass;
  163. }(BaseDocument)),
  164. _a.OPTIONS = __assign(__assign({}, BaseDocument.OPTIONS), { enableAssistiveMml: true, renderActions: (0, Options_js_1.expandable)(__assign(__assign({}, BaseDocument.OPTIONS.renderActions), { assistiveMml: [MathItem_js_1.STATE.ASSISTIVEMML] })) }),
  165. _a.assistiveStyles = {
  166. 'mjx-assistive-mml': {
  167. position: 'absolute !important',
  168. top: '0px', left: '0px',
  169. clip: 'rect(1px, 1px, 1px, 1px)',
  170. padding: '1px 0px 0px 0px !important',
  171. border: '0px !important',
  172. display: 'block !important',
  173. width: 'auto !important',
  174. overflow: 'hidden !important',
  175. '-webkit-touch-callout': 'none',
  176. '-webkit-user-select': 'none',
  177. '-khtml-user-select': 'none',
  178. '-moz-user-select': 'none',
  179. '-ms-user-select': 'none',
  180. 'user-select': 'none'
  181. },
  182. 'mjx-assistive-mml[display="block"]': {
  183. width: '100% !important'
  184. }
  185. },
  186. _a;
  187. }
  188. exports.AssistiveMmlMathDocumentMixin = AssistiveMmlMathDocumentMixin;
  189. function AssistiveMmlHandler(handler) {
  190. handler.documentClass =
  191. AssistiveMmlMathDocumentMixin(handler.documentClass);
  192. return handler;
  193. }
  194. exports.AssistiveMmlHandler = AssistiveMmlHandler;
  195. //# sourceMappingURL=assistive-mml.js.map