Menu ▾ ▴

#1266 Spurious </script> tag causes javascript to fail

2.10
open
nobody
None
5
2015-01-01
2011-03-21
Matt
No

The following test case works in Chrome, Firefox, and Internet Explorer, but htmlunit does not correctly load all javascript and results in an incorrect dom. Any javascript following the spurious </script> tag is not executed.

     /**

     * @throws Exception if the test fails
     */
    @Test
    public void shouldHandleFunkyDocumentWriteScriptTag() throws Exception {
        final String content = "<html>\n"
            + "<head>\n"
            + "  <title>test</title>\n"
            + "  <script language=\"JavaScript1.1\">\n"
        + "    <!--\n"
            + "document.write('<SCRIPT LANGUAGE=JavaScript1.1 SRC=\"
http://bogus.destination.com/fakescript.js\">');\n"
             + "//--></script>\n"
             + "</SCRIPT>\n"
            + "</head>\n"
            + "<body>\n"
        + "<div>\n"
            + "alert('test');"
             + "</div>\n"
            + "</body>\n"
            + "</html>";

        final List<String> collectedAlerts =
Collections.synchronizedList(new ArrayList<String>());
        final HtmlPage page = loadPage(content, collectedAlerts);
        final JavaScriptJobManager jobManager =
page.getEnclosingWindow().getJobManager();
        assertNotNull(jobManager);

        // Not perfect, but 100 chances to start should be enough for a
loaded system
        Thread.sleep(500);

        Assert.assertFalse("At least one alert should have fired by now",
collectedAlerts.isEmpty());
        ((TopLevelWindow) page.getEnclosingWindow()).close();

        // 100 chances to stop
        jobManager.waitForJobs(500);

        final int finalValue = collectedAlerts.size();

        // 100 chances to fail
        jobManager.waitForJobs(500);

        Assert.assertEquals("No new alerts should have happened",
finalValue, collectedAlerts.size());
    }

Discussion

  • Matt

    Matt - 2011-03-22

    Looks like I accidentally oversimplified my test case. I'll paste a better example below. I'm starting to think what I'm seeing may be a timing issue. I'm expecting the link to a js file at hosted2.ap.org, below, to print out a number of new DOM elements including a number of img tags pointed to a service reference ending with 'Image.svc'. What I see, instead, is the original script tag, unevaluated, in the DOM. If you want more js src locations to try (in case the one below stops working) you can find a number of them here: http://businessinsavannah.com/ssi/aggregator.shtml

    /**
    
    * @throws Exception if the test fails
    */
    @Test
    public void shouldHandleFunkyDocumentWriteScriptTag() throws Exception {
    final String content = "<html>\n"
    + "<head>\n"
    + " <title>test</title>\n"
    + " <script language=\"JavaScript1.1\">\n"
    + " <!--\n"
    + "document.write('<SCRIPT LANGUAGE=JavaScript1.1 SRC=\"http://bogus.destination.com/fakescript.js\">');\n"
    + "//--></script>\n"
    + "</SCRIPT>\n"
    + "</head>\n"
    + "<body>\n"
    + "<div>\n"
    + "<script src=\"http://hosted2.ap.org/js/wrko/47cfc2b1fb494a84925f31f4af0cea5e\">"
    + "</script>"
    + "</div>\n"
    + "</body>\n"
    + "</html>";
    
    final HtmlPage page = loadPage(content);
    
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    
    assertNotNull(jobManager);
    
    // Not perfect, but 100 chances to start should be enough for a loaded system
    Thread.sleep(500);
    
    ((TopLevelWindow) page.getEnclosingWindow()).close();
    
    // 100 chances to stop
    jobManager.waitForJobs(500);
    
    // expecting the supplied ap.org script to print out a number of references
    // to a service ending with Image.svc
    assertTrue(page.asXml().contains("mage.svc"));
    
    }
    

    Thanks again for your help. Let me know if there is any you might need from me.

    Matt Walters

     
  • Matt

    Matt - 2011-03-27

    Any luck looking into the issue?

    Let me know if you need any additional info from me.

    Thanks,

     
  • RBRi

    RBRi - 2012-11-04
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,5 +1,6 @@
    -The following test case works in Chrome, Firefox, and Internet Explorer, but htmlunit does not correctly load all javascript and results in an incorrect dom. Any javascript following the spurious </script> tag is not executed.  
    +The following test case works in Chrome, Firefox, and Internet Explorer, but htmlunit does not correctly load all javascript and results in an incorrect dom. Any javascript following the spurious &lt;/script> tag is not executed.  
    
    +~~~~~~
          /**
    
          * @throws Exception if the test fails
          */
    @@ -48,3 +49,4 @@
             Assert.assertEquals("No new alerts should have happened",
     finalValue, collectedAlerts.size());
         }
    +~~~~~~
    
    • milestone: 2.8 --> 2.10
     

Log in to post a comment.