如何在 WebView 中为 Javascript 转义 Android 中的 MathML 字符串
How to escape MathML String in Android for Javascript in WebView
我正在尝试使用 MathJax 在 WebView 中显示 MathML。
显示我自己的 MathML 时效果很好:
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]);");
但是,当我尝试显示从小部件获取的 MathML 时,以下内容不起作用:
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 是一个打印到 Logcat 的字符串(在我的字符串替换之前):
<math xmlns='http://www.w3.org/1998/Math/MathML'>
<mfrac>
<mrow>
<mn> 2 </mn>
</mrow>
<mrow>
<mn> 3 </mn>
</mrow>
</mfrac>
</math>
我还从 chromium 获得了以下信息:
I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected identifier", source: (1)
任何关于如何适当地转义 mlOutput MathML-String 以便我可以在 Javascript 中使用它的帮助将不胜感激!
到目前为止,我能找到的最好的东西是关于 Apache StringEscapeUtils.escapeJavaScript 功能的。 但据我所知,这不能在 Android 中使用? 我刚刚导入了 500KB 的 .jar 库,但这不是解决方案。
编辑:
解决方案是来自此 MathJax 示例实现应用程序 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;
}
解决方案是来自此 MathJax 示例实现应用程序的一些方法 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;
}
我正在尝试使用 MathJax 在 WebView 中显示 MathML。
显示我自己的 MathML 时效果很好:
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]);");
但是,当我尝试显示从小部件获取的 MathML 时,以下内容不起作用:
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 是一个打印到 Logcat 的字符串(在我的字符串替换之前):
<math xmlns='http://www.w3.org/1998/Math/MathML'>
<mfrac>
<mrow>
<mn> 2 </mn>
</mrow>
<mrow>
<mn> 3 </mn>
</mrow>
</mfrac>
</math>
我还从 chromium 获得了以下信息:
I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected identifier", source: (1)
任何关于如何适当地转义 mlOutput MathML-String 以便我可以在 Javascript 中使用它的帮助将不胜感激!
到目前为止,我能找到的最好的东西是关于 Apache StringEscapeUtils.escapeJavaScript 功能的。 但据我所知,这不能在 Android 中使用? 我刚刚导入了 500KB 的 .jar 库,但这不是解决方案。
编辑: 解决方案是来自此 MathJax 示例实现应用程序 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;
}
解决方案是来自此 MathJax 示例实现应用程序的一些方法 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;
}