8

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. */
2
  • having same issue with latest Android Studio build and API 21 Commented Mar 6, 2015 at 21:24
  • The glitch confirmed alive and kicking up to webview/Chromium 44 in API 23. I suggest starring the issue as it was considered "obsolete" ? Commented Feb 4, 2016 at 8:20

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Does your Java string contain a linebreak after the comment row? If not, everything in the javascript element after the first "//" will be interpreted as a unterminated single line comment, because it finds no "\n" after "// test".

See comment 5 in this issue: https://issuetracker.google.com/issues/36937564#comment5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.