Reputation: 31
I am trying to display MathML in a WebView using MathJax.
When displaying my own MathML this works well:
w.evaluateJavascript("javascript:document.getElementById('math').innerHTML='"
+ "New <math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
+ "<mi>d</mi><mo>≠</mo><mn>13</mn>"
+ "</math>'",
null);
w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
However, when I try to display MathML which I get from a widget, the following does not work:
mlOutput = mlOutput.replaceAll("(\\r|\\n)", ""); //some random trial and error
mlOutput = mlOutput.replaceAll("'", "\\'");
mlOutput = mlOutput.replaceAll("\'", "\\\'");
mlOutput = mlOutput.replaceAll("'", "\\\'");
w.evaluateJavascript("javascript:document.getElementById('math').innerHTML='"
+ mlOutput+ "'",
null);
w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
mlOutput is a String which is printed to Logcat like this (before my string replacing):
<math xmlns='http://www.w3.org/1998/Math/MathML'>
<mfrac>
<mrow>
<mn> 2 </mn>
</mrow>
<mrow>
<mn> 3 </mn>
</mrow>
</mfrac>
</math>
Also I get the following Info from chromium:
I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected identifier", source: (1)
Any help on how to appropriately escape the mlOutput MathML-String so that I can use it within Javascript would be highly appreciated!
So far the best I could find was something about the Apache StringEscapeUtils.escapeJavaScript function. But as far as I know this can not be used within Android? I just imported the 500KB .jar library but this was not the solution.
Edit: the solution was some method from this MathJax sample implementation app https://github.com/leathrum/android-apps/tree/master/MathJaxApp
mlOutput = doubleEscape(mlOutput);
private String doubleEscape(String s) {
String t="";
for (int i=0; i < s.length(); i++) {
if (s.charAt(i) == '\'') t += '\\';
if (s.charAt(i) != '\n') t += s.charAt(i);
if (s.charAt(i) == '\\') t += "\\";
}
return t;
}
Upvotes: 0
Views: 305
Reputation: 31
the solution was some method from this MathJax sample implementation app https://github.com/leathrum/android-apps/tree/master/MathJaxApp
mlOutput = doubleEscape(mlOutput);
private String doubleEscape(String s) {
String t="";
for (int i=0; i < s.length(); i++) {
if (s.charAt(i) == '\'') t += '\\';
if (s.charAt(i) != '\n') t += s.charAt(i);
if (s.charAt(i) == '\\') t += "\\";
}
return t;
}
Upvotes: 1