temml 0.13.4 → 0.13.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/temml.mjs CHANGED
@@ -68,9 +68,17 @@ ParseError.prototype.__proto__ = Error.prototype;
68
68
  */
69
69
 
70
70
  /**
71
- * Provide a default value if a setting is undefined
71
+ * Look up `key` on a user-supplied `options` object and return its value, or
72
+ * `defaultIfUndefined` if the setting is absent. Reads via
73
+ * `Object.prototype.hasOwnProperty.call` rather than plain property access,
74
+ * so a key that only exists because `Object.prototype` was polluted
75
+ * elsewhere is never mistaken for a setting the caller actually provided.
72
76
  */
73
- const deflt = function(setting, defaultIfUndefined) {
77
+ const deflt = function(options, key, defaultIfUndefined) {
78
+ if (!Object.prototype.hasOwnProperty.call(options, key)) {
79
+ return defaultIfUndefined;
80
+ }
81
+ const setting = options[key];
74
82
  return setting === undefined ? defaultIfUndefined : setting;
75
83
  };
76
84
 
@@ -188,6 +196,27 @@ const smalls = "acegıȷmnopqrsuvwxyzαγεηικμνοπρςστυχωϕ𝐚
188
196
  */
189
197
 
190
198
 
199
+ const FORBIDDEN_MACRO_KEYS = ["__proto__", "prototype", "constructor"];
200
+
201
+ // Sanitize a user-supplied macros object: strip its prototype (so later
202
+ // bracket-notation writes, e.g. from \gdef, can never reach Object.prototype)
203
+ // and delete any own key that could be used to pollute it. This mutates
204
+ // `macros` in place and returns the same reference, rather than a copy,
205
+ // because callers such as auto-render.js rely on that identity to share
206
+ // \gdef'd macros across multiple render calls that pass the same options
207
+ // object.
208
+ const sanitizeMacros = (macros) => {
209
+ if (macros == null || typeof macros !== "object") {
210
+ return Object.create(null);
211
+ }
212
+ for (const key of FORBIDDEN_MACRO_KEYS) {
213
+ if (Object.prototype.hasOwnProperty.call(macros, key)) {
214
+ delete macros[key];
215
+ }
216
+ }
217
+ return Object.setPrototypeOf(macros, null);
218
+ };
219
+
191
220
  /**
192
221
  * The main Settings object
193
222
  */
@@ -195,24 +224,29 @@ class Settings {
195
224
  constructor(options) {
196
225
  // allow null options
197
226
  options = options || {};
198
- this.displayMode = deflt(options.displayMode, false); // boolean
199
- this.annotate = deflt(options.annotate, false); // boolean
200
- this.leqno = deflt(options.leqno, false); // boolean
201
- this.throwOnError = deflt(options.throwOnError, false); // boolean
202
- this.errorColor = deflt(options.errorColor, "#b22222"); // string
203
- this.macros = options.macros || {};
204
- this.wrap = deflt(options.wrap, "none"); // "none" | "tex" | "="
205
- this.xml = deflt(options.xml, false); // boolean
206
- this.colorIsTextColor = deflt(options.colorIsTextColor, false); // boolean
207
- this.strict = deflt(options.strict, false); // boolean
208
- this.trust = deflt(options.trust, false); // trust context. See html.js.
209
- this.maxSize = (options.maxSize === undefined
210
- ? [Infinity, Infinity]
211
- : Array.isArray(options.maxSize)
227
+ this.displayMode = deflt(options, "displayMode", false); // boolean
228
+ this.annotate = deflt(options, "annotate", false); // boolean
229
+ this.leqno = deflt(options, "leqno", false); // boolean
230
+ this.throwOnError = deflt(options, "throwOnError", false); // boolean
231
+ this.errorColor = deflt(options, "errorColor", "#b22222"); // string
232
+ this.macros = sanitizeMacros(
233
+ Object.prototype.hasOwnProperty.call(options, "macros") ? options.macros : undefined
234
+ );
235
+ this.wrap = deflt(options, "wrap", "none"); // "none" | "tex" | "="
236
+ this.xml = deflt(options, "xml", false); // boolean
237
+ this.colorIsTextColor = deflt(options, "colorIsTextColor", false); // boolean
238
+ this.strict = deflt(options, "strict", false); // boolean
239
+ this.trust = deflt(options, "trust", false); // trust context. See html.js.
240
+ const maxSize = Object.prototype.hasOwnProperty.call(options, "maxSize")
212
241
  ? options.maxSize
242
+ : undefined;
243
+ this.maxSize = (maxSize === undefined
244
+ ? [Infinity, Infinity]
245
+ : Array.isArray(maxSize)
246
+ ? maxSize
213
247
  : [Infinity, Infinity]
214
248
  );
215
- this.maxExpand = Math.max(0, deflt(options.maxExpand, 1000)); // number
249
+ this.maxExpand = Math.max(0, deflt(options, "maxExpand", 1000)); // number
216
250
  this.wrapDelimiterPairs = true; // boolean
217
251
  }
218
252
 
@@ -912,22 +946,26 @@ const NON_ATOMS = {
912
946
  textord: 1
913
947
  };
914
948
 
915
- const symbols = {
916
- math: {},
917
- text: {}
918
- };
949
+ const symbols = Object.create(null);
950
+ symbols.math = Object.create(null);
951
+ symbols.text = Object.create(null);
919
952
 
920
- /** `acceptUnicodeChar = true` is only applicable if `replace` is set. */
921
953
  function defineSymbol(mode, group, replace, name, acceptUnicodeChar) {
922
- symbols[mode][name] = { group, replace };
954
+ if (mode !== "math" && mode !== "text") {
955
+ throw new TypeError(`Invalid mode: ${mode} called in defineSymbol`);
956
+ }
957
+
958
+ const entry = { group, replace };
959
+ symbols[mode][name] = entry;
923
960
 
961
+ /** `acceptUnicodeChar = true` is only applicable if `replace` is set. */
924
962
  if (acceptUnicodeChar && replace) {
925
- symbols[mode][replace] = symbols[mode][name];
963
+ symbols[mode][replace] = entry;
926
964
  }
927
965
  }
928
966
 
929
967
  // Some abbreviations for commonly used strings.
930
- // This helps minify the code, and also spotting typos using jshint.
968
+ // This helps minify the code, and also spotting typos via linter.
931
969
 
932
970
  // modes:
933
971
  const math = "math";
@@ -3557,7 +3595,7 @@ const StyleLevel = {
3557
3595
  * `macros.js` exports this same dictionary again and makes it public.
3558
3596
  * `Parser.js` requires this dictionary via `macros.js`.
3559
3597
  */
3560
- const _macros = {};
3598
+ const _macros = Object.create(null);
3561
3599
 
3562
3600
  // This function might one day accept an additional argument and do more things.
3563
3601
  function defineMacro(name, body) {
@@ -11998,7 +12036,7 @@ class Namespace {
11998
12036
  * of initial (global-level) mappings, which will constantly change
11999
12037
  * according to any global/top-level `set`s done.
12000
12038
  */
12001
- constructor(builtins = {}, globalMacros = {}) {
12039
+ constructor(builtins = Object.create(null), globalMacros = Object.create(null)) {
12002
12040
  this.current = globalMacros;
12003
12041
  this.builtins = builtins;
12004
12042
  this.undefStack = [];
@@ -12008,7 +12046,7 @@ class Namespace {
12008
12046
  * Start a new nested group, affecting future local `set`s.
12009
12047
  */
12010
12048
  beginGroup() {
12011
- this.undefStack.push({});
12049
+ this.undefStack.push(Object.create(null));
12012
12050
  }
12013
12051
 
12014
12052
  /**
@@ -14206,7 +14244,7 @@ class Style {
14206
14244
  * https://mit-license.org/
14207
14245
  */
14208
14246
 
14209
- const version = "0.13.4";
14247
+ const version = "0.13.5";
14210
14248
 
14211
14249
  function postProcess(block) {
14212
14250
  const labelMap = {};
@@ -14527,7 +14565,7 @@ const renderMathInElement = function(elem, options) {
14527
14565
 
14528
14566
  // Enable sharing of global macros defined via `\gdef` between different
14529
14567
  // math elements within a single call to `renderMathInElement`.
14530
- optionsCopy.macros = optionsCopy.macros || {};
14568
+ optionsCopy.macros = optionsCopy.macros || Object.create(null);
14531
14569
 
14532
14570
  renderElem(elem, optionsCopy);
14533
14571
  postProcess(elem);
@@ -14606,7 +14644,7 @@ const generateParseTree = function(expression, options) {
14606
14644
  */
14607
14645
  const definePreamble = function(expression, options) {
14608
14646
  const settings = new Settings(options);
14609
- settings.macros = {};
14647
+ settings.macros = Object.create(null);
14610
14648
  if (!(typeof expression === "string" || expression instanceof String)) {
14611
14649
  throw new TypeError("Temml can only parse string typed expression")
14612
14650
  }
@@ -11,7 +11,7 @@
11
11
  * https://mit-license.org/
12
12
  */
13
13
 
14
- const version = "0.13.4";
14
+ const version = "0.13.5";
15
15
 
16
16
  function postProcess(block) {
17
17
  const labelMap = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "temml",
3
- "version": "0.13.4",
3
+ "version": "0.13.5",
4
4
  "description": "TeX to MathML conversion in JavaScript.",
5
5
  "main": "dist/temml.js",
6
6
  "engines": {
package/src/Namespace.js CHANGED
@@ -15,7 +15,7 @@ export default class Namespace {
15
15
  * of initial (global-level) mappings, which will constantly change
16
16
  * according to any global/top-level `set`s done.
17
17
  */
18
- constructor(builtins = {}, globalMacros = {}) {
18
+ constructor(builtins = Object.create(null), globalMacros = Object.create(null)) {
19
19
  this.current = globalMacros;
20
20
  this.builtins = builtins;
21
21
  this.undefStack = [];
@@ -25,7 +25,7 @@ export default class Namespace {
25
25
  * Start a new nested group, affecting future local `set`s.
26
26
  */
27
27
  beginGroup() {
28
- this.undefStack.push({});
28
+ this.undefStack.push(Object.create(null));
29
29
  }
30
30
 
31
31
  /**
package/src/Settings.js CHANGED
@@ -5,6 +5,27 @@
5
5
 
6
6
  import * as utils from "./utils";
7
7
 
8
+ const FORBIDDEN_MACRO_KEYS = ["__proto__", "prototype", "constructor"];
9
+
10
+ // Sanitize a user-supplied macros object: strip its prototype (so later
11
+ // bracket-notation writes, e.g. from \gdef, can never reach Object.prototype)
12
+ // and delete any own key that could be used to pollute it. This mutates
13
+ // `macros` in place and returns the same reference, rather than a copy,
14
+ // because callers such as auto-render.js rely on that identity to share
15
+ // \gdef'd macros across multiple render calls that pass the same options
16
+ // object.
17
+ const sanitizeMacros = (macros) => {
18
+ if (macros == null || typeof macros !== "object") {
19
+ return Object.create(null);
20
+ }
21
+ for (const key of FORBIDDEN_MACRO_KEYS) {
22
+ if (Object.prototype.hasOwnProperty.call(macros, key)) {
23
+ delete macros[key];
24
+ }
25
+ }
26
+ return Object.setPrototypeOf(macros, null);
27
+ };
28
+
8
29
  /**
9
30
  * The main Settings object
10
31
  */
@@ -12,24 +33,29 @@ export default class Settings {
12
33
  constructor(options) {
13
34
  // allow null options
14
35
  options = options || {};
15
- this.displayMode = utils.deflt(options.displayMode, false); // boolean
16
- this.annotate = utils.deflt(options.annotate, false) // boolean
17
- this.leqno = utils.deflt(options.leqno, false); // boolean
18
- this.throwOnError = utils.deflt(options.throwOnError, false); // boolean
19
- this.errorColor = utils.deflt(options.errorColor, "#b22222"); // string
20
- this.macros = options.macros || {};
21
- this.wrap = utils.deflt(options.wrap, "none") // "none" | "tex" | "="
22
- this.xml = utils.deflt(options.xml, false); // boolean
23
- this.colorIsTextColor = utils.deflt(options.colorIsTextColor, false); // boolean
24
- this.strict = utils.deflt(options.strict, false); // boolean
25
- this.trust = utils.deflt(options.trust, false); // trust context. See html.js.
26
- this.maxSize = (options.maxSize === undefined
27
- ? [Infinity, Infinity]
28
- : Array.isArray(options.maxSize)
36
+ this.displayMode = utils.deflt(options, "displayMode", false); // boolean
37
+ this.annotate = utils.deflt(options, "annotate", false) // boolean
38
+ this.leqno = utils.deflt(options, "leqno", false); // boolean
39
+ this.throwOnError = utils.deflt(options, "throwOnError", false); // boolean
40
+ this.errorColor = utils.deflt(options, "errorColor", "#b22222"); // string
41
+ this.macros = sanitizeMacros(
42
+ Object.prototype.hasOwnProperty.call(options, "macros") ? options.macros : undefined
43
+ );
44
+ this.wrap = utils.deflt(options, "wrap", "none") // "none" | "tex" | "="
45
+ this.xml = utils.deflt(options, "xml", false); // boolean
46
+ this.colorIsTextColor = utils.deflt(options, "colorIsTextColor", false); // boolean
47
+ this.strict = utils.deflt(options, "strict", false); // boolean
48
+ this.trust = utils.deflt(options, "trust", false); // trust context. See html.js.
49
+ const maxSize = Object.prototype.hasOwnProperty.call(options, "maxSize")
29
50
  ? options.maxSize
51
+ : undefined;
52
+ this.maxSize = (maxSize === undefined
53
+ ? [Infinity, Infinity]
54
+ : Array.isArray(maxSize)
55
+ ? maxSize
30
56
  : [Infinity, Infinity]
31
57
  )
32
- this.maxExpand = Math.max(0, utils.deflt(options.maxExpand, 1000)); // number
58
+ this.maxExpand = Math.max(0, utils.deflt(options, "maxExpand", 1000)); // number
33
59
  this.wrapDelimiterPairs = true; // boolean
34
60
  }
35
61
 
@@ -256,7 +256,7 @@ export const renderMathInElement = function(elem, options) {
256
256
 
257
257
  // Enable sharing of global macros defined via `\gdef` between different
258
258
  // math elements within a single call to `renderMathInElement`.
259
- optionsCopy.macros = optionsCopy.macros || {};
259
+ optionsCopy.macros = optionsCopy.macros || Object.create(null);
260
260
 
261
261
  renderElem(elem, optionsCopy);
262
262
  postProcess(elem);
@@ -3,7 +3,7 @@
3
3
  * `macros.js` exports this same dictionary again and makes it public.
4
4
  * `Parser.js` requires this dictionary via `macros.js`.
5
5
  */
6
- export const _macros = {};
6
+ export const _macros = Object.create(null);
7
7
 
8
8
  // This function might one day accept an additional argument and do more things.
9
9
  export default function defineMacro(name, body) {
@@ -5,7 +5,7 @@
5
5
  * https://mit-license.org/
6
6
  */
7
7
 
8
- export const version = "0.13.4";
8
+ export const version = "0.13.5";
9
9
 
10
10
  export function postProcess(block) {
11
11
  const labelMap = {}
package/src/symbols.js CHANGED
@@ -33,23 +33,27 @@ export const NON_ATOMS = {
33
33
  textord: 1
34
34
  };
35
35
 
36
- const symbols = {
37
- math: {},
38
- text: {}
39
- };
36
+ const symbols = Object.create(null);
37
+ symbols.math = Object.create(null);
38
+ symbols.text = Object.create(null);
40
39
  export default symbols;
41
40
 
42
- /** `acceptUnicodeChar = true` is only applicable if `replace` is set. */
43
41
  export function defineSymbol(mode, group, replace, name, acceptUnicodeChar) {
44
- symbols[mode][name] = { group, replace };
42
+ if (mode !== "math" && mode !== "text") {
43
+ throw new TypeError(`Invalid mode: ${mode} called in defineSymbol`);
44
+ }
45
+
46
+ const entry = { group, replace };
47
+ symbols[mode][name] = entry;
45
48
 
49
+ /** `acceptUnicodeChar = true` is only applicable if `replace` is set. */
46
50
  if (acceptUnicodeChar && replace) {
47
- symbols[mode][replace] = symbols[mode][name];
51
+ symbols[mode][replace] = entry;
48
52
  }
49
53
  }
50
54
 
51
55
  // Some abbreviations for commonly used strings.
52
- // This helps minify the code, and also spotting typos using jshint.
56
+ // This helps minify the code, and also spotting typos via linter.
53
57
 
54
58
  // modes:
55
59
  const math = "math";
package/src/utils.js CHANGED
@@ -5,9 +5,17 @@
5
5
  */
6
6
 
7
7
  /**
8
- * Provide a default value if a setting is undefined
8
+ * Look up `key` on a user-supplied `options` object and return its value, or
9
+ * `defaultIfUndefined` if the setting is absent. Reads via
10
+ * `Object.prototype.hasOwnProperty.call` rather than plain property access,
11
+ * so a key that only exists because `Object.prototype` was polluted
12
+ * elsewhere is never mistaken for a setting the caller actually provided.
9
13
  */
10
- export const deflt = function(setting, defaultIfUndefined) {
14
+ export const deflt = function(options, key, defaultIfUndefined) {
15
+ if (!Object.prototype.hasOwnProperty.call(options, key)) {
16
+ return defaultIfUndefined;
17
+ }
18
+ const setting = options[key];
11
19
  return setting === undefined ? defaultIfUndefined : setting;
12
20
  };
13
21
 
package/temml.js CHANGED
@@ -84,7 +84,7 @@ const generateParseTree = function(expression, options) {
84
84
  */
85
85
  const definePreamble = function(expression, options) {
86
86
  const settings = new Settings(options);
87
- settings.macros = {};
87
+ settings.macros = Object.create(null);
88
88
  if (!(typeof expression === "string" || expression instanceof String)) {
89
89
  throw new TypeError("Temml can only parse string typed expression")
90
90
  }