HTMLParser chokes on bad end tag in comment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rene Pijlman

    HTMLParser chokes on bad end tag in comment

    The code below results in an exception (Python 2.4.2):

    HTMLParser.HTML ParseError: bad end tag: "</foo' + 'bar>", at line 4,
    column 6

    Should it? The end tag it chokes on is in comment, isn't it?

    import HTMLParser
    HTMLParser.HTML Parser().feed(" ""
    <html><head><ti tle></title></head><body><scr ipt>
    <!--
    x = '</foo' + 'bar>'
    // -->
    </script></body></html>
    """)

    --
    René Pijlman
  • Fredrik Lundh

    #2
    Re: HTMLParser chokes on bad end tag in comment

    Rene Pijlman wrote:
    [color=blue]
    > The code below results in an exception (Python 2.4.2):
    >
    > HTMLParser.HTML ParseError: bad end tag: "</foo' + 'bar>", at line 4,
    > column 6
    >
    > Should it? The end tag it chokes on is in comment, isn't it?[/color]

    no. STYLE and SCRIPT elements contain character data, not parsed
    character data, so comments are treated as characters, and the first
    "</" ends the element.

    if you have broken documents, you can tweak this by setting the
    CDATA_CONTENT_E LEMENTS parser attribute before you start parsing.

    </F>

    Comment

    • Rene Pijlman

      #3
      Re: HTMLParser chokes on bad end tag in comment

      Fredrik Lundh:[color=blue]
      >Rene Pijlman:
      >[end tag in html comment in script element]
      >The end tag it chokes on is in comment, isn't it?
      >
      >no. STYLE and SCRIPT elements contain character data, not parsed
      >character data, so comments are treated as characters, and the first
      >"</" ends the element.[/color]

      Ah, I see. I'll report the problem to the application that's generating
      this broken code (vBulletin forum)...
      [color=blue]
      >if you have broken documents, you can tweak this by setting the
      >CDATA_CONTENT_ ELEMENTS parser attribute before you start parsing.[/color]

      .... and in the mean time that's a good workaround.

      Thank you very much Fredrik.

      --
      René Pijlman

      Comment

      • Miki

        #4
        Re: HTMLParser chokes on bad end tag in comment

        Hello Rene,

        You can also check out BeautifulSoup
        (http://www.crummy.com/software/BeautifulSoup/) which is less strict
        than the regular HTML parser.

        HTH,
        Miki
        If it won't be simple, it simply won't be. [Hire me, source code]


        Comment

        • Rene Pijlman

          #5
          Re: HTMLParser chokes on bad end tag in comment

          Miki:[color=blue]
          >You can also check out BeautifulSoup
          >(http://www.crummy.com/software/BeautifulSoup/) which is less strict
          >than the regular HTML parser.[/color]

          Yes, thanks. Ik this case it was my sitechecker which checks for syntax
          and broken links, so it was supposed to find the syntax error.
          BeautifulSoup is not very well suited for validators :-)

          --
          René Pijlman

          Comment

          • Edward Elliott

            #6
            Re: HTMLParser chokes on bad end tag in comment

            Fredrik Lundh wrote:
            [color=blue][color=green]
            >> Should it? The end tag it chokes on is in comment, isn't it?[/color]
            >
            > no. STYLE and SCRIPT elements contain character data, not parsed
            > character data, so comments are treated as characters, and the first
            > "</" ends the element.[/color]

            Rather than take your word for it, I checked the W3C HTML4 DTD and found
            this:



            Element content

            When script or style data is the content of an element (SCRIPT and STYLE),
            the data begins immediately after the element start tag and ends at the
            first ETAGO ("</") delimiter followed by a name start character ([a-zA-Z]);
            note that this may not be the element's end tag. Authors should therefore
            escape "</" within the content. Escape mechanisms are specific to each
            scripting or style sheet language.

            ILLEGAL EXAMPLE:
            The following script data incorrectly contains a "</" sequence (as part of
            "</EM>") before the SCRIPT end tag:

            <SCRIPT type="text/javascript">
            document.write ("<EM>This won't work</EM>")
            </SCRIPT>

            In JavaScript, this code can be expressed legally by hiding the ETAGO
            delimiter before an SGML name start character:

            <SCRIPT type="text/javascript">
            document.write ("<EM>This will work<\/EM>")
            </SCRIPT>


            Guess you learn something new every day. Too bad there's so much illegal
            code in the wild. :(

            --
            Edward Elliott
            UC Berkeley School of Law (Boalt Hall)
            complangpython at eddeye dot net

            Comment

            • Fredrik Lundh

              #7
              Re: HTMLParser chokes on bad end tag in comment

              Edward Elliott wrote:
              [color=blue]
              > Guess you learn something new every day. Too bad there's so much illegal
              > code in the wild. :([/color]

              if more people learned something new every day, the wild would look a
              lot different.

              </F>


              Comment

              Working...