temml 0.10.4 → 0.10.5

Sign up to get free protection for your applications and to get access to all the features.
package/dist/temml.mjs CHANGED
@@ -1759,125 +1759,102 @@ for (let i = 0; i < 10; i++) {
1759
1759
  * much of this module.
1760
1760
  */
1761
1761
 
1762
- function setLineBreaks(expression, wrapMode, isDisplayMode, color) {
1763
- if (color === undefined && wrapMode !== "none") {
1764
- // First, make one pass through the expression and split any color nodes.
1765
- const upperLimit = expression.length - 1;
1766
- for (let i = upperLimit; i >= 0; i--) {
1767
- const node = expression[i];
1768
- if (node.type === "mstyle" && node.attributes.mathcolor) {
1769
- const color = node.attributes.mathcolor;
1770
- const fragment = setLineBreaks(node.children, wrapMode, isDisplayMode, color);
1771
- if (!(fragment.type && fragment.type !== "mtable")) {
1772
- expression.splice(i, 1, ...fragment.children);
1773
- }
1774
- }
1775
- }
1776
- }
1777
-
1778
- const tagName = color ? "mstyle" : "mrow";
1779
-
1762
+ function setLineBreaks(expression, wrapMode, isDisplayMode) {
1780
1763
  const mtrs = [];
1781
1764
  let mrows = [];
1782
1765
  let block = [];
1783
1766
  let numTopLevelEquals = 0;
1784
1767
  let canBeBIN = false; // The first node cannot be an infix binary operator.
1785
- for (let i = 0; i < expression.length; i++) {
1786
- const node = expression[i];
1787
- if (node.type && node.type === "mstyle" && node.attributes.mathcolor) {
1788
- if (block.length > 0) {
1789
- // Start a new block. (Insert a soft linebreak.)
1790
- mrows.push(new mathMLTree.MathNode(tagName, block));
1791
- }
1792
- // Insert the mstyle
1793
- mrows.push(node);
1794
- block = [];
1795
- continue
1768
+ let i = 0;
1769
+ while (i < expression.length) {
1770
+ while (expression[i] instanceof DocumentFragment) {
1771
+ expression.splice(i, 1, ...expression[i].children); // Expand the fragment.
1796
1772
  }
1773
+ const node = expression[i];
1797
1774
  if (node.attributes && node.attributes.linebreak &&
1798
1775
  node.attributes.linebreak === "newline") {
1799
1776
  // A hard line break. Create a <mtr> for the current block.
1800
1777
  if (block.length > 0) {
1801
- const element = new mathMLTree.MathNode(tagName, block);
1802
- if (color) { element.setAttribute("mathcolor", color); }
1803
- mrows.push(new mathMLTree.MathNode(tagName, block));
1778
+ mrows.push(new mathMLTree.MathNode("mrow", block));
1804
1779
  }
1805
1780
  mrows.push(node);
1806
1781
  block = [];
1807
1782
  const mtd = new mathMLTree.MathNode("mtd", mrows);
1783
+ mtd.style.textAlign = "left";
1808
1784
  mtrs.push(new mathMLTree.MathNode("mtr", [mtd]));
1809
1785
  mrows = [];
1786
+ i += 1;
1810
1787
  continue
1811
1788
  }
1812
1789
  block.push(node);
1813
- if (node.type && node.type === "mo" && wrapMode === "=") {
1814
- if (node.children.length === 1 && node.children[0].text === "=") {
1790
+ if (node.type && node.type === "mo" && node.children.length === 1) {
1791
+ if (wrapMode === "=" && node.children[0].text === "=") {
1815
1792
  numTopLevelEquals += 1;
1816
1793
  if (numTopLevelEquals > 1) {
1817
1794
  block.pop();
1818
1795
  // Start a new block. (Insert a soft linebreak.)
1819
- const element = new mathMLTree.MathNode(tagName, block);
1820
- if (color) { element.setAttribute("mathcolor", color); }
1796
+ const element = new mathMLTree.MathNode("mrow", block);
1821
1797
  mrows.push(element);
1822
1798
  block = [node];
1823
1799
  }
1824
- }
1825
- } else if (node.type && node.type === "mo" && wrapMode === "tex") {
1826
- // This may be a place for a soft line break.
1827
- if (canBeBIN && !node.attributes.form) {
1828
- // Check if the following node is a \nobreak text node, e.g. "~""
1829
- const next = i < expression.length - 1 ? expression[i + 1] : null;
1830
- let glueIsFreeOfNobreak = true;
1831
- if (
1832
- !(
1833
- next &&
1834
- next.type === "mtext" &&
1835
- next.attributes.linebreak &&
1836
- next.attributes.linebreak === "nobreak"
1837
- )
1838
- ) {
1839
- // We may need to start a new block.
1840
- // First, put any post-operator glue on same line as operator.
1841
- for (let j = i + 1; j < expression.length; j++) {
1842
- const nd = expression[j];
1843
- if (
1844
- nd.type &&
1845
- nd.type === "mspace" &&
1846
- !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
1847
- ) {
1848
- block.push(nd);
1849
- i += 1;
1800
+ } else if (wrapMode === "tex") {
1801
+ // This may be a place for a soft line break.
1802
+ if (canBeBIN && !node.attributes.form) {
1803
+ // Check if the following node is a \nobreak text node, e.g. "~""
1804
+ const next = i < expression.length - 1 ? expression[i + 1] : null;
1805
+ let glueIsFreeOfNobreak = true;
1806
+ if (
1807
+ !(
1808
+ next &&
1809
+ next.type === "mtext" &&
1810
+ next.attributes.linebreak &&
1811
+ next.attributes.linebreak === "nobreak"
1812
+ )
1813
+ ) {
1814
+ // We may need to start a new block.
1815
+ // First, put any post-operator glue on same line as operator.
1816
+ for (let j = i + 1; j < expression.length; j++) {
1817
+ const nd = expression[j];
1850
1818
  if (
1851
- nd.attributes &&
1852
- nd.attributes.linebreak &&
1853
- nd.attributes.linebreak === "nobreak"
1819
+ nd.type &&
1820
+ nd.type === "mspace" &&
1821
+ !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
1854
1822
  ) {
1855
- glueIsFreeOfNobreak = false;
1823
+ block.push(nd);
1824
+ i += 1;
1825
+ if (
1826
+ nd.attributes &&
1827
+ nd.attributes.linebreak &&
1828
+ nd.attributes.linebreak === "nobreak"
1829
+ ) {
1830
+ glueIsFreeOfNobreak = false;
1831
+ }
1832
+ } else {
1833
+ break;
1856
1834
  }
1857
- } else {
1858
- break;
1859
1835
  }
1860
1836
  }
1837
+ if (glueIsFreeOfNobreak) {
1838
+ // Start a new block. (Insert a soft linebreak.)
1839
+ const element = new mathMLTree.MathNode("mrow", block);
1840
+ mrows.push(element);
1841
+ block = [];
1842
+ }
1843
+ canBeBIN = false;
1861
1844
  }
1862
- if (glueIsFreeOfNobreak) {
1863
- // Start a new block. (Insert a soft linebreak.)
1864
- const element = new mathMLTree.MathNode(tagName, block);
1865
- if (color) { element.setAttribute("mathcolor", color); }
1866
- mrows.push(element);
1867
- block = [];
1868
- }
1869
- canBeBIN = false;
1845
+ const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix";
1846
+ // Any operator that follows an open delimiter is unary.
1847
+ canBeBIN = !(node.attributes.separator || isOpenDelimiter);
1848
+ } else {
1849
+ canBeBIN = true;
1870
1850
  }
1871
- const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix";
1872
- // Any operator that follows an open delimiter is unary.
1873
- canBeBIN = !(node.attributes.separator || isOpenDelimiter);
1874
1851
  } else {
1875
1852
  canBeBIN = true;
1876
1853
  }
1854
+ i += 1;
1877
1855
  }
1878
1856
  if (block.length > 0) {
1879
- const element = new mathMLTree.MathNode(tagName, block);
1880
- if (color) { element.setAttribute("mathcolor", color); }
1857
+ const element = new mathMLTree.MathNode("mrow", block);
1881
1858
  mrows.push(element);
1882
1859
  }
1883
1860
  if (mtrs.length > 0) {
@@ -3080,11 +3057,15 @@ const validateColor = (color, macros, token) => {
3080
3057
  };
3081
3058
 
3082
3059
  const mathmlBuilder$9 = (group, style) => {
3083
- const inner = buildExpression(group.body, style.withColor(group.color));
3084
- // Wrap with an <mstyle> element.
3085
- const node = wrapWithMstyle(inner);
3086
- node.setAttribute("mathcolor", group.color);
3087
- return node
3060
+ // In LaTeX, color is not supposed to change the spacing of any node.
3061
+ // So instead of wrapping the group in an <mstyle>, we apply
3062
+ // the color individually to each node and return a document fragment.
3063
+ let expr = buildExpression(group.body, style.withColor(group.color));
3064
+ expr = expr.map(e => {
3065
+ e.style.color = group.color;
3066
+ return e
3067
+ });
3068
+ return mathMLTree.newDocumentFragment(expr)
3088
3069
  };
3089
3070
 
3090
3071
  defineFunction({
@@ -12995,7 +12976,7 @@ class Style {
12995
12976
  * https://mit-license.org/
12996
12977
  */
12997
12978
 
12998
- const version = "0.10.4";
12979
+ const version = "0.10.5";
12999
12980
 
13000
12981
  function postProcess(block) {
13001
12982
  const labelMap = {};
@@ -14,7 +14,7 @@
14
14
  * https://mit-license.org/
15
15
  */
16
16
 
17
- const version = "0.10.4";
17
+ const version = "0.10.5";
18
18
 
19
19
  function postProcess(block) {
20
20
  const labelMap = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "temml",
3
- "version": "0.10.4",
3
+ "version": "0.10.5",
4
4
  "description": "TeX to MathML conversion in JavaScript.",
5
5
  "main": "dist/temml.js",
6
6
  "exports": {
@@ -1,5 +1,5 @@
1
1
  import defineFunction, { ordargument } from "../defineFunction"
2
- import { wrapWithMstyle } from "../mathMLTree"
2
+ import mathMLTree from "../mathMLTree"
3
3
  import { assertNodeType } from "../parseNode"
4
4
  import ParseError from "../ParseError"
5
5
  import * as mml from "../buildMathML"
@@ -152,11 +152,15 @@ export const validateColor = (color, macros, token) => {
152
152
  }
153
153
 
154
154
  const mathmlBuilder = (group, style) => {
155
- const inner = mml.buildExpression(group.body, style.withColor(group.color))
156
- // Wrap with an <mstyle> element.
157
- const node = wrapWithMstyle(inner)
158
- node.setAttribute("mathcolor", group.color)
159
- return node
155
+ // In LaTeX, color is not supposed to change the spacing of any node.
156
+ // So instead of wrapping the group in an <mstyle>, we apply
157
+ // the color individually to each node and return a document fragment.
158
+ let expr = mml.buildExpression(group.body, style.withColor(group.color))
159
+ expr = expr.map(e => {
160
+ e.style.color = group.color
161
+ return e
162
+ })
163
+ return mathMLTree.newDocumentFragment(expr)
160
164
  }
161
165
 
162
166
  defineFunction({
@@ -1,4 +1,5 @@
1
1
  import mathMLTree from "./mathMLTree"
2
+ import { DocumentFragment } from "./tree"
2
3
 
3
4
  /*
4
5
  * Neither Firefox nor Chrome support hard line breaks or soft line breaks.
@@ -25,125 +26,102 @@ import mathMLTree from "./mathMLTree"
25
26
  * much of this module.
26
27
  */
27
28
 
28
- export default function setLineBreaks(expression, wrapMode, isDisplayMode, color) {
29
- if (color === undefined && wrapMode !== "none") {
30
- // First, make one pass through the expression and split any color nodes.
31
- const upperLimit = expression.length - 1
32
- for (let i = upperLimit; i >= 0; i--) {
33
- const node = expression[i];
34
- if (node.type === "mstyle" && node.attributes.mathcolor) {
35
- const color = node.attributes.mathcolor
36
- const fragment = setLineBreaks(node.children, wrapMode, isDisplayMode, color)
37
- if (!(fragment.type && fragment.type !== "mtable")) {
38
- expression.splice(i, 1, ...fragment.children)
39
- }
40
- }
41
- }
42
- }
43
-
44
- const tagName = color ? "mstyle" : "mrow"
45
-
29
+ export default function setLineBreaks(expression, wrapMode, isDisplayMode) {
46
30
  const mtrs = [];
47
31
  let mrows = [];
48
32
  let block = [];
49
33
  let numTopLevelEquals = 0
50
34
  let canBeBIN = false // The first node cannot be an infix binary operator.
51
- for (let i = 0; i < expression.length; i++) {
52
- const node = expression[i];
53
- if (node.type && node.type === "mstyle" && node.attributes.mathcolor) {
54
- if (block.length > 0) {
55
- // Start a new block. (Insert a soft linebreak.)
56
- mrows.push(new mathMLTree.MathNode(tagName, block))
57
- }
58
- // Insert the mstyle
59
- mrows.push(node)
60
- block = [];
61
- continue
35
+ let i = 0
36
+ while (i < expression.length) {
37
+ while (expression[i] instanceof DocumentFragment) {
38
+ expression.splice(i, 1, ...expression[i].children) // Expand the fragment.
62
39
  }
40
+ const node = expression[i];
63
41
  if (node.attributes && node.attributes.linebreak &&
64
42
  node.attributes.linebreak === "newline") {
65
43
  // A hard line break. Create a <mtr> for the current block.
66
44
  if (block.length > 0) {
67
- const element = new mathMLTree.MathNode(tagName, block)
68
- if (color) { element.setAttribute("mathcolor", color) }
69
- mrows.push(new mathMLTree.MathNode(tagName, block))
45
+ mrows.push(new mathMLTree.MathNode("mrow", block))
70
46
  }
71
47
  mrows.push(node)
72
48
  block = [];
73
49
  const mtd = new mathMLTree.MathNode("mtd", mrows)
50
+ mtd.style.textAlign = "left"
74
51
  mtrs.push(new mathMLTree.MathNode("mtr", [mtd]))
75
52
  mrows = [];
53
+ i += 1
76
54
  continue
77
55
  }
78
56
  block.push(node);
79
- if (node.type && node.type === "mo" && wrapMode === "=") {
80
- if (node.children.length === 1 && node.children[0].text === "=") {
57
+ if (node.type && node.type === "mo" && node.children.length === 1) {
58
+ if (wrapMode === "=" && node.children[0].text === "=") {
81
59
  numTopLevelEquals += 1
82
60
  if (numTopLevelEquals > 1) {
83
61
  block.pop()
84
62
  // Start a new block. (Insert a soft linebreak.)
85
- const element = new mathMLTree.MathNode(tagName, block)
86
- if (color) { element.setAttribute("mathcolor", color) }
63
+ const element = new mathMLTree.MathNode("mrow", block)
87
64
  mrows.push(element)
88
65
  block = [node];
89
66
  }
90
- }
91
- } else if (node.type && node.type === "mo" && wrapMode === "tex") {
92
- // This may be a place for a soft line break.
93
- if (canBeBIN && !node.attributes.form) {
94
- // Check if the following node is a \nobreak text node, e.g. "~""
95
- const next = i < expression.length - 1 ? expression[i + 1] : null;
96
- let glueIsFreeOfNobreak = true;
97
- if (
98
- !(
99
- next &&
100
- next.type === "mtext" &&
101
- next.attributes.linebreak &&
102
- next.attributes.linebreak === "nobreak"
103
- )
104
- ) {
105
- // We may need to start a new block.
106
- // First, put any post-operator glue on same line as operator.
107
- for (let j = i + 1; j < expression.length; j++) {
108
- const nd = expression[j];
109
- if (
110
- nd.type &&
111
- nd.type === "mspace" &&
112
- !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
113
- ) {
114
- block.push(nd);
115
- i += 1;
67
+ } else if (wrapMode === "tex") {
68
+ // This may be a place for a soft line break.
69
+ if (canBeBIN && !node.attributes.form) {
70
+ // Check if the following node is a \nobreak text node, e.g. "~""
71
+ const next = i < expression.length - 1 ? expression[i + 1] : null;
72
+ let glueIsFreeOfNobreak = true;
73
+ if (
74
+ !(
75
+ next &&
76
+ next.type === "mtext" &&
77
+ next.attributes.linebreak &&
78
+ next.attributes.linebreak === "nobreak"
79
+ )
80
+ ) {
81
+ // We may need to start a new block.
82
+ // First, put any post-operator glue on same line as operator.
83
+ for (let j = i + 1; j < expression.length; j++) {
84
+ const nd = expression[j];
116
85
  if (
117
- nd.attributes &&
118
- nd.attributes.linebreak &&
119
- nd.attributes.linebreak === "nobreak"
86
+ nd.type &&
87
+ nd.type === "mspace" &&
88
+ !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
120
89
  ) {
121
- glueIsFreeOfNobreak = false;
90
+ block.push(nd);
91
+ i += 1;
92
+ if (
93
+ nd.attributes &&
94
+ nd.attributes.linebreak &&
95
+ nd.attributes.linebreak === "nobreak"
96
+ ) {
97
+ glueIsFreeOfNobreak = false;
98
+ }
99
+ } else {
100
+ break;
122
101
  }
123
- } else {
124
- break;
125
102
  }
126
103
  }
104
+ if (glueIsFreeOfNobreak) {
105
+ // Start a new block. (Insert a soft linebreak.)
106
+ const element = new mathMLTree.MathNode("mrow", block)
107
+ mrows.push(element)
108
+ block = [];
109
+ }
110
+ canBeBIN = false
127
111
  }
128
- if (glueIsFreeOfNobreak) {
129
- // Start a new block. (Insert a soft linebreak.)
130
- const element = new mathMLTree.MathNode(tagName, block)
131
- if (color) { element.setAttribute("mathcolor", color) }
132
- mrows.push(element)
133
- block = [];
134
- }
135
- canBeBIN = false;
112
+ const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix"
113
+ // Any operator that follows an open delimiter is unary.
114
+ canBeBIN = !(node.attributes.separator || isOpenDelimiter);
115
+ } else {
116
+ canBeBIN = true
136
117
  }
137
- const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix";
138
- // Any operator that follows an open delimiter is unary.
139
- canBeBIN = !(node.attributes.separator || isOpenDelimiter);
140
118
  } else {
141
- canBeBIN = true;
119
+ canBeBIN = true
142
120
  }
121
+ i += 1
143
122
  }
144
123
  if (block.length > 0) {
145
- const element = new mathMLTree.MathNode(tagName, block)
146
- if (color) { element.setAttribute("mathcolor", color) }
124
+ const element = new mathMLTree.MathNode("mrow", block)
147
125
  mrows.push(element)
148
126
  }
149
127
  if (mtrs.length > 0) {
@@ -8,7 +8,7 @@
8
8
  * https://mit-license.org/
9
9
  */
10
10
 
11
- export const version = "0.10.4";
11
+ export const version = "0.10.5";
12
12
 
13
13
  export function postProcess(block) {
14
14
  const labelMap = {}