temml 0.10.3 → 0.10.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.3";
11
+ export const version = "0.10.5";
12
12
 
13
13
  export function postProcess(block) {
14
14
  const labelMap = {}