temml 0.10.4 → 0.10.6

Sign up to get free protection for your applications and to get access to all the features.
package/dist/temml.cjs CHANGED
@@ -1761,125 +1761,102 @@ for (let i = 0; i < 10; i++) {
1761
1761
  * much of this module.
1762
1762
  */
1763
1763
 
1764
- function setLineBreaks(expression, wrapMode, isDisplayMode, color) {
1765
- if (color === undefined && wrapMode !== "none") {
1766
- // First, make one pass through the expression and split any color nodes.
1767
- const upperLimit = expression.length - 1;
1768
- for (let i = upperLimit; i >= 0; i--) {
1769
- const node = expression[i];
1770
- if (node.type === "mstyle" && node.attributes.mathcolor) {
1771
- const color = node.attributes.mathcolor;
1772
- const fragment = setLineBreaks(node.children, wrapMode, isDisplayMode, color);
1773
- if (!(fragment.type && fragment.type !== "mtable")) {
1774
- expression.splice(i, 1, ...fragment.children);
1775
- }
1776
- }
1777
- }
1778
- }
1779
-
1780
- const tagName = color ? "mstyle" : "mrow";
1781
-
1764
+ function setLineBreaks(expression, wrapMode, isDisplayMode) {
1782
1765
  const mtrs = [];
1783
1766
  let mrows = [];
1784
1767
  let block = [];
1785
1768
  let numTopLevelEquals = 0;
1786
1769
  let canBeBIN = false; // The first node cannot be an infix binary operator.
1787
- for (let i = 0; i < expression.length; i++) {
1788
- const node = expression[i];
1789
- if (node.type && node.type === "mstyle" && node.attributes.mathcolor) {
1790
- if (block.length > 0) {
1791
- // Start a new block. (Insert a soft linebreak.)
1792
- mrows.push(new mathMLTree.MathNode(tagName, block));
1793
- }
1794
- // Insert the mstyle
1795
- mrows.push(node);
1796
- block = [];
1797
- continue
1770
+ let i = 0;
1771
+ while (i < expression.length) {
1772
+ while (expression[i] instanceof DocumentFragment) {
1773
+ expression.splice(i, 1, ...expression[i].children); // Expand the fragment.
1798
1774
  }
1775
+ const node = expression[i];
1799
1776
  if (node.attributes && node.attributes.linebreak &&
1800
1777
  node.attributes.linebreak === "newline") {
1801
1778
  // A hard line break. Create a <mtr> for the current block.
1802
1779
  if (block.length > 0) {
1803
- const element = new mathMLTree.MathNode(tagName, block);
1804
- if (color) { element.setAttribute("mathcolor", color); }
1805
- mrows.push(new mathMLTree.MathNode(tagName, block));
1780
+ mrows.push(new mathMLTree.MathNode("mrow", block));
1806
1781
  }
1807
1782
  mrows.push(node);
1808
1783
  block = [];
1809
1784
  const mtd = new mathMLTree.MathNode("mtd", mrows);
1785
+ mtd.style.textAlign = "left";
1810
1786
  mtrs.push(new mathMLTree.MathNode("mtr", [mtd]));
1811
1787
  mrows = [];
1788
+ i += 1;
1812
1789
  continue
1813
1790
  }
1814
1791
  block.push(node);
1815
- if (node.type && node.type === "mo" && wrapMode === "=") {
1816
- if (node.children.length === 1 && node.children[0].text === "=") {
1792
+ if (node.type && node.type === "mo" && node.children.length === 1) {
1793
+ if (wrapMode === "=" && node.children[0].text === "=") {
1817
1794
  numTopLevelEquals += 1;
1818
1795
  if (numTopLevelEquals > 1) {
1819
1796
  block.pop();
1820
1797
  // Start a new block. (Insert a soft linebreak.)
1821
- const element = new mathMLTree.MathNode(tagName, block);
1822
- if (color) { element.setAttribute("mathcolor", color); }
1798
+ const element = new mathMLTree.MathNode("mrow", block);
1823
1799
  mrows.push(element);
1824
1800
  block = [node];
1825
1801
  }
1826
- }
1827
- } else if (node.type && node.type === "mo" && wrapMode === "tex") {
1828
- // This may be a place for a soft line break.
1829
- if (canBeBIN && !node.attributes.form) {
1830
- // Check if the following node is a \nobreak text node, e.g. "~""
1831
- const next = i < expression.length - 1 ? expression[i + 1] : null;
1832
- let glueIsFreeOfNobreak = true;
1833
- if (
1834
- !(
1835
- next &&
1836
- next.type === "mtext" &&
1837
- next.attributes.linebreak &&
1838
- next.attributes.linebreak === "nobreak"
1839
- )
1840
- ) {
1841
- // We may need to start a new block.
1842
- // First, put any post-operator glue on same line as operator.
1843
- for (let j = i + 1; j < expression.length; j++) {
1844
- const nd = expression[j];
1845
- if (
1846
- nd.type &&
1847
- nd.type === "mspace" &&
1848
- !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
1849
- ) {
1850
- block.push(nd);
1851
- i += 1;
1802
+ } else if (wrapMode === "tex") {
1803
+ // This may be a place for a soft line break.
1804
+ if (canBeBIN && !node.attributes.form) {
1805
+ // Check if the following node is a \nobreak text node, e.g. "~""
1806
+ const next = i < expression.length - 1 ? expression[i + 1] : null;
1807
+ let glueIsFreeOfNobreak = true;
1808
+ if (
1809
+ !(
1810
+ next &&
1811
+ next.type === "mtext" &&
1812
+ next.attributes.linebreak &&
1813
+ next.attributes.linebreak === "nobreak"
1814
+ )
1815
+ ) {
1816
+ // We may need to start a new block.
1817
+ // First, put any post-operator glue on same line as operator.
1818
+ for (let j = i + 1; j < expression.length; j++) {
1819
+ const nd = expression[j];
1852
1820
  if (
1853
- nd.attributes &&
1854
- nd.attributes.linebreak &&
1855
- nd.attributes.linebreak === "nobreak"
1821
+ nd.type &&
1822
+ nd.type === "mspace" &&
1823
+ !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
1856
1824
  ) {
1857
- glueIsFreeOfNobreak = false;
1825
+ block.push(nd);
1826
+ i += 1;
1827
+ if (
1828
+ nd.attributes &&
1829
+ nd.attributes.linebreak &&
1830
+ nd.attributes.linebreak === "nobreak"
1831
+ ) {
1832
+ glueIsFreeOfNobreak = false;
1833
+ }
1834
+ } else {
1835
+ break;
1858
1836
  }
1859
- } else {
1860
- break;
1861
1837
  }
1862
1838
  }
1839
+ if (glueIsFreeOfNobreak) {
1840
+ // Start a new block. (Insert a soft linebreak.)
1841
+ const element = new mathMLTree.MathNode("mrow", block);
1842
+ mrows.push(element);
1843
+ block = [];
1844
+ }
1845
+ canBeBIN = false;
1863
1846
  }
1864
- if (glueIsFreeOfNobreak) {
1865
- // Start a new block. (Insert a soft linebreak.)
1866
- const element = new mathMLTree.MathNode(tagName, block);
1867
- if (color) { element.setAttribute("mathcolor", color); }
1868
- mrows.push(element);
1869
- block = [];
1870
- }
1871
- canBeBIN = false;
1847
+ const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix";
1848
+ // Any operator that follows an open delimiter is unary.
1849
+ canBeBIN = !(node.attributes.separator || isOpenDelimiter);
1850
+ } else {
1851
+ canBeBIN = true;
1872
1852
  }
1873
- const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix";
1874
- // Any operator that follows an open delimiter is unary.
1875
- canBeBIN = !(node.attributes.separator || isOpenDelimiter);
1876
1853
  } else {
1877
1854
  canBeBIN = true;
1878
1855
  }
1856
+ i += 1;
1879
1857
  }
1880
1858
  if (block.length > 0) {
1881
- const element = new mathMLTree.MathNode(tagName, block);
1882
- if (color) { element.setAttribute("mathcolor", color); }
1859
+ const element = new mathMLTree.MathNode("mrow", block);
1883
1860
  mrows.push(element);
1884
1861
  }
1885
1862
  if (mtrs.length > 0) {
@@ -2014,7 +1991,9 @@ const consolidateNumbers = expression => {
2014
1991
  */
2015
1992
  const makeRow = function(body) {
2016
1993
  if (body.length === 1) {
2017
- return body[0];
1994
+ return body[0] instanceof DocumentFragment
1995
+ ? body[0]
1996
+ : new mathMLTree.MathNode("mrow", body);
2018
1997
  } else {
2019
1998
  return new mathMLTree.MathNode("mrow", body);
2020
1999
  }
@@ -2128,7 +2107,6 @@ function buildMathML(tree, texExpression, style, settings) {
2128
2107
 
2129
2108
  const n1 = expression.length === 0 ? null : expression[0];
2130
2109
  let wrapper = expression.length === 1 && tag === null && (n1 instanceof MathNode)
2131
- && !(n1.type === "mstyle" && n1.attributes.mathcolor)
2132
2110
  ? expression[0]
2133
2111
  : setLineBreaks(expression, wrap, settings.displayMode);
2134
2112
 
@@ -3055,7 +3033,7 @@ const colorFromSpec = (model, spec) => {
3055
3033
  spec.split(",").map(e => {
3056
3034
  const num = Number(e.trim());
3057
3035
  if (num > 1) { throw new ParseError("Color rgb input must be < 1.") }
3058
- color += toHex((num * 255));
3036
+ color += toHex(Number((num * 255).toFixed(0)));
3059
3037
  });
3060
3038
  }
3061
3039
  if (color.charAt(0) !== "#") { color = "#" + color; }
@@ -3082,11 +3060,15 @@ const validateColor = (color, macros, token) => {
3082
3060
  };
3083
3061
 
3084
3062
  const mathmlBuilder$9 = (group, style) => {
3085
- const inner = buildExpression(group.body, style.withColor(group.color));
3086
- // Wrap with an <mstyle> element.
3087
- const node = wrapWithMstyle(inner);
3088
- node.setAttribute("mathcolor", group.color);
3089
- return node
3063
+ // In LaTeX, color is not supposed to change the spacing of any node.
3064
+ // So instead of wrapping the group in an <mstyle>, we apply
3065
+ // the color individually to each node and return a document fragment.
3066
+ let expr = buildExpression(group.body, style.withColor(group.color));
3067
+ expr = expr.map(e => {
3068
+ e.style.color = group.color;
3069
+ return e
3070
+ });
3071
+ return mathMLTree.newDocumentFragment(expr)
3090
3072
  };
3091
3073
 
3092
3074
  defineFunction({
@@ -3738,7 +3720,7 @@ defineFunction({
3738
3720
  if (group.right === "\u2216" || group.right.indexOf("arrow") > -1) {
3739
3721
  rightNode.setAttribute("stretchy", "true");
3740
3722
  }
3741
- if (group.rightColor) { rightNode.setAttribute("mathcolor", group.rightColor); }
3723
+ if (group.rightColor) { rightNode.style.color = group.rightColor; }
3742
3724
  inner.push(rightNode);
3743
3725
 
3744
3726
  return makeRow(inner);
@@ -4922,7 +4904,6 @@ const mathmlBuilder$6 = (group, style) => {
4922
4904
  for (let i = 1; i < mathGroup.children.length; i++) {
4923
4905
  mi.children.push(mathGroup.children[i].children[0]);
4924
4906
  }
4925
- if (mathGroup.attributes.mathcolor) { mi.attributes.mathcolor = mathGroup.attributes.mathcolor; }
4926
4907
  if (mi.attributes.mathvariant && mi.attributes.mathvariant === "normal") {
4927
4908
  // Workaround for a Firefox bug that renders spurious space around
4928
4909
  // a <mi mathvariant="normal">
@@ -12997,7 +12978,7 @@ class Style {
12997
12978
  * https://mit-license.org/
12998
12979
  */
12999
12980
 
13000
- const version = "0.10.4";
12981
+ const version = "0.10.6";
13001
12982
 
13002
12983
  function postProcess(block) {
13003
12984
  const labelMap = {};
package/dist/temml.js CHANGED
@@ -1762,125 +1762,102 @@ var temml = (function () {
1762
1762
  * much of this module.
1763
1763
  */
1764
1764
 
1765
- function setLineBreaks(expression, wrapMode, isDisplayMode, color) {
1766
- if (color === undefined && wrapMode !== "none") {
1767
- // First, make one pass through the expression and split any color nodes.
1768
- const upperLimit = expression.length - 1;
1769
- for (let i = upperLimit; i >= 0; i--) {
1770
- const node = expression[i];
1771
- if (node.type === "mstyle" && node.attributes.mathcolor) {
1772
- const color = node.attributes.mathcolor;
1773
- const fragment = setLineBreaks(node.children, wrapMode, isDisplayMode, color);
1774
- if (!(fragment.type && fragment.type !== "mtable")) {
1775
- expression.splice(i, 1, ...fragment.children);
1776
- }
1777
- }
1778
- }
1779
- }
1780
-
1781
- const tagName = color ? "mstyle" : "mrow";
1782
-
1765
+ function setLineBreaks(expression, wrapMode, isDisplayMode) {
1783
1766
  const mtrs = [];
1784
1767
  let mrows = [];
1785
1768
  let block = [];
1786
1769
  let numTopLevelEquals = 0;
1787
1770
  let canBeBIN = false; // The first node cannot be an infix binary operator.
1788
- for (let i = 0; i < expression.length; i++) {
1789
- const node = expression[i];
1790
- if (node.type && node.type === "mstyle" && node.attributes.mathcolor) {
1791
- if (block.length > 0) {
1792
- // Start a new block. (Insert a soft linebreak.)
1793
- mrows.push(new mathMLTree.MathNode(tagName, block));
1794
- }
1795
- // Insert the mstyle
1796
- mrows.push(node);
1797
- block = [];
1798
- continue
1771
+ let i = 0;
1772
+ while (i < expression.length) {
1773
+ while (expression[i] instanceof DocumentFragment) {
1774
+ expression.splice(i, 1, ...expression[i].children); // Expand the fragment.
1799
1775
  }
1776
+ const node = expression[i];
1800
1777
  if (node.attributes && node.attributes.linebreak &&
1801
1778
  node.attributes.linebreak === "newline") {
1802
1779
  // A hard line break. Create a <mtr> for the current block.
1803
1780
  if (block.length > 0) {
1804
- const element = new mathMLTree.MathNode(tagName, block);
1805
- if (color) { element.setAttribute("mathcolor", color); }
1806
- mrows.push(new mathMLTree.MathNode(tagName, block));
1781
+ mrows.push(new mathMLTree.MathNode("mrow", block));
1807
1782
  }
1808
1783
  mrows.push(node);
1809
1784
  block = [];
1810
1785
  const mtd = new mathMLTree.MathNode("mtd", mrows);
1786
+ mtd.style.textAlign = "left";
1811
1787
  mtrs.push(new mathMLTree.MathNode("mtr", [mtd]));
1812
1788
  mrows = [];
1789
+ i += 1;
1813
1790
  continue
1814
1791
  }
1815
1792
  block.push(node);
1816
- if (node.type && node.type === "mo" && wrapMode === "=") {
1817
- if (node.children.length === 1 && node.children[0].text === "=") {
1793
+ if (node.type && node.type === "mo" && node.children.length === 1) {
1794
+ if (wrapMode === "=" && node.children[0].text === "=") {
1818
1795
  numTopLevelEquals += 1;
1819
1796
  if (numTopLevelEquals > 1) {
1820
1797
  block.pop();
1821
1798
  // Start a new block. (Insert a soft linebreak.)
1822
- const element = new mathMLTree.MathNode(tagName, block);
1823
- if (color) { element.setAttribute("mathcolor", color); }
1799
+ const element = new mathMLTree.MathNode("mrow", block);
1824
1800
  mrows.push(element);
1825
1801
  block = [node];
1826
1802
  }
1827
- }
1828
- } else if (node.type && node.type === "mo" && wrapMode === "tex") {
1829
- // This may be a place for a soft line break.
1830
- if (canBeBIN && !node.attributes.form) {
1831
- // Check if the following node is a \nobreak text node, e.g. "~""
1832
- const next = i < expression.length - 1 ? expression[i + 1] : null;
1833
- let glueIsFreeOfNobreak = true;
1834
- if (
1835
- !(
1836
- next &&
1837
- next.type === "mtext" &&
1838
- next.attributes.linebreak &&
1839
- next.attributes.linebreak === "nobreak"
1840
- )
1841
- ) {
1842
- // We may need to start a new block.
1843
- // First, put any post-operator glue on same line as operator.
1844
- for (let j = i + 1; j < expression.length; j++) {
1845
- const nd = expression[j];
1846
- if (
1847
- nd.type &&
1848
- nd.type === "mspace" &&
1849
- !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
1850
- ) {
1851
- block.push(nd);
1852
- i += 1;
1803
+ } else if (wrapMode === "tex") {
1804
+ // This may be a place for a soft line break.
1805
+ if (canBeBIN && !node.attributes.form) {
1806
+ // Check if the following node is a \nobreak text node, e.g. "~""
1807
+ const next = i < expression.length - 1 ? expression[i + 1] : null;
1808
+ let glueIsFreeOfNobreak = true;
1809
+ if (
1810
+ !(
1811
+ next &&
1812
+ next.type === "mtext" &&
1813
+ next.attributes.linebreak &&
1814
+ next.attributes.linebreak === "nobreak"
1815
+ )
1816
+ ) {
1817
+ // We may need to start a new block.
1818
+ // First, put any post-operator glue on same line as operator.
1819
+ for (let j = i + 1; j < expression.length; j++) {
1820
+ const nd = expression[j];
1853
1821
  if (
1854
- nd.attributes &&
1855
- nd.attributes.linebreak &&
1856
- nd.attributes.linebreak === "nobreak"
1822
+ nd.type &&
1823
+ nd.type === "mspace" &&
1824
+ !(nd.attributes.linebreak && nd.attributes.linebreak === "newline")
1857
1825
  ) {
1858
- glueIsFreeOfNobreak = false;
1826
+ block.push(nd);
1827
+ i += 1;
1828
+ if (
1829
+ nd.attributes &&
1830
+ nd.attributes.linebreak &&
1831
+ nd.attributes.linebreak === "nobreak"
1832
+ ) {
1833
+ glueIsFreeOfNobreak = false;
1834
+ }
1835
+ } else {
1836
+ break;
1859
1837
  }
1860
- } else {
1861
- break;
1862
1838
  }
1863
1839
  }
1840
+ if (glueIsFreeOfNobreak) {
1841
+ // Start a new block. (Insert a soft linebreak.)
1842
+ const element = new mathMLTree.MathNode("mrow", block);
1843
+ mrows.push(element);
1844
+ block = [];
1845
+ }
1846
+ canBeBIN = false;
1864
1847
  }
1865
- if (glueIsFreeOfNobreak) {
1866
- // Start a new block. (Insert a soft linebreak.)
1867
- const element = new mathMLTree.MathNode(tagName, block);
1868
- if (color) { element.setAttribute("mathcolor", color); }
1869
- mrows.push(element);
1870
- block = [];
1871
- }
1872
- canBeBIN = false;
1848
+ const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix";
1849
+ // Any operator that follows an open delimiter is unary.
1850
+ canBeBIN = !(node.attributes.separator || isOpenDelimiter);
1851
+ } else {
1852
+ canBeBIN = true;
1873
1853
  }
1874
- const isOpenDelimiter = node.attributes.form && node.attributes.form === "prefix";
1875
- // Any operator that follows an open delimiter is unary.
1876
- canBeBIN = !(node.attributes.separator || isOpenDelimiter);
1877
1854
  } else {
1878
1855
  canBeBIN = true;
1879
1856
  }
1857
+ i += 1;
1880
1858
  }
1881
1859
  if (block.length > 0) {
1882
- const element = new mathMLTree.MathNode(tagName, block);
1883
- if (color) { element.setAttribute("mathcolor", color); }
1860
+ const element = new mathMLTree.MathNode("mrow", block);
1884
1861
  mrows.push(element);
1885
1862
  }
1886
1863
  if (mtrs.length > 0) {
@@ -2015,7 +1992,9 @@ var temml = (function () {
2015
1992
  */
2016
1993
  const makeRow = function(body) {
2017
1994
  if (body.length === 1) {
2018
- return body[0];
1995
+ return body[0] instanceof DocumentFragment
1996
+ ? body[0]
1997
+ : new mathMLTree.MathNode("mrow", body);
2019
1998
  } else {
2020
1999
  return new mathMLTree.MathNode("mrow", body);
2021
2000
  }
@@ -2129,7 +2108,6 @@ var temml = (function () {
2129
2108
 
2130
2109
  const n1 = expression.length === 0 ? null : expression[0];
2131
2110
  let wrapper = expression.length === 1 && tag === null && (n1 instanceof MathNode)
2132
- && !(n1.type === "mstyle" && n1.attributes.mathcolor)
2133
2111
  ? expression[0]
2134
2112
  : setLineBreaks(expression, wrap, settings.displayMode);
2135
2113
 
@@ -3056,7 +3034,7 @@ var temml = (function () {
3056
3034
  spec.split(",").map(e => {
3057
3035
  const num = Number(e.trim());
3058
3036
  if (num > 1) { throw new ParseError("Color rgb input must be < 1.") }
3059
- color += toHex((num * 255));
3037
+ color += toHex(Number((num * 255).toFixed(0)));
3060
3038
  });
3061
3039
  }
3062
3040
  if (color.charAt(0) !== "#") { color = "#" + color; }
@@ -3083,11 +3061,15 @@ var temml = (function () {
3083
3061
  };
3084
3062
 
3085
3063
  const mathmlBuilder$9 = (group, style) => {
3086
- const inner = buildExpression(group.body, style.withColor(group.color));
3087
- // Wrap with an <mstyle> element.
3088
- const node = wrapWithMstyle(inner);
3089
- node.setAttribute("mathcolor", group.color);
3090
- return node
3064
+ // In LaTeX, color is not supposed to change the spacing of any node.
3065
+ // So instead of wrapping the group in an <mstyle>, we apply
3066
+ // the color individually to each node and return a document fragment.
3067
+ let expr = buildExpression(group.body, style.withColor(group.color));
3068
+ expr = expr.map(e => {
3069
+ e.style.color = group.color;
3070
+ return e
3071
+ });
3072
+ return mathMLTree.newDocumentFragment(expr)
3091
3073
  };
3092
3074
 
3093
3075
  defineFunction({
@@ -3739,7 +3721,7 @@ var temml = (function () {
3739
3721
  if (group.right === "\u2216" || group.right.indexOf("arrow") > -1) {
3740
3722
  rightNode.setAttribute("stretchy", "true");
3741
3723
  }
3742
- if (group.rightColor) { rightNode.setAttribute("mathcolor", group.rightColor); }
3724
+ if (group.rightColor) { rightNode.style.color = group.rightColor; }
3743
3725
  inner.push(rightNode);
3744
3726
 
3745
3727
  return makeRow(inner);
@@ -4923,7 +4905,6 @@ var temml = (function () {
4923
4905
  for (let i = 1; i < mathGroup.children.length; i++) {
4924
4906
  mi.children.push(mathGroup.children[i].children[0]);
4925
4907
  }
4926
- if (mathGroup.attributes.mathcolor) { mi.attributes.mathcolor = mathGroup.attributes.mathcolor; }
4927
4908
  if (mi.attributes.mathvariant && mi.attributes.mathvariant === "normal") {
4928
4909
  // Workaround for a Firefox bug that renders spurious space around
4929
4910
  // a <mi mathvariant="normal">
@@ -11098,7 +11079,7 @@ var temml = (function () {
11098
11079
  * https://mit-license.org/
11099
11080
  */
11100
11081
 
11101
- const version = "0.10.4";
11082
+ const version = "0.10.6";
11102
11083
 
11103
11084
  function postProcess(block) {
11104
11085
  const labelMap = {};