stange no-escape parsing

  • Thread starter Vincent van Beveren
  • Start date
V

Vincent van Beveren

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.getElementById('space').
appendChild(document.createTextNode('<'));

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="JavaScript">
function doItAgain() {
document.getElementById('space').
appendChild(document.createTextNode('&lt;'));
}
</script>
</head><body onLoad="document.getElementById('space').
appendChild(document.createTextNode('&lt;'));doItAgain();">

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

</body></html>
 
M

Michael Winter

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

document.getElementById('space').
appendChild(document.createTextNode('&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.

[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.createTextNode('<')" 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 "<\/".
 
V

Vincent van Beveren

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 said:
I try to insert a special character into my HTML using the DOM.
I do this by the following piece of code:

document.getElementById('space').
appendChild(document.createTextNode('&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.


[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.createTextNode('<')" 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 "<\/".
 
M

Michael Winter

On Mon, 19 Apr 2004 15:56:06 +0200, Vincent van Beveren

Please don't top-post.
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.

[snip]

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

document.createTextNode( '\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
 
V

Vincent van Beveren

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

G Roydor

Incompréhensible !

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:
 
L

Lasse Reichstein Nielsen

Vincent van Beveren said:
I do this by the following piece of code:

document.getElementById('space').
appendChild(document.createTextNode('&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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top