Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview - single line javascript comments causing Uncaught SyntaxError errors?

I am trying to load the following html as a string into a 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");

I get the following message from the webview console:

Uncaught SyntaxError: Unexpected end of input

If I remove the "// test." comment, I don't get the syntax error. It's as if the webview is stripping newlines, and so the function body is applying the comment to the closing brace like so:

function foo() { // test. }

Can anyone else repro this? I thought maybe my readAboveContentIntoString() was stripping newlines, but tested and it is not. I'm using android 4.4.4.

Thanks

-- Edit ---

Also, a block comment works fine in place of the line comment:

/* test. */
like image 635
user3203425 Avatar asked Oct 21 '14 19:10

user3203425


1 Answers

I had the same problem. It seems the only way is to remove comments from content string first and then load it to webview.

String content = readAboveContentIntoString();
WebView webview = ...;

// Add This :
content = removeComment(content);

webview.loadData(content, "text/html", "utf-8");

The function removeComment() removes both single line comments and block comments.

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;
}
like image 116
Payam Roozbahani Fard Avatar answered Oct 21 '22 09:10

Payam Roozbahani Fard