use*_*425 7 android webview android-webview
我试图将以下html作为字符串加载到webview中:
<html>
<head>
<script>
function foo() {
// test.
}
</script>
</head>
<body>
<p>hi.</p>
</body>
</html>
------------------------------
String content = readAboveContentIntoString();
WebView webview = ...;
webview.loadData(content, "text/html", "utf-8");
Run Code Online (Sandbox Code Playgroud)
我从webview控制台收到以下消息:
Uncaught SyntaxError: Unexpected end of input
Run Code Online (Sandbox Code Playgroud)
如果我删除"// test".评论,我没有得到语法错误.这就好像webview正在剥离换行符,因此函数体将注释应用于右括号,如下所示:
function foo() { // test. }
Run Code Online (Sandbox Code Playgroud)
其他人可以重复这个吗?我想也许我的readAboveContentIntoString()正在剥离换行符,但是经过测试但事实并非如此.我正在使用android 4.4.4.
谢谢
- 编辑---
此外,块注释可以替代行注释:
/* test. */
Run Code Online (Sandbox Code Playgroud)
我有同样的问题。看来唯一的方法是先从内容字符串中删除注释,然后将其加载到 webview 中。
String content = readAboveContentIntoString();
WebView webview = ...;
// Add This :
content = removeComment(content);
webview.loadData(content, "text/html", "utf-8");
Run Code Online (Sandbox Code Playgroud)
函数removeComment() 删除单行注释和块注释。
private String removeComment(String codeString){
int pointer;
int[] pos;
String str = codeString;
while(true) {
pointer = 0;
pos = new int[2];
pos[0] = str.indexOf("/*",pointer);
pos[1] = str.indexOf("//",pointer);
int xPos = xMin(pos);
if(xPos != -1){
//========================= Pos 0
if(xPos == pos[0]){
pointer = xPos + 2;
int pos2 = str.indexOf("*/", pointer);
if(pos2 != -1){
str = str.substring(0,xPos) + str.substring(pos2+2,str.length());
}
else{
str = str.substring(0,xPos);
break;
}
}
//========================= Pos 1
if(xPos == pos[1]){
pointer = xPos + 2;
int pos2 = str.indexOf('\n', pointer);
if(pos2 != -1){
str = str.substring(0,xPos) + str.substring(pos2+1,str.length());
}
else{
str = str.substring(0,xPos);
break;
}
}
}
else break;
}
return str;
}
private int xMin(int[] x){
int out = -1;
for(int i = 0;i < x.length;i++){
if(x[i] > out)out = x[i];
}
if(out == -1)return out;
for(int i = 0;i < x.length;i++){
if(x[i] != -1 && x[i] < out)out = x[i];
}
return out;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1115 次 |
| 最近记录: |