stange no-escape parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vincent van Beveren

    #1

    stange no-escape parsing

    Hey everyone,

    I try to insert a special character into my HTML using the DOM.
    I do this by the following piece of code:

    document.getEle mentById('space ').
    appendChild(doc ument.createTex tNode('<'));

    Can anyone tell me why this does parse correctly if I use it in
    the onLoad, but not in the function that I call directly from
    the onload. You can run the code below in Netscape 7.1 or IE 6, it
    gives the same result, so I'm sure I forgot something, but I can't
    figure out what.

    It prints:

    This should be two smaller-then chars: <&lt;

    It should print:

    This should be two smaller-then chars: <<;

    Thanks,
    Vincent

    <html><head>
    <script language="JavaS cript">
    function doItAgain() {
    document.getEle mentById('space ').
    appendChild(doc ument.createTex tNode('&lt;'));
    }
    </script>
    </head><body onLoad="documen t.getElementByI d('space').
    appendChild(doc ument.createTex tNode('&lt;')); doItAgain();">

    This should be two smaller-then chars: <span id="space"></span>

    </body></html>


  • Michael Winter

    #2
    Re: stange no-escape parsing

    On Mon, 19 Apr 2004 09:34:17 +0200, Vincent van Beveren
    <vincent@provid ent.remove.this .nl> wrote:
    [color=blue]
    > I try to insert a special character into my HTML using the DOM.
    > I do this by the following piece of code:
    >
    > document.getEle mentById('space ').
    > appendChild(doc ument.createTex tNode('&lt;'));
    >
    > Can anyone tell me why this does parse correctly if I use it in
    > the onLoad, but not in the function that I call directly from
    > the onload. You can run the code below in Netscape 7.1 or IE 6, it
    > gives the same result, so I'm sure I forgot something, but I can't
    > figure out what.[/color]

    [snip]

    It took me a while to think of the answer, but it's quite simple:
    character entities are treated differently in attribute values when
    compared to element content.

    Whilst the character type for onload attribute values and SCRIPT element
    content is the same, CDATA, browsers honour entity references in the
    former, but ignore them in the latter. The means that in the onload
    attribute value, you are actually performing
    "document.creat eTextNode('<')" because the entity reference is resolved
    properly.

    Whilst you must use entity references in attribute values and in text in
    most elements, it is safe to use actual characters in SCRIPT elements[1].

    This information is covered in Appendix B.3.2 - Specifying non-HTML data,
    in the HTML 4.01 Specification.

    <URL:http://www.w3.org/TR/html401/appendix/notes.html#h-B.3.2>

    Mike


    [1] This also applies to STYLE content, which is the only other element
    that can contain CDATA. In both, you must make sure that the sequence "</"
    doesn't exist. In STYLE elements, I doubt it ever will. With JavaScript,
    use "<\/".

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Vincent van Beveren

      #3
      Re: stange no-escape parsing

      Oh, right, that would explain it, thanks!

      Then I have another problem.

      The example was only meant to illustrate the problem, now suppose one
      would like to insert mutliple spaces before an element. (For a tree
      view) Normally, I would add &nbsp; infront of that element, but for this
      script, I need to do it on-the-fly, using DOM. I'v been thinking of
      using an invisible 1x1 pixel image for the spacing... but I think the
      &nbsp; solution would be less ugly.


      Thanks,
      Vincent



      Michael Winter wrote:
      [color=blue]
      > On Mon, 19 Apr 2004 09:34:17 +0200, Vincent van Beveren
      > <vincent@provid ent.remove.this .nl> wrote:
      >[color=green]
      >> I try to insert a special character into my HTML using the DOM.
      >> I do this by the following piece of code:
      >>
      >> document.getEle mentById('space ').
      >> appendChild(doc ument.createTex tNode('&lt;'));
      >>
      >> Can anyone tell me why this does parse correctly if I use it in
      >> the onLoad, but not in the function that I call directly from
      >> the onload. You can run the code below in Netscape 7.1 or IE 6, it
      >> gives the same result, so I'm sure I forgot something, but I can't
      >> figure out what.[/color]
      >
      >
      > [snip]
      >
      > It took me a while to think of the answer, but it's quite simple:
      > character entities are treated differently in attribute values when
      > compared to element content.
      >
      > Whilst the character type for onload attribute values and SCRIPT element
      > content is the same, CDATA, browsers honour entity references in the
      > former, but ignore them in the latter. The means that in the onload
      > attribute value, you are actually performing
      > "document.creat eTextNode('<')" because the entity reference is resolved
      > properly.
      >
      > Whilst you must use entity references in attribute values and in text in
      > most elements, it is safe to use actual characters in SCRIPT elements[1].
      >
      > This information is covered in Appendix B.3.2 - Specifying non-HTML
      > data, in the HTML 4.01 Specification.
      >
      > <URL:http://www.w3.org/TR/html401/appendix/notes.html#h-B.3.2>
      >
      > Mike
      >
      >
      > [1] This also applies to STYLE content, which is the only other element
      > that can contain CDATA. In both, you must make sure that the sequence
      > "</" doesn't exist. In STYLE elements, I doubt it ever will. With
      > JavaScript, use "<\/".
      >[/color]

      Comment

      • Michael Winter

        #4
        Re: stange no-escape parsing

        On Mon, 19 Apr 2004 15:56:06 +0200, Vincent van Beveren
        <vincent@provid ent.remove.this .nl> wrote:

        Please don't top-post.
        [color=blue]
        > The example was only meant to illustrate the problem, now suppose one
        > would like to insert mutliple spaces before an element. (For a tree
        > view) Normally, I would add &nbsp; infront of that element, but for this
        > script, I need to do it on-the-fly, using DOM. I'v been thinking of
        > using an invisible 1x1 pixel image for the spacing... but I think the
        > &nbsp; solution would be less ugly.[/color]

        [snip]

        All entity references resolve to a particular character. The HTML
        specification defines &nbsp; as 0xa0 (or dec. 160). You could write

        document.create TextNode( '\xa0\xa0some text' );

        to create a text node that has two leading spaces, but I'd recommend that
        you styled the tree node with the CSS margin property. Indenting with
        &nbsp; is just as ugly as using spacing images.

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Vincent van Beveren

          #5
          Re: stange no-escape parsing

          Oh, the ident thing is a good idea, why didn't I think of that. Thanks!

          Comment

          • G Roydor

            #6
            Re: stange no-escape parsing

            Incompréhensibl e !

            après avoir copier le texte sous Netscape 7 :
            &lt; in body devient < (après enregistrement et avant exécution)
            &lt; in function doagain est inchangé.




            Vincent van Beveren a écrit:[color=blue]
            > Hey everyone,
            >
            > I try to insert a special character into my HTML using the DOM.
            > I do this by the following piece of code:
            >
            > document.getEle mentById('space ').
            > appendChild(doc ument.createTex tNode('&lt;'));
            >
            > Can anyone tell me why this does parse correctly if I use it in
            > the onLoad, but not in the function that I call directly from
            > the onload. You can run the code below in Netscape 7.1 or IE 6, it
            > gives the same result, so I'm sure I forgot something, but I can't
            > figure out what.
            >
            > It prints:
            >
            > This should be two smaller-then chars: <&lt;
            >
            > It should print:
            >
            > This should be two smaller-then chars: <<;
            >
            > Thanks,
            > Vincent
            >
            > <html><head>
            > <script language="JavaS cript">
            > function doItAgain() {
            > document.getEle mentById('space ').
            > appendChild(doc ument.createTex tNode('&lt;'));
            > }
            > </script>
            > </head><body onLoad="documen t.getElementByI d('space').
            > appendChild(doc ument.createTex tNode('&lt;')); doItAgain();">
            >
            > This should be two smaller-then chars: <span id="space"></span>
            >
            > </body></html>
            >
            >[/color]

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: stange no-escape parsing

              Vincent van Beveren <vincent@provid ent.remove.this .nl> writes:
              [color=blue]
              > I do this by the following piece of code:
              >
              > document.getEle mentById('space ').
              > appendChild(doc ument.createTex tNode('&lt;'));
              >
              > Can anyone tell me why this does parse correctly if I use it in
              > the onLoad, but not in the function that I call directly from
              > the onload.[/color]

              The onload attribute is an HTML attribute, and entities are replaced
              when the document is parsed.

              If the function is defined in the page inside a standard script tag,
              then it is part of something parsed as CDATA, and in script tags,
              entities in CDATA are not converted (in HTML at least, in XHTML script
              tags are PCDATA and entities are converted!).

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              Working...