safe.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 __values = (this && this.__values) || function(o) {
  14. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  15. if (m) return m.call(o);
  16. if (o && typeof o.length === "number") return {
  17. next: function () {
  18. if (o && i >= o.length) o = void 0;
  19. return { value: o && o[i++], done: !o };
  20. }
  21. };
  22. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. exports.Safe = void 0;
  26. var Options_js_1 = require("../../util/Options.js");
  27. var SafeMethods_js_1 = require("./SafeMethods.js");
  28. var Safe = (function () {
  29. function Safe(document, options) {
  30. this.filterAttributes = new Map([
  31. ['href', 'filterURL'],
  32. ['src', 'filterURL'],
  33. ['altimg', 'filterURL'],
  34. ['class', 'filterClassList'],
  35. ['style', 'filterStyles'],
  36. ['id', 'filterID'],
  37. ['fontsize', 'filterFontSize'],
  38. ['mathsize', 'filterFontSize'],
  39. ['scriptminsize', 'filterFontSize'],
  40. ['scriptsizemultiplier', 'filterSizeMultiplier'],
  41. ['scriptlevel', 'filterScriptLevel'],
  42. ['data-', 'filterData']
  43. ]);
  44. this.filterMethods = __assign({}, SafeMethods_js_1.SafeMethods);
  45. this.adaptor = document.adaptor;
  46. this.options = options;
  47. this.allow = this.options.allow;
  48. }
  49. Safe.prototype.sanitize = function (math, document) {
  50. try {
  51. math.root.walkTree(this.sanitizeNode.bind(this));
  52. }
  53. catch (err) {
  54. document.options.compileError(document, math, err);
  55. }
  56. };
  57. Safe.prototype.sanitizeNode = function (node) {
  58. var e_1, _a;
  59. var attributes = node.attributes.getAllAttributes();
  60. try {
  61. for (var _b = __values(Object.keys(attributes)), _c = _b.next(); !_c.done; _c = _b.next()) {
  62. var id = _c.value;
  63. var method = this.filterAttributes.get(id);
  64. if (method) {
  65. var value = this.filterMethods[method](this, attributes[id]);
  66. if (value) {
  67. if (value !== (typeof value === 'number' ? parseFloat(attributes[id]) : attributes[id])) {
  68. attributes[id] = value;
  69. }
  70. }
  71. else {
  72. delete attributes[id];
  73. }
  74. }
  75. }
  76. }
  77. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  78. finally {
  79. try {
  80. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  81. }
  82. finally { if (e_1) throw e_1.error; }
  83. }
  84. };
  85. Safe.prototype.mmlAttribute = function (id, value) {
  86. if (id === 'class')
  87. return null;
  88. var method = this.filterAttributes.get(id);
  89. var filter = (method || (id.substr(0, 5) === 'data-' ? this.filterAttributes.get('data-') : null));
  90. if (!filter) {
  91. return value;
  92. }
  93. var result = this.filterMethods[filter](this, value, id);
  94. return (typeof result === 'number' || typeof result === 'boolean' ? String(result) : result);
  95. };
  96. Safe.prototype.mmlClassList = function (list) {
  97. var _this = this;
  98. return list.map(function (name) { return _this.filterMethods.filterClass(_this, name); })
  99. .filter(function (value) { return value !== null; });
  100. };
  101. Safe.OPTIONS = {
  102. allow: {
  103. URLs: 'safe',
  104. classes: 'safe',
  105. cssIDs: 'safe',
  106. styles: 'safe'
  107. },
  108. lengthMax: 3,
  109. scriptsizemultiplierRange: [.6, 1],
  110. scriptlevelRange: [-2, 2],
  111. classPattern: /^mjx-[-a-zA-Z0-9_.]+$/,
  112. idPattern: /^mjx-[-a-zA-Z0-9_.]+$/,
  113. dataPattern: /^data-mjx-/,
  114. safeProtocols: (0, Options_js_1.expandable)({
  115. http: true,
  116. https: true,
  117. file: true,
  118. javascript: false,
  119. data: false
  120. }),
  121. safeStyles: (0, Options_js_1.expandable)({
  122. color: true,
  123. backgroundColor: true,
  124. border: true,
  125. cursor: true,
  126. margin: true,
  127. padding: true,
  128. textShadow: true,
  129. fontFamily: true,
  130. fontSize: true,
  131. fontStyle: true,
  132. fontWeight: true,
  133. opacity: true,
  134. outline: true
  135. }),
  136. styleParts: (0, Options_js_1.expandable)({
  137. border: true,
  138. padding: true,
  139. margin: true,
  140. outline: true
  141. }),
  142. styleLengths: (0, Options_js_1.expandable)({
  143. borderTop: 'borderTopWidth',
  144. borderRight: 'borderRightWidth',
  145. borderBottom: 'borderBottomWidth',
  146. borderLeft: 'borderLeftWidth',
  147. paddingTop: true,
  148. paddingRight: true,
  149. paddingBottom: true,
  150. paddingLeft: true,
  151. marginTop: true,
  152. marginRight: true,
  153. marginBottom: true,
  154. marginLeft: true,
  155. outlineTop: true,
  156. outlineRight: true,
  157. outlineBottom: true,
  158. outlineLeft: true,
  159. fontSize: [.707, 1.44]
  160. })
  161. };
  162. return Safe;
  163. }());
  164. exports.Safe = Safe;
  165. //# sourceMappingURL=safe.js.map