HTMLDomStrings.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.HTMLDomStrings = void 0;
  20. var Options_js_1 = require("../../util/Options.js");
  21. var HTMLDomStrings = (function () {
  22. function HTMLDomStrings(options) {
  23. if (options === void 0) { options = null; }
  24. var CLASS = this.constructor;
  25. this.options = (0, Options_js_1.userOptions)((0, Options_js_1.defaultOptions)({}, CLASS.OPTIONS), options);
  26. this.init();
  27. this.getPatterns();
  28. }
  29. HTMLDomStrings.prototype.init = function () {
  30. this.strings = [];
  31. this.string = '';
  32. this.snodes = [];
  33. this.nodes = [];
  34. this.stack = [];
  35. };
  36. HTMLDomStrings.prototype.getPatterns = function () {
  37. var skip = (0, Options_js_1.makeArray)(this.options['skipHtmlTags']);
  38. var ignore = (0, Options_js_1.makeArray)(this.options['ignoreHtmlClass']);
  39. var process = (0, Options_js_1.makeArray)(this.options['processHtmlClass']);
  40. this.skipHtmlTags = new RegExp('^(?:' + skip.join('|') + ')$', 'i');
  41. this.ignoreHtmlClass = new RegExp('(?:^| )(?:' + ignore.join('|') + ')(?: |$)');
  42. this.processHtmlClass = new RegExp('(?:^| )(?:' + process + ')(?: |$)');
  43. };
  44. HTMLDomStrings.prototype.pushString = function () {
  45. if (this.string.match(/\S/)) {
  46. this.strings.push(this.string);
  47. this.nodes.push(this.snodes);
  48. }
  49. this.string = '';
  50. this.snodes = [];
  51. };
  52. HTMLDomStrings.prototype.extendString = function (node, text) {
  53. this.snodes.push([node, text.length]);
  54. this.string += text;
  55. };
  56. HTMLDomStrings.prototype.handleText = function (node, ignore) {
  57. if (!ignore) {
  58. this.extendString(node, this.adaptor.value(node));
  59. }
  60. return this.adaptor.next(node);
  61. };
  62. HTMLDomStrings.prototype.handleTag = function (node, ignore) {
  63. if (!ignore) {
  64. var text = this.options['includeHtmlTags'][this.adaptor.kind(node)];
  65. this.extendString(node, text);
  66. }
  67. return this.adaptor.next(node);
  68. };
  69. HTMLDomStrings.prototype.handleContainer = function (node, ignore) {
  70. this.pushString();
  71. var cname = this.adaptor.getAttribute(node, 'class') || '';
  72. var tname = this.adaptor.kind(node) || '';
  73. var process = this.processHtmlClass.exec(cname);
  74. var next = node;
  75. if (this.adaptor.firstChild(node) && !this.adaptor.getAttribute(node, 'data-MJX') &&
  76. (process || !this.skipHtmlTags.exec(tname))) {
  77. if (this.adaptor.next(node)) {
  78. this.stack.push([this.adaptor.next(node), ignore]);
  79. }
  80. next = this.adaptor.firstChild(node);
  81. ignore = (ignore || this.ignoreHtmlClass.exec(cname)) && !process;
  82. }
  83. else {
  84. next = this.adaptor.next(node);
  85. }
  86. return [next, ignore];
  87. };
  88. HTMLDomStrings.prototype.handleOther = function (node, _ignore) {
  89. this.pushString();
  90. return this.adaptor.next(node);
  91. };
  92. HTMLDomStrings.prototype.find = function (node) {
  93. var _a, _b;
  94. this.init();
  95. var stop = this.adaptor.next(node);
  96. var ignore = false;
  97. var include = this.options['includeHtmlTags'];
  98. while (node && node !== stop) {
  99. var kind = this.adaptor.kind(node);
  100. if (kind === '#text') {
  101. node = this.handleText(node, ignore);
  102. }
  103. else if (include.hasOwnProperty(kind)) {
  104. node = this.handleTag(node, ignore);
  105. }
  106. else if (kind) {
  107. _a = __read(this.handleContainer(node, ignore), 2), node = _a[0], ignore = _a[1];
  108. }
  109. else {
  110. node = this.handleOther(node, ignore);
  111. }
  112. if (!node && this.stack.length) {
  113. this.pushString();
  114. _b = __read(this.stack.pop(), 2), node = _b[0], ignore = _b[1];
  115. }
  116. }
  117. this.pushString();
  118. var result = [this.strings, this.nodes];
  119. this.init();
  120. return result;
  121. };
  122. HTMLDomStrings.OPTIONS = {
  123. skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code', 'annotation', 'annotation-xml'],
  124. includeHtmlTags: { br: '\n', wbr: '', '#comment': '' },
  125. ignoreHtmlClass: 'mathjax_ignore',
  126. processHtmlClass: 'mathjax_process'
  127. };
  128. return HTMLDomStrings;
  129. }());
  130. exports.HTMLDomStrings = HTMLDomStrings;
  131. //# sourceMappingURL=HTMLDomStrings.js.map