XML parsing and HTML comments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Colin McKinnon

    #1

    XML parsing and HTML comments

    Hi all,

    I find myaelf writing a template system (yeah, I know - but there is a
    reason I'm not using an existing one). So I'm trying to parse xhtml using
    the builtin expat parser. Mostly it works fine, however it ignores anything
    that looks like an HTML comment. This is a bit of a problem as I see a lot
    of code written like:

    <script type='text/javascript>
    <!--
    alert("hello world');
    // -->
    </script>
    <style type="text/css">
    <!--
    ..style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 9px;
    }
    -->
    </style>

    Now obviously the browser is seeing the stuff inside '<!--' ...'-->' but
    expat doesn't. I tried adding a non-parsed handler, but still can't see it.

    Anybody fixed this?

    C.
  • Andy Hassall

    #2
    Re: XML parsing and HTML comments

    On Tue, 13 Sep 2005 13:27:18 +0100, Colin McKinnon
    <colin.deleteth is@andthis.mms3 .com> wrote:
    [color=blue]
    >I find myaelf writing a template system (yeah, I know - but there is a
    >reason I'm not using an existing one). So I'm trying to parse xhtml using
    >the builtin expat parser. Mostly it works fine, however it ignores anything
    >that looks like an HTML comment. This is a bit of a problem as I see a lot
    >of code written like:
    >
    ><script type='text/javascript>
    ><!--
    > alert("hello world');
    >// -->
    ></script>
    ><style type="text/css">
    ><!--
    >.style1 {
    > font-family: Verdana, Arial, Helvetica, sans-serif;
    > font-size: 9px;
    >}
    >-->
    ></style>
    >
    >Now obviously the browser is seeing the stuff inside '<!--' ...'-->' but
    >expat doesn't. I tried adding a non-parsed handler, but still can't see it.
    >
    >Anybody fixed this?[/color]

    expat (the XML parser used in these functions) has support for adding comment
    handlers, but that doesn't appear to be hooked into the PHP extension, so you
    can't get at that functionality without patching the source of the extension.

    If you look in the PHP source, under ext/xml/xml.c you see:

    /* Short-term TODO list:
    * - Implement XML_ExternalEnt ityParserCreate ()
    * - XML_SetCommentH andler
    * - XML_SetCdataSec tionHandler
    * - XML_SetParamEnt ityParsing
    */

    The second one being what you want.

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Colin McKinnon

      #3
      Re: XML parsing and HTML comments

      Andy Hassall wrote:
      [color=blue]
      > On Tue, 13 Sep 2005 13:27:18 +0100, Colin McKinnon
      > <colin.deleteth is@andthis.mms3 .com> wrote:
      >[color=green]
      >> Mostly it works fine, however it ignores
      >>anything that looks like an HTML comment. This is a bit of a problem as I
      >>see a lot of code written like:
      >>
      >><script type='text/javascript>
      >><!--[/color]
      >
      > expat (the XML parser used in these functions) has support for adding
      > comment
      > handlers, but that doesn't appear to be hooked into the PHP extension, so
      > you can't get at that functionality without patching the source of the
      > extension.
      >[/color]

      erk.

      Thanks Andy. At least I know I'm not doing something stupid.

      For software I'm planning to release, patching the source isn't an ideal
      solution. I managed to implement a workaround by running this on the XML
      first:

      $xml=str_replac e('<!--', '<![CDATA[<!--', $xml);
      $xml=str_replac e('-->', '-->]]>', $xml);

      (again not ideal, but hopefully less painful than recompiling/maintaining
      expat)

      C.

      Comment

      Working...