Skip to content

Commit

Permalink
Fixed #1465: toHTML() not correct for unary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Apr 8, 2019
1 parent b716a09 commit c155eb2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Expand Up @@ -4,6 +4,8 @@
# not yet released, version 5.9.0

- Implemented functions `row` and `column` (see #1413). Thanks @SzechuanSage.
- Fixed #1465: `node.toHTML()` not correct for unary operators like
`factorial`.


# 2019-03-20, version 5.8.0
Expand Down
7 changes: 2 additions & 5 deletions src/expression/node/OperatorNode.js
Expand Up @@ -463,12 +463,9 @@ function factory (type, config, load, typed) {

if (assoc === 'right') { // prefix operator
return '<span class="math-operator math-unary-operator math-lefthand-unary-operator">' + escape(this.op) + '</span>' + operand
} else if (assoc === 'left') { // postfix
return '<span class="math-operator math-unary-operator math-righthand-unary-operator">' + escape(this.op) + '</span>' + operand
} else { // postfix when assoc === 'left' or undefined
return operand + '<span class="math-operator math-unary-operator math-righthand-unary-operator">' + escape(this.op) + '</span>'
}

// fall back to postfix
return '<span class="math-operator math-unary-operator math-righthand-unary-operator">' + escape(this.op) + '</span>' + operand
} else if (args.length === 2) { // binary operatoes
let lhs = args[0].toHTML(options) // left hand side
let rhs = args[1].toHTML(options) // right hand side
Expand Down
23 changes: 23 additions & 0 deletions test/expression/node/OperatorNode.test.js
Expand Up @@ -766,6 +766,29 @@ describe('OperatorNode', function () {
assert.strictEqual(h.toTex({ implicit: 'show' }), '2\\cdot\\left(3+4\\right)')
})

it('should HTML operators', function () {
assert.strictEqual(math.parse('2 + 3').toHTML(),
'<span class="math-number">2</span>' +
'<span class="math-operator math-binary-operator math-explicit-binary-operator">+</span>' +
'<span class="math-number">3</span>'
)

assert.strictEqual(math.parse('not 5').toHTML(),
'<span class="math-operator math-unary-operator math-lefthand-unary-operator">not</span>' +
'<span class="math-number">5</span>'
)

assert.strictEqual(math.parse('5!').toHTML(),
'<span class="math-number">5</span>' +
'<span class="math-operator math-unary-operator math-righthand-unary-operator">!</span>'
)

assert.strictEqual(math.parse('5\'').toHTML(),
'<span class="math-number">5</span>' +
'<span class="math-operator math-unary-operator math-righthand-unary-operator">&#39;</span>'
)
})

it('should stringify implicit multiplications between ConstantNodes with parentheses', function () {
const a = math.parse('(4)(4)(4)(4)')
const b = math.parse('4b*4(4)')
Expand Down

0 comments on commit c155eb2

Please sign in to comment.