katex 0.18.3 → 0.18.4

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/katex.mjs CHANGED
@@ -6818,6 +6818,24 @@ function checkSymbolNodeType(node) {
6818
6818
  }
6819
6819
  return null;
6820
6820
  }
6821
+ /**
6822
+ * Returns the string spelled out by a group of plain characters, throwing the
6823
+ * given ParseError if the group holds anything else. With allowSpaces, a
6824
+ * literal space counts as a character; `~` and `\ ` do not.
6825
+ */
6826
+ function assertCharacterGroup(group, errorMessage, allowSpaces) {
6827
+ var text = "";
6828
+ for (var node of group.body) {
6829
+ if (node.type === "textord") {
6830
+ text += node.text;
6831
+ } else if (allowSpaces && node.type === "spacing" && node.text === " ") {
6832
+ text += " ";
6833
+ } else {
6834
+ throw new ParseError(errorMessage, group);
6835
+ }
6836
+ }
6837
+ return text;
6838
+ }
6821
6839
 
6822
6840
  var getBaseSymbol = group => {
6823
6841
  if (group instanceof SymbolNode) {
@@ -7658,12 +7676,7 @@ defineFunction({
7658
7676
  handler(_ref, args) {
7659
7677
  var parser = _ref.parser;
7660
7678
  var arg = assertNodeType(args[0], "ordgroup");
7661
- var group = arg.body;
7662
- var number = "";
7663
- for (var i = 0; i < group.length; i++) {
7664
- var node = assertNodeType(group[i], "textord");
7665
- number += node.text;
7666
- }
7679
+ var number = assertCharacterGroup(arg, "\\@char has non-numeric argument");
7667
7680
  var code = parseInt(number);
7668
7681
  var text;
7669
7682
  if (isNaN(code)) {
@@ -9948,19 +9961,19 @@ var alignedHandler = function alignedHandler(context, args) {
9948
9961
  body: []
9949
9962
  };
9950
9963
  if (args[0] && args[0].type === "ordgroup") {
9951
- var arg0 = "";
9952
- for (var i = 0; i < args[0].body.length; i++) {
9953
- var textord = assertNodeType(args[0].body[i], "textord");
9954
- arg0 += textord.text;
9964
+ var message = "Number of columns should be a positive integer";
9965
+ var numColumns = assertCharacterGroup(args[0], message);
9966
+ if (!/^[0-9]+$/.test(numColumns) || Number(numColumns) < 1) {
9967
+ throw new ParseError(message, args[0]);
9955
9968
  }
9956
- numMaths = Number(arg0);
9969
+ numMaths = Number(numColumns);
9957
9970
  numCols = numMaths * 2;
9958
9971
  }
9959
9972
  var isAligned = !numCols;
9960
9973
  res.body.forEach(function (row) {
9961
- for (var _i4 = 1; _i4 < row.length; _i4 += 2) {
9974
+ for (var i = 1; i < row.length; i += 2) {
9962
9975
  // Modify ordgroup node within styling node
9963
- var styling = assertNodeType(row[_i4], "styling");
9976
+ var styling = assertNodeType(row[i], "styling");
9964
9977
  var ordgroup = assertNodeType(styling.body[0], "ordgroup");
9965
9978
  ordgroup.body.unshift(emptyGroup);
9966
9979
  }
@@ -9978,16 +9991,16 @@ var alignedHandler = function alignedHandler(context, args) {
9978
9991
  // Adjusting alignment.
9979
9992
  // In aligned mode, we add one \qquad between columns;
9980
9993
  // otherwise we add nothing.
9981
- for (var _i5 = 0; _i5 < numCols; ++_i5) {
9994
+ for (var i = 0; i < numCols; ++i) {
9982
9995
  var align = "r";
9983
9996
  var pregap = 0;
9984
- if (_i5 % 2 === 1) {
9997
+ if (i % 2 === 1) {
9985
9998
  align = "l";
9986
- } else if (_i5 > 0 && isAligned) {
9999
+ } else if (i > 0 && isAligned) {
9987
10000
  // "aligned" mode.
9988
10001
  pregap = 1; // add one \quad
9989
10002
  }
9990
- cols[_i5] = {
10003
+ cols[i] = {
9991
10004
  type: "align",
9992
10005
  align: align,
9993
10006
  pregap: pregap,
@@ -10337,10 +10350,7 @@ defineFunction({
10337
10350
  if (nameGroup.type !== "ordgroup") {
10338
10351
  throw new ParseError("Invalid environment name", nameGroup);
10339
10352
  }
10340
- var envName = "";
10341
- for (var i = 0; i < nameGroup.body.length; ++i) {
10342
- envName += assertNodeType(nameGroup.body[i], "textord").text;
10343
- }
10353
+ var envName = assertCharacterGroup(nameGroup, "Environment name should contain only text characters and spaces", true);
10344
10354
  if (funcName === "\\begin") {
10345
10355
  // begin...end is similar to left...right
10346
10356
  if (!Object.prototype.hasOwnProperty.call(environments, envName)) {
@@ -16265,7 +16275,7 @@ var renderToHTMLTree = function renderToHTMLTree(expression, options) {
16265
16275
  return renderError(error, expression, settings);
16266
16276
  }
16267
16277
  };
16268
- var version = "0.18.3";
16278
+ var version = "0.18.4";
16269
16279
  var __domTree = {
16270
16280
  Span,
16271
16281
  Anchor,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "katex",
3
- "version": "0.18.3",
3
+ "version": "0.18.4",
4
4
  "description": "Fast math typesetting for the web.",
5
5
  "main": "dist/katex.js",
6
6
  "types": "types/katex.d.ts",
@@ -6,8 +6,12 @@ import defineFunction from "../defineFunction";
6
6
  import defineMacro from "../defineMacro";
7
7
  import {MathNode} from "../mathMLTree";
8
8
  import ParseError from "../ParseError";
9
- import {assertNodeType, assertSymbolNodeType} from "../parseNode";
10
- import {checkSymbolNodeType} from "../parseNode";
9
+ import {
10
+ assertCharacterGroup,
11
+ assertNodeType,
12
+ assertSymbolNodeType,
13
+ checkSymbolNodeType,
14
+ } from "../parseNode";
11
15
  import {Token} from "../Token";
12
16
  import {calculateSize, makeEm} from "../units";
13
17
 
@@ -734,12 +738,12 @@ const alignedHandler = function(context: EnvContextLike, args: AnyParseNode[]) {
734
738
  body: [],
735
739
  };
736
740
  if (args[0] && args[0].type === "ordgroup") {
737
- let arg0 = "";
738
- for (let i = 0; i < args[0].body.length; i++) {
739
- const textord = assertNodeType(args[0].body[i], "textord");
740
- arg0 += textord.text;
741
+ const message = "Number of columns should be a positive integer";
742
+ const numColumns = assertCharacterGroup(args[0], message);
743
+ if (!/^[0-9]+$/.test(numColumns) || Number(numColumns) < 1) {
744
+ throw new ParseError(message, args[0]);
741
745
  }
742
- numMaths = Number(arg0);
746
+ numMaths = Number(numColumns);
743
747
  numCols = numMaths * 2;
744
748
  }
745
749
  const isAligned = !numCols;
@@ -1,6 +1,6 @@
1
1
  import defineFunction from "../defineFunction";
2
2
  import ParseError from "../ParseError";
3
- import {assertNodeType} from "../parseNode";
3
+ import {assertCharacterGroup, assertNodeType} from "../parseNode";
4
4
 
5
5
  // \@char is an internal function that takes a grouped decimal argument like
6
6
  // {123} and converts into symbol with code 123. It is used by the *macro*
@@ -13,12 +13,8 @@ defineFunction({
13
13
 
14
14
  handler({parser}, args) {
15
15
  const arg = assertNodeType(args[0], "ordgroup");
16
- const group = arg.body;
17
- let number = "";
18
- for (let i = 0; i < group.length; i++) {
19
- const node = assertNodeType(group[i], "textord");
20
- number += node.text;
21
- }
16
+ const number = assertCharacterGroup(
17
+ arg, "\\@char has non-numeric argument");
22
18
  let code = parseInt(number);
23
19
  let text;
24
20
  if (isNaN(code)) {
@@ -1,6 +1,6 @@
1
1
  import defineFunction from "../defineFunction";
2
2
  import ParseError from "../ParseError";
3
- import {assertNodeType} from "../parseNode";
3
+ import {assertCharacterGroup, assertNodeType} from "../parseNode";
4
4
  import environments from "../environments";
5
5
 
6
6
  import type {ParseNode} from "../types/nodes";
@@ -18,10 +18,10 @@ defineFunction({
18
18
  if (nameGroup.type !== "ordgroup") {
19
19
  throw new ParseError("Invalid environment name", nameGroup);
20
20
  }
21
- let envName = "";
22
- for (let i = 0; i < nameGroup.body.length; ++i) {
23
- envName += assertNodeType(nameGroup.body[i], "textord").text;
24
- }
21
+ const envName = assertCharacterGroup(
22
+ nameGroup,
23
+ "Environment name should contain only text characters and spaces",
24
+ true);
25
25
 
26
26
  if (funcName === "\\begin") {
27
27
  // begin...end is similar to left...right
package/src/parseNode.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {NonAtoms} from "./atoms";
2
+ import ParseError from "./ParseError";
2
3
  import type {AnyParseNode, NodeType, ParseNode, SymbolParseNode} from "./types/nodes";
3
4
 
4
5
  /**
@@ -42,3 +43,26 @@ export function checkSymbolNodeType(node: AnyParseNode): SymbolParseNode | null
42
43
  }
43
44
  return null;
44
45
  }
46
+
47
+ /**
48
+ * Returns the string spelled out by a group of plain characters, throwing the
49
+ * given ParseError if the group holds anything else. With allowSpaces, a
50
+ * literal space counts as a character; `~` and `\ ` do not.
51
+ */
52
+ export function assertCharacterGroup(
53
+ group: ParseNode<"ordgroup">,
54
+ errorMessage: string,
55
+ allowSpaces?: boolean,
56
+ ): string {
57
+ let text = "";
58
+ for (const node of group.body) {
59
+ if (node.type === "textord") {
60
+ text += node.text;
61
+ } else if (allowSpaces && node.type === "spacing" && node.text === " ") {
62
+ text += " ";
63
+ } else {
64
+ throw new ParseError(errorMessage, group);
65
+ }
66
+ }
67
+ return text;
68
+ }