hi everyone!

in case anyone's interested, i just came across the following
interesting problem:

in a <script/> section, no other xml elements are allowed. therefore,
the following is invalid xhtml:

(1)
<script type="text/javascript">
  var myXhtmlString = "<strong>Yabbadabbadoo!</strong>";
</script>

lenya's editors will rightfully complain when you enter such code.

the problem is, the obvious workaround doesn't work:

(2)
<script type="text/javascript">
  var myXhtmlString = "&lt;strong&gt;Yabbadabbadoo!&lt;/strong&gt;";
</script>

because whatever processes your string later will not re-convert them
into elements, so you get literal &foo; strings in the output.

when using javascript outside of an XML CMS, the following is good practice:

(3)
<script type="text/javascript">
<![CDATA[
  var myXhtmlString = "<strong>Yabbadabbadoo!</strong>";
]]>
</script>

this is correct. but unfortunately, our editors will evaluate the CDATA
section and transform it into (2) when saving. which does not work.
but it's not a bug, because from an XML POV both are syntactically
equivalent :(

my usual last resort (apart from moving javascript into an external file
whenever possible [which means we should really introduce a js
doctype]), was using octal escapes, but these have been deprecated as of
javascript 1.5.

iiuc, the current correct way of weaseling around this problem is

(4)
<script type="text/javascript">
  var myXhtmlString = decodeURIComponent(
    "%3cstrong%3eYabbadabbadoo!%3c/strong%3e"
  );
</script>

where % denotes a hexadecimal character encoding and 3c and 3e are "<"
and ">" respectively.

comments/improvements appreciated.


best,

jörn



-- 
Jörn Nettingsmeier

"One of my most productive days was throwing away 1000 lines of code."
  - Ken Thompson.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to