mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
OperatorNode: Fix bug from wrong use of getIdentifier
1+(1+1) with parenthesis set to 'auto' triggered a bug because getPrecedence gets the precedence of the content of a node, but getIdentifier still returns the 'ParenthesisNode' identifier in case of a ParenthesisNode.
This commit is contained in:
parent
2959858b99
commit
5767f1f912
@ -121,8 +121,17 @@ function factory (type, config, load, typed, math) {
|
||||
|
||||
//handle special cases for LaTeX, where some of the parentheses aren't needed
|
||||
if (latex && (operandPrecedence !== null)) {
|
||||
var operandIdentifier = args[0].getIdentifier();
|
||||
var rootIdentifier = root.getIdentifier();
|
||||
var operandIdentifier;
|
||||
var rootIdentifier;
|
||||
if (config.parenthesis === 'keep') {
|
||||
operandIdentifier = args[0].getIdentifier();
|
||||
rootIdentifier = root.getIdentifier();
|
||||
}
|
||||
else {
|
||||
//Ignore Parenthesis Nodes when not in 'keep' mode
|
||||
operandIdentifier = args[0].getContent().getIdentifier();
|
||||
rootIdentifier = root.getContent().getIdentifier();
|
||||
}
|
||||
if (operators.properties[precedence][rootIdentifier].latexLeftParens === false) {
|
||||
return [false];
|
||||
}
|
||||
@ -198,9 +207,20 @@ function factory (type, config, load, typed, math) {
|
||||
|
||||
//handle special cases for LaTeX, where some of the parentheses aren't needed
|
||||
if (latex) {
|
||||
var rootIdentifier = root.getIdentifier();
|
||||
var lhsIdentifier = root.args[0].getIdentifier();
|
||||
var rhsIdentifier = root.args[1].getIdentifier();
|
||||
var rootIdentifier;
|
||||
var lhsIdentifier;
|
||||
var rhsIdentifier;
|
||||
if (config.parenthesis === 'keep') {
|
||||
rootIdentifier = root.getIdentifier();
|
||||
lhsIdentifier = root.args[0].getIdentifier();
|
||||
rhsIdentifier = root.args[1].getIdentifier();
|
||||
}
|
||||
else {
|
||||
//Ignore ParenthesisNodes when not in 'keep' mode
|
||||
rootIdentifier = root.getContent().getIdentifier();
|
||||
lhsIdentifier = root.args[0].getContent().getIdentifier();
|
||||
rhsIdentifier = root.args[1].getContent().getIdentifier();
|
||||
}
|
||||
|
||||
if (lhsPrecedence !== null) {
|
||||
if (operators.properties[precedence][rootIdentifier].latexLeftParens === false) {
|
||||
@ -323,6 +343,14 @@ function factory (type, config, load, typed, math) {
|
||||
}
|
||||
|
||||
//handle some exceptions (due to the way LaTeX works)
|
||||
var lhsIdentifier;
|
||||
if (config.parenthesis === 'keep') {
|
||||
lhsIdentifier = lhs.getIdentifier();
|
||||
}
|
||||
else {
|
||||
//Ignore ParenthesisNodes if in 'keep' mode
|
||||
lhsIdentifier = lhs.getContent().getIdentifier();
|
||||
}
|
||||
switch (this.getIdentifier()) {
|
||||
case 'OperatorNode:divide':
|
||||
//op contains '\\frac' at this point
|
||||
@ -330,7 +358,7 @@ function factory (type, config, load, typed, math) {
|
||||
case 'OperatorNode:pow':
|
||||
lhsTex = '{' + lhsTex + '}';
|
||||
rhsTex = '{' + rhsTex + '}';
|
||||
switch (lhs.getIdentifier()) {
|
||||
switch (lhsIdentifier) {
|
||||
case 'ConditionalNode': //
|
||||
case 'OperatorNode:divide':
|
||||
lhsTex = '\\left(' + lhsTex + '\\right)';
|
||||
|
||||
@ -458,4 +458,11 @@ describe('OperatorNode', function() {
|
||||
assert.equal(pow.toTex(), '\\left({\\left\\{\\begin{array}{l l}{1}, &\\quad{\\text{if}\\;1}\\\\{1}, &\\quad{\\text{otherwise}}\\end{array}\\right.}\\right)^{1}');
|
||||
});
|
||||
|
||||
it ('should LaTeX simple expressions in \'auto\' mode', function () {
|
||||
var autoMath = math.create({parenthesis: 'auto'});
|
||||
|
||||
//this covers a bug that was triggered previously
|
||||
assert.equal(autoMath.parse('1+(1+1)').toTex(), '1+1+1');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user