Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mathml2chtml fails with a newline at the end. #2805

Closed
NSoiffer opened this issue Nov 20, 2021 · 3 comments
Closed

mathml2chtml fails with a newline at the end. #2805

NSoiffer opened this issue Nov 20, 2021 · 3 comments
Labels
Accepted Issue has been reproduced by MathJax team Code Example Contains an illustrative code example, solution, or work-around Feature Request Fixed v3 v3.2
Milestone

Comments

@NSoiffer
Copy link

Issue Summary

A newline at the end of some MathML in a call to mathml2chtml causes an error (actually, the call never returns)
This works

MathJax.mathml2chtml("<math><mi>XXX</mi></math>");

This fails:

MathJax.mathml2chtml("<math><mi>XXX</mi></math>\n");

Steps to Reproduce:

I tried to get a codepen example to run, and codepen complained about "MathJax" not being defined. Here's some HTML that shows the problem (the "AFTER..." is never shown):

<!DOCTYPE html>
<html>
<head>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/mml-chtml.js"></script> 
<script>
function ConvertMathML() {
  MathJax.startup.defaultReady();
  document.getElementById("before").innerHTML = "BEFORE conversion";
  let foo = MathJax.mathml2chtml("<math><mi>XXX</mi></math>\n");
  document.getElementById("after").innerHTML = "AFTER conversion";
}
</script>
</head>
<body>
<button onclick="ConvertMathML()"> Click me to see bug </button>
<p id="before"></p>
<p id="after"></p>
</body>
</html>
@dpvc dpvc added Accepted Issue has been reproduced by MathJax team v3 labels Dec 2, 2021
@dpvc
Copy link
Member

dpvc commented Dec 2, 2021

The reason this is happening is that MathJax parses the string you pass it as HTML, not XML (mainly to support embedded HTML in <mtext> elements, which we plan to support in the future). That means your string produces two HTML nodes: the <math> node followed by a #text node containing the new line, and MathJax complains about that. If you check the browser console, you will see an error message to that effect.

You can set what type of parsing will be used, however, in the options for the MathML input jax.

MathJax = {
  mml: {
    parseAs: 'xml'
  }
};

Unfortunately, there is a bug with the XML parsing that causes this to fail (it will be fixed in the next release). But you can work around it using

MathJax = {
  mml: {
    parseAs: 'xml'
  },
  startup: {
    ready() {
      const {HTMLAdaptor} = MathJax._.adaptors.HTMLAdaptor;
      HTMLAdaptor.prototype.body = function(doc) {return doc.body || doc};
      MathJax.startup.defaultReady();
    }
  }
}

for now.

It would be reasonable for MathJax to trim the whitespace from the MathML before parsing it, however.

@dpvc dpvc added Code Example Contains an illustrative code example, solution, or work-around Feature Request Ready for Development labels Dec 2, 2021
@NSoiffer
Copy link
Author

NSoiffer commented Dec 2, 2021

Trimming whitespace was the solution I did to avoid the problem. It just took a while to figure out. I think MathJax trimming whitespace makes a lot of sense... but I don't know enough to know if there is any downside.

Unfortunately the message "MathML must consist of a single element" wasn't helpful in figuring out the problem in that as far as I could tell, I was sending a single element. I thought that maybe there was bad tagging in the MathML I generated.

@dpvc
Copy link
Member

dpvc commented Dec 2, 2021

Yes, the message could probably be better. But trimming the white space should be sufficient to resolve the main issue.

@dpvc dpvc self-assigned this Jan 11, 2022
dpvc added a commit to mathjax/MathJax-src that referenced this issue Jan 18, 2022
@dpvc dpvc added this to the 3.2.1 milestone Jan 31, 2022
dpvc added a commit to mathjax/MathJax-src that referenced this issue Feb 22, 2022
Trim MathML string before parsing it.  (mathjax/MathJax#2805)
@dpvc dpvc added Merged Merged into develop branch and removed Ready for Review labels Feb 22, 2022
@dpvc dpvc removed their assignment Jun 1, 2022
@dpvc dpvc added Fixed v3.2 and removed Merged Merged into develop branch labels Jun 1, 2022
@dpvc dpvc closed this as completed Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Issue has been reproduced by MathJax team Code Example Contains an illustrative code example, solution, or work-around Feature Request Fixed v3 v3.2
Projects
None yet
Development

No branches or pull requests

2 participants