HTMLDocument.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 __values = (this && this.__values) || function(o) {
  45. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  46. if (m) return m.call(o);
  47. if (o && typeof o.length === "number") return {
  48. next: function () {
  49. if (o && i >= o.length) o = void 0;
  50. return { value: o && o[i++], done: !o };
  51. }
  52. };
  53. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  54. };
  55. Object.defineProperty(exports, "__esModule", { value: true });
  56. exports.HTMLDocument = void 0;
  57. var MathDocument_js_1 = require("../../core/MathDocument.js");
  58. var Options_js_1 = require("../../util/Options.js");
  59. var HTMLMathItem_js_1 = require("./HTMLMathItem.js");
  60. var HTMLMathList_js_1 = require("./HTMLMathList.js");
  61. var HTMLDomStrings_js_1 = require("./HTMLDomStrings.js");
  62. var MathItem_js_1 = require("../../core/MathItem.js");
  63. var HTMLDocument = (function (_super) {
  64. __extends(HTMLDocument, _super);
  65. function HTMLDocument(document, adaptor, options) {
  66. var _this = this;
  67. var _a = __read((0, Options_js_1.separateOptions)(options, HTMLDomStrings_js_1.HTMLDomStrings.OPTIONS), 2), html = _a[0], dom = _a[1];
  68. _this = _super.call(this, document, adaptor, html) || this;
  69. _this.domStrings = _this.options['DomStrings'] || new HTMLDomStrings_js_1.HTMLDomStrings(dom);
  70. _this.domStrings.adaptor = adaptor;
  71. _this.styles = [];
  72. return _this;
  73. }
  74. HTMLDocument.prototype.findPosition = function (N, index, delim, nodes) {
  75. var e_1, _a;
  76. var adaptor = this.adaptor;
  77. try {
  78. for (var _b = __values(nodes[N]), _c = _b.next(); !_c.done; _c = _b.next()) {
  79. var list = _c.value;
  80. var _d = __read(list, 2), node = _d[0], n = _d[1];
  81. if (index <= n && adaptor.kind(node) === '#text') {
  82. return { node: node, n: Math.max(index, 0), delim: delim };
  83. }
  84. index -= n;
  85. }
  86. }
  87. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  88. finally {
  89. try {
  90. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  91. }
  92. finally { if (e_1) throw e_1.error; }
  93. }
  94. return { node: null, n: 0, delim: delim };
  95. };
  96. HTMLDocument.prototype.mathItem = function (item, jax, nodes) {
  97. var math = item.math;
  98. var start = this.findPosition(item.n, item.start.n, item.open, nodes);
  99. var end = this.findPosition(item.n, item.end.n, item.close, nodes);
  100. return new this.options.MathItem(math, jax, item.display, start, end);
  101. };
  102. HTMLDocument.prototype.findMath = function (options) {
  103. var e_2, _a, e_3, _b, _c, e_4, _d, e_5, _e;
  104. if (!this.processed.isSet('findMath')) {
  105. this.adaptor.document = this.document;
  106. options = (0, Options_js_1.userOptions)({ elements: this.options.elements || [this.adaptor.body(this.document)] }, options);
  107. try {
  108. for (var _f = __values(this.adaptor.getElements(options['elements'], this.document)), _g = _f.next(); !_g.done; _g = _f.next()) {
  109. var container = _g.value;
  110. var _h = __read([null, null], 2), strings = _h[0], nodes = _h[1];
  111. try {
  112. for (var _j = (e_3 = void 0, __values(this.inputJax)), _k = _j.next(); !_k.done; _k = _j.next()) {
  113. var jax = _k.value;
  114. var list = new (this.options['MathList'])();
  115. if (jax.processStrings) {
  116. if (strings === null) {
  117. _c = __read(this.domStrings.find(container), 2), strings = _c[0], nodes = _c[1];
  118. }
  119. try {
  120. for (var _l = (e_4 = void 0, __values(jax.findMath(strings))), _m = _l.next(); !_m.done; _m = _l.next()) {
  121. var math = _m.value;
  122. list.push(this.mathItem(math, jax, nodes));
  123. }
  124. }
  125. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  126. finally {
  127. try {
  128. if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
  129. }
  130. finally { if (e_4) throw e_4.error; }
  131. }
  132. }
  133. else {
  134. try {
  135. for (var _o = (e_5 = void 0, __values(jax.findMath(container))), _p = _o.next(); !_p.done; _p = _o.next()) {
  136. var math = _p.value;
  137. var item = new this.options.MathItem(math.math, jax, math.display, math.start, math.end);
  138. list.push(item);
  139. }
  140. }
  141. catch (e_5_1) { e_5 = { error: e_5_1 }; }
  142. finally {
  143. try {
  144. if (_p && !_p.done && (_e = _o.return)) _e.call(_o);
  145. }
  146. finally { if (e_5) throw e_5.error; }
  147. }
  148. }
  149. this.math.merge(list);
  150. }
  151. }
  152. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  153. finally {
  154. try {
  155. if (_k && !_k.done && (_b = _j.return)) _b.call(_j);
  156. }
  157. finally { if (e_3) throw e_3.error; }
  158. }
  159. }
  160. }
  161. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  162. finally {
  163. try {
  164. if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
  165. }
  166. finally { if (e_2) throw e_2.error; }
  167. }
  168. this.processed.set('findMath');
  169. }
  170. return this;
  171. };
  172. HTMLDocument.prototype.updateDocument = function () {
  173. if (!this.processed.isSet('updateDocument')) {
  174. this.addPageElements();
  175. this.addStyleSheet();
  176. _super.prototype.updateDocument.call(this);
  177. this.processed.set('updateDocument');
  178. }
  179. return this;
  180. };
  181. HTMLDocument.prototype.addPageElements = function () {
  182. var body = this.adaptor.body(this.document);
  183. var node = this.documentPageElements();
  184. if (node) {
  185. this.adaptor.append(body, node);
  186. }
  187. };
  188. HTMLDocument.prototype.addStyleSheet = function () {
  189. var sheet = this.documentStyleSheet();
  190. var adaptor = this.adaptor;
  191. if (sheet && !adaptor.parent(sheet)) {
  192. var head = adaptor.head(this.document);
  193. var styles = this.findSheet(head, adaptor.getAttribute(sheet, 'id'));
  194. if (styles) {
  195. adaptor.replace(sheet, styles);
  196. }
  197. else {
  198. adaptor.append(head, sheet);
  199. }
  200. }
  201. };
  202. HTMLDocument.prototype.findSheet = function (head, id) {
  203. var e_6, _a;
  204. if (id) {
  205. try {
  206. for (var _b = __values(this.adaptor.tags(head, 'style')), _c = _b.next(); !_c.done; _c = _b.next()) {
  207. var sheet = _c.value;
  208. if (this.adaptor.getAttribute(sheet, 'id') === id) {
  209. return sheet;
  210. }
  211. }
  212. }
  213. catch (e_6_1) { e_6 = { error: e_6_1 }; }
  214. finally {
  215. try {
  216. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  217. }
  218. finally { if (e_6) throw e_6.error; }
  219. }
  220. }
  221. return null;
  222. };
  223. HTMLDocument.prototype.removeFromDocument = function (restore) {
  224. var e_7, _a;
  225. if (restore === void 0) { restore = false; }
  226. if (this.processed.isSet('updateDocument')) {
  227. try {
  228. for (var _b = __values(this.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  229. var math = _c.value;
  230. if (math.state() >= MathItem_js_1.STATE.INSERTED) {
  231. math.state(MathItem_js_1.STATE.TYPESET, restore);
  232. }
  233. }
  234. }
  235. catch (e_7_1) { e_7 = { error: e_7_1 }; }
  236. finally {
  237. try {
  238. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  239. }
  240. finally { if (e_7) throw e_7.error; }
  241. }
  242. }
  243. this.processed.clear('updateDocument');
  244. return this;
  245. };
  246. HTMLDocument.prototype.documentStyleSheet = function () {
  247. return this.outputJax.styleSheet(this);
  248. };
  249. HTMLDocument.prototype.documentPageElements = function () {
  250. return this.outputJax.pageElements(this);
  251. };
  252. HTMLDocument.prototype.addStyles = function (styles) {
  253. this.styles.push(styles);
  254. };
  255. HTMLDocument.prototype.getStyles = function () {
  256. return this.styles;
  257. };
  258. HTMLDocument.KIND = 'HTML';
  259. HTMLDocument.OPTIONS = __assign(__assign({}, MathDocument_js_1.AbstractMathDocument.OPTIONS), { renderActions: (0, Options_js_1.expandable)(__assign(__assign({}, MathDocument_js_1.AbstractMathDocument.OPTIONS.renderActions), { styles: [MathItem_js_1.STATE.INSERTED + 1, '', 'updateStyleSheet', false] })), MathList: HTMLMathList_js_1.HTMLMathList, MathItem: HTMLMathItem_js_1.HTMLMathItem, DomStrings: null });
  260. return HTMLDocument;
  261. }(MathDocument_js_1.AbstractMathDocument));
  262. exports.HTMLDocument = HTMLDocument;
  263. //# sourceMappingURL=HTMLDocument.js.map