Menu ▾ ▴

#1324 Open HTML comments not auto-closed in script blocks

2.9
open
None
5
2014-10-16
2011-08-30
No

Quite often I come across JS code snippets which are embedded in HTML comments:

<script type="text/javascript"><!--
// some JS code
//--></script>

Now and then web developers forget to add the closing comment marker:

<script type="text/javascript"><!--
// some JS code
</script>

Firefox etc. seems to implicitly close the comment at the end of the script block. What happens when run with HtmlUnit depends on the content that follows. Imagine there is another comment somewhere below the script block:

<script type="text/javascript"><!--
// some JS code
</script>
<h1>some text</h1>
<!-- some comment -->

In this case, the script block is extended up to the end of the page and then auto-closed. Usually, this results in JS errors later on, as the HTML code that follows will be part of the script body.

Interestingly, if there is no such comment below the script block, everything works as smooth as in Firefox!?

Since JS errors do not result in exceptions by default, such a missing comment marker can be hard to spot. It would be great if HtmlUnit could auto-close open comments in all cases.

Discussion

  • Joerg Werner

    Joerg Werner - 2011-08-30

    test case

     
  • Marc Guillemot

    Marc Guillemot - 2011-09-21

    Note that IE (6-8) isn't lenient for this case.

     
  • Marc Guillemot

    Marc Guillemot - 2011-09-21

    Unit test added (as NotYetImplemented): HTMLParser2Test.unclosedCommentsInScript

     
  • RBRi

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

    Diff:

    --- old
    +++ new
    @@ -1,22 +1,22 @@
     Quite often I come across JS code snippets which are embedded in HTML comments:
    
    -<script type="text/javascript"><!--
    -// some JS code
    -//--></script>
    
    +    <script type="text/javascript"><!--
    +    // some JS code
    +    //--></script>
    
     Now and then web developers forget to add the closing comment marker:
    
    -<script type="text/javascript"><!--
    -// some JS code
    -</script>
    
    +    <script type="text/javascript"><!--
    +    // some JS code
    +    </script>
    
     Firefox etc. seems to implicitly close the comment at the end of the script block. What happens when run with HtmlUnit depends on the content that follows. Imagine there is another comment somewhere below the script block:  
    
    -<script type="text/javascript"><!--
    -// some JS code
    -</script>
    -<h1>some text</h1>
    -<!-- some comment -->
    
    +    <script type="text/javascript"><!--
    +    // some JS code
    +    </script>
    +    <h1>some text</h1>
    +    <!-- some comment -->
    
     In this case, the script block is extended up to the end of the page and then auto-closed. Usually, this results in JS errors later on, as the HTML code that follows will be part of the script body. 
    
     

Log in to post a comment.