rapier-math 2.4.1 → 2.7.0

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.
@@ -110,6 +110,7 @@ function mathML(tree, source, safeSource, options = {}) {
110
110
  const classOf = node => {
111
111
  if (!node) return undefined;
112
112
  if (node.class) return node.class;
113
+ if (node.type === 'row' && node.group) return 'ord';
113
114
  if (node.type === 'scripts' || node.type === 'accent') return classOf(node.base);
114
115
  if (node.type === 'style' || node.type === 'link') return classOf(node.body);
115
116
  if (node.type === 'row' && node.children.length === 1) return classOf(node.children[0]);
@@ -118,6 +119,7 @@ function mathML(tree, source, safeSource, options = {}) {
118
119
  const limitsOf = node => {
119
120
  if (!node) return undefined;
120
121
  if (node.limits) return node.limits;
122
+ if (node.type === 'row' && node.group) return undefined;
121
123
  if (node.type === 'style' || node.type === 'link') return limitsOf(node.body);
122
124
  if (node.type === 'row' && node.children.length === 1) return limitsOf(node.children[0]);
123
125
  if (node.type === 'accent') return limitsOf(node.base);
@@ -148,6 +150,16 @@ function mathML(tree, source, safeSource, options = {}) {
148
150
  };
149
151
 
150
152
  function scripts(node, context, depth, base) {
153
+ if (node.pre) {
154
+ open('mmultiscripts');
155
+ if (base) base(); else emit(node.base, {...context, form: undefined, movableLimits: false}, depth + 1);
156
+ open('mprescripts'); close('mprescripts');
157
+ for (const part of [node.sub, node.sup]) {
158
+ if (part) emit(part, scriptContext(context), depth + 1);
159
+ else { open('mrow'); close('mrow'); }
160
+ }
161
+ close('mmultiscripts'); return;
162
+ }
151
163
  const limits = node.limits || limitsOf(node.base), stacked = limits === 'always' || limits === 'display' && context.display && context.level === 0;
152
164
  const name = stacked ? node.sub && node.sup ? 'munderover' : node.sub ? 'munder' : 'mover' : node.sub && node.sup ? 'msubsup' : node.sub ? 'msub' : 'msup';
153
165
  open(name, stacked ? {accent: node.sup ? 'false' : undefined, accentunder: node.sub ? 'false' : undefined} : {});
@@ -158,12 +170,42 @@ function mathML(tree, source, safeSource, options = {}) {
158
170
  }
159
171
 
160
172
  function row(children, context, depth) {
173
+ if (children.some(node => node.type === 'choice')) {
174
+ const selected = [];
175
+ let selectionContext = context;
176
+ const append = (nodes, level) => {
177
+ for (const node of nodes) {
178
+ if (node.type === 'style' && node.mathStyle && node.declaration && node.body.type === 'row' && !node.body.children.length)
179
+ selectionContext = styledContext(selectionContext, node);
180
+ if (node.type !== 'choice') { selected.push(node); continue; }
181
+ visit(node, level); family(node);
182
+ if (active.has(node)) fail('unsupported-semantic-node', 'A mathematical tree contains a cycle.');
183
+ active.add(node);
184
+ const branch = mathChoice(node, selectionContext);
185
+ visit(branch, level + 1);
186
+ append(branch.children, level + 2);
187
+ active.delete(node);
188
+ }
189
+ };
190
+ append(children, depth + 1); children = selected;
191
+ if (children.length !== 1) context = {...context, ordinary: undefined};
192
+ }
161
193
  // movablelimits belongs to an operator that is itself the base, not to operators inside a compound base.
162
194
  if (children.length > 1) context = {...context, movableLimits: undefined};
163
195
  open('mrow');
164
- let previous;
196
+ let previous, styled = false;
165
197
  for (let index = 0; index < children.length; index++) {
166
198
  const node = children[index];
199
+ if (node.type === 'style' && node.mathStyle && node.declaration && node.body.type === 'row' && !node.body.children.length) {
200
+ visit(node, depth + 1); visit(node.body, depth + 2); context = styledContext(context, node);
201
+ if (styled) close('mstyle');
202
+ open('mstyle', {displaystyle: String(context.display), scriptlevel: String(context.level)}); styled = true; continue;
203
+ }
204
+ // The parser's prescript uses a phantom nucleus followed by the real nucleus for SVG placement.
205
+ if (node.type === 'scripts' && node.pre && node.base?.type === 'phantom' && node.base.body === children[index + 1]) {
206
+ emit({...node, base: children[++index]}, context, depth + 1);
207
+ previous = classOf(node.base.body); continue;
208
+ }
167
209
  if (numberPart(node)) {
168
210
  let end = index + 1;
169
211
  while (end < children.length && numberPart(children[end]) && sameNumberStyle(node, children[end])) end++;
@@ -188,12 +230,13 @@ function mathML(tree, source, safeSource, options = {}) {
188
230
  let form;
189
231
  if ((node.type === 'glyph' || node.type === 'style' && node.body.type === 'glyph') && classOf(node) === 'bin') {
190
232
  // TeX's rule 5: a binary operator before a relation, a closing symbol or nothing is not infix.
191
- const next = children.slice(index + 1).find(child => child.type !== 'space');
233
+ const next = children.slice(index + 1).find(child => child.type !== 'space' && !(child.type === 'style' && child.mathStyle && child.declaration && child.body.type === 'row' && !child.body.children.length));
192
234
  form = !previous || ['bin', 'op', 'rel', 'open', 'punct'].includes(previous) ? 'prefix' : !next || ['rel', 'close', 'punct'].includes(classOf(next)) ? 'postfix' : 'infix';
193
235
  }
194
236
  emit(node, {...context, form}, depth + 1);
195
237
  if (node.type !== 'space') previous = classOf(node);
196
238
  }
239
+ if (styled) close('mstyle');
197
240
  close('mrow');
198
241
  }
199
242
 
@@ -220,22 +263,26 @@ function mathML(tree, source, safeSource, options = {}) {
220
263
  const small = /smallmatrix$/.test(node.env) || node.env === 'subarray' || node.env === 'substack';
221
264
  const display = /^(align(?:ed)?(?:at)?|flalign|split|[lr]?gather(?:ed)?|eqnarray|multline(?:d)?|equation|drcases|dcases|darray)$/.test(node.env);
222
265
  const cellContext = {...context, display: small ? false : display, level: small ? Math.max(1, context.level) : context.level};
223
- const delimiters = {pmatrix: ['(', ')'], bmatrix: ['[', ']'], Bmatrix: ['{', '}'], vmatrix: ['|', '|'], Vmatrix: ['‖', '‖'],
224
- psmallmatrix: ['(', ')'], bsmallmatrix: ['[', ']'], Bsmallmatrix: ['{', '}'], vsmallmatrix: ['|', '|'], Vsmallmatrix: ['‖', '‖'],
225
- cases: ['{', ''], dcases: ['{', ''], rcases: ['', '}'], drcases: ['', '}']}[node.env];
266
+ const delimiters = mathTableDelimiters(node.env);
226
267
  if (delimiters) { open('mrow'); fence(delimiters[0], 'prefix', context); }
227
268
  open('mtable', {displaystyle: String(cellContext.display), scriptlevel: String(cellContext.level), 'data-math-environment': node.env});
228
269
  for (let at = 0; at < rows.length; at++) {
229
270
  open('mtr');
230
- for (let column = 0; column < columns; column++) {
271
+ // \intertext spans the whole row at its left; \multicolumn and \hdotsfor span their columns.
272
+ const intertext = rows[at][0]?.intertext;
273
+ for (let column = 0; column < (intertext ? 1 : columns); column++) {
231
274
  if (++metrics.nodes > 100000) fail('resource-limit', 'MathML table cells exceed its work budget.');
232
- let align = node.env === 'eqnarray' ? ['r', 'c', 'l'][column % 3] : node.align?.[column] || 'c';
275
+ const cell = rows[at][column];
276
+ if (cell?.covered) continue;
277
+ let align = intertext ? 'l' : cell?.spanAlign || (node.env === 'eqnarray' ? ['r', 'c', 'l'][column % 3] : node.align?.[column] || 'c');
233
278
  if (/^multlined?$/.test(node.env) && columns === 1) align = at === 0 ? 'l' : at === rows.length - 1 ? 'r' : 'c';
234
- open('mtd', {style: 'text-align:' + ({l: 'left', r: 'right', c: 'center'}[align] || 'center')});
279
+ const span = intertext ? columns : cell?.span > 1 ? Math.min(cell.span, columns - column) : 0;
280
+ open('mtd', {...(span > 1 ? {columnspan: String(span)} : {}), style: 'text-align:' + ({l: 'left', r: 'right', c: 'center'}[align] || 'center')});
235
281
  open('mrow');
236
282
  for (const body of before[column]) emit(body, cellContext, depth + 1);
237
- if (rows[at][column]) emit(rows[at][column], cellContext, depth + 1);
238
- if (column === columns - 1) for (const body of before[columns]) emit(body, cellContext, depth + 1);
283
+ if (cell?.dots) token('mo', '…', {stretchy: 'false'}, cellContext);
284
+ else if (cell) emit(cell, cellContext, depth + 1);
285
+ if (column + Math.max(1, span) >= columns) for (const body of before[columns]) emit(body, cellContext, depth + 1);
239
286
  close('mrow'); close('mtd');
240
287
  }
241
288
  close('mtr');
@@ -252,11 +299,13 @@ function mathML(tree, source, safeSource, options = {}) {
252
299
  active.add(node);
253
300
  try {
254
301
  switch (node.type) {
255
- case 'row': row(node.children, context, depth); break;
302
+ // A braced group is an ordinary atom in TeX; an operator alone in it takes no spacing of its own.
303
+ case 'row': row(node.children, node.group && node.children.length === 1 ? {...context, ordinary: true} : {...context, ordinary: undefined}, depth); break;
304
+ case 'choice': emit(mathChoice(node, context), context, depth + 1); break;
256
305
  case 'glyph': {
257
306
  const name = kind(node), attributes = {};
258
307
  if (name === 'mo') {
259
- if (node.class === 'open' || node.class === 'close') { attributes.fence = 'true'; attributes.form = node.class === 'open' ? 'prefix' : 'postfix'; }
308
+ if (node.class === 'open' || node.class === 'close') { attributes.fence = String(!/^[!?]$/.test(node.value)); attributes.form = node.class === 'open' ? 'prefix' : 'postfix'; }
260
309
  else if (node.class === 'punct') attributes.separator = 'true';
261
310
  else if (context.form) attributes.form = context.form;
262
311
  if (node.large !== undefined) attributes.largeop = String(!!node.large);
@@ -264,12 +313,18 @@ function mathML(tree, source, safeSource, options = {}) {
264
313
  if (node.delimiter || node.delimiterSize || node.stretch) attributes.stretchy = 'true';
265
314
  else attributes.stretchy = 'false';
266
315
  if (node.delimiterSize) attributes.minsize = attributes.maxsize = em(node.delimiterSize);
316
+ else if (node.minWidth) attributes.minsize = em(node.minWidth);
317
+ if (node.class === 'ord' && node.tokenKind === 'mo') attributes.lspace = attributes.rspace = '0em';
318
+ if (context.ordinary) { attributes.lspace = attributes.rspace = '0em'; if (node.limits) attributes.movablelimits = 'false'; }
267
319
  }
268
320
  atom(node, node.value, name, context, attributes); break;
269
321
  }
270
322
  case 'text': atom(node, node.value, ['op', 'bin', 'rel'].includes(node.class) ? 'mo' : 'mtext', context,
271
323
  node.class === 'op' ? {form: 'prefix', movablelimits: String(node.limits === 'display')} : {}); break;
272
- case 'space': open('mspace', {width: em(Math.max(0, node.width)), style: node.width < 0 ? 'margin-left:' + em(node.width) : undefined}); close('mspace'); break;
324
+ case 'space': {
325
+ const width = options.displayMode && node.displayWidth !== undefined ? node.displayWidth : node.width;
326
+ open('mspace', {width: em(Math.max(0, width)), style: width < 0 ? 'margin-left:' + em(width) : undefined}); close('mspace'); break;
327
+ }
273
328
  case 'scripts': scripts(node, context, depth); break;
274
329
  case 'frac': {
275
330
  const local = node.style ? styledContext(context, {mathStyle: node.style}) : context;
@@ -289,6 +344,11 @@ function mathML(tree, source, safeSource, options = {}) {
289
344
  close(node.index ? 'mroot' : 'msqrt'); break;
290
345
  case 'fence':
291
346
  open('mrow'); fence(node.left, 'prefix', context, node.size); emit(node.body, inner(context), depth + 1); fence(node.right, 'postfix', context, node.size); close('mrow'); break;
347
+ case 'enclose': {
348
+ warn('semantic-overlay', 'An enclosure uses the MathML 3 menclose extension; hosts implementing only Core may omit its lines.');
349
+ const css = (node.color ? 'color:' + node.color + ';' : '') + (node.background ? 'background:' + node.background + ';' : '');
350
+ open('menclose', {notation: node.notation.join(' '), style: css || undefined}); emit(node.body, context, depth + 1); close('menclose'); break;
351
+ }
292
352
  case 'accent': {
293
353
  if (node.overlay || node.position === 'through') {
294
354
  warn('semantic-overlay', 'An overlay uses the MathML 3 menclose extension; hosts implementing only Core may omit the cancellation or negation mark.');
@@ -300,8 +360,10 @@ function mathML(tree, source, safeSource, options = {}) {
300
360
  token('mo', node.value, {stretchy: String(!!node.stretch)}, context); close(name); break;
301
361
  }
302
362
  case 'overunder': {
363
+ const base = node.minWidth ? {...node.base, minWidth: node.minWidth} : node.base;
364
+ if (!node.over && !node.under) { emit(base, {...context, ordinary: node.unspaced || undefined}, depth + 1); break; }
303
365
  const name = node.over && node.under ? 'munderover' : node.under ? 'munder' : 'mover';
304
- open(name, {accent: node.over ? 'false' : undefined, accentunder: node.under ? 'false' : undefined}); emit(node.base, {...context, movableLimits: false}, depth + 1);
366
+ open(name, {accent: node.over ? 'false' : undefined, accentunder: node.under ? 'false' : undefined}); emit(base, {...context, movableLimits: false, ordinary: node.unspaced || undefined}, depth + 1);
305
367
  if (node.under) emit(node.under, scriptContext(context), depth + 1);
306
368
  if (node.over) emit(node.over, scriptContext(context), depth + 1);
307
369
  close(name); break;
@@ -331,6 +393,8 @@ function mathML(tree, source, safeSource, options = {}) {
331
393
  } else if (node.class) {
332
394
  const leaf = singleToken(node.body);
333
395
  if (leaf) emit({...leaf, class: node.class}, local, depth + 1);
396
+ else if (node.body.type === 'row' && node.body.children.filter(child => child.type !== 'space').length === 1 &&
397
+ node.body.children.every(child => child.type === 'space' || classOf(child) === node.class)) emit(node.body, local, depth + 1);
334
398
  else {
335
399
  warn('semantic-atom-class', 'A compound expression retains its mathematical children; its TeX atom class does not create a single MathML operator.');
336
400
  emit(node.body, local, depth + 1);
@@ -412,4 +476,5 @@ function mathML(tree, source, safeSource, options = {}) {
412
476
 
413
477
  import {mathDiagnostic} from './source.js';
414
478
  import {mathStyledCodePoint} from './variants.js';
479
+ import {mathChoice, mathTableDelimiters} from './parser.js';
415
480
  export {mathML};
@@ -5,6 +5,7 @@ export const MATH_CLASSIC = 'dist/rapier-math.js';
5
5
  export function mathPackagePath(file) {
6
6
  const owners = {
7
7
  'math/README.md': 'docs/api.md',
8
+ 'math/CHANGELOG.md': 'CHANGELOG.md',
8
9
  'math/SEMANTICS.md': 'docs/semantics.md',
9
10
  'math/semantic-verify.mjs': 'tests/semantics.mjs',
10
11
  'math/accessible.css': 'dist/accessible.css',
@@ -31,21 +31,20 @@ for host composition. All return synchronously. Type declarations accompany the
31
31
  - [MathML, accessibility and source contracts](docs/semantics.md)
32
32
  - [Runnable examples](examples/README.md)
33
33
  - [Font source and regeneration](docs/font-source.md)
34
+ - [Changes](CHANGELOG.md)
34
35
 
35
36
  ## Develop
36
37
 
37
38
  The classic script and native entry points live in \`dist/\`. Native entries import the editable
38
39
  modules in \`src/math/\` directly, sharing one parser without a duplicate generated renderer. Build
39
- tools live in \`src/tools/\`. Optional adapters have one source copy in \`dist/\`. Examples, documentation and tests each
40
+ tools live in \`src/tools/\`. Optional adapters have one source copy in \`dist/\`. Examples and documentation each
40
41
  have their own directory. Only the current release is kept in this checkout.
41
42
 
42
- From a repository checkout, run \`node tests/check.mjs\` and \`node tests/verify.mjs\` to check the package and rebuild its exact bytes.
43
43
  Run \`node src/tools/build-math.mjs --package .\` to regenerate the classic and ESM files from source.
44
- Optional host checks run with \`node tests/adoption.mjs\` after installing the example dependencies.
45
44
 
46
45
  ## License
47
46
 
48
- The renderer and adapters are MIT-licensed. Embedded Latin Modern Math outlines and metrics are
47
+ The renderer and adapters are MIT-licensed. Embedded Latin Modern outlines and metrics are
49
48
  licensed under the GUST Font License. [LICENSE](LICENSE) contains both complete grants.
50
49
  [Component provenance](docs/math-components.json) and [source hashes](docs/math-resources.json)
51
50
  travel with the package.