mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

handler option for #toHTML not working

Open dphuang2 opened this issue 3 years ago • 1 comments

math.parse("sumMap(list,mapper)=sum(map(list,mapper))").toHTML({handler: (node, options) => "hello"})

'<span class="math-function">sumMap</span><span class="math-parenthesis math-round-parenthesis">(</span><span class="math-symbol math-parameter">list</span><span class="math-separator">,</span><span class="math-symbol math-parameter">mapper</span><span class="math-parenthesis math-round-parenthesis">)</span><span class="math-operator math-assignment-operator math-variable-assignment-operator math-binary-operator">=</span><span class="math-function">sum</span><span class="math-paranthesis math-round-parenthesis">(</span><span class="math-function">map</span><span class="math-paranthesis math-round-parenthesis">(</span><span class="math-symbol">list</span><span class="math-separator">,</span><span class="math-symbol">mapper</span><span class="math-paranthesis math-round-parenthesis">)</span><span class="math-paranthesis math-round-parenthesis">)</span>'

There is no "hello" in output. Am I doing something wrong here?

dphuang2 avatar Feb 17 '22 08:02 dphuang2

Thanks for reporting. I had a look, and this is simply a bug. The three methods .toString, .toTex, and .toHTML should work the same in this regard, but it's not working for .toHTML.

The reason is that each of the Node classes (FunctionNode, OperatorNode, etc). do have a .toHTML method implemented that overrides the .toHTML method of the base class Node. The solution is to rename the methods on all subclasses to ._toHTML (similar to the ._toTex and .toString methods), and refer to this ._toHTML method from the public method .toHTML in the base class Node.

I see all classes are missing unit tests for toHTML, so writing tests will probably be the most work when fix this.

A workaround:

math.Node.prototype.toHTMLWorkaround = math.Node.prototype.toHTML

math.parse("sumMap(list,mapper)=sum(map(list,mapper))").toHTMLWorkaround({handler: (node, options) => "hello"}) 
// "hello"

Anyone interested in implementing a fix?

josdejong avatar Feb 28 '22 10:02 josdejong