Re: document type does not allow element "style" here

Jürgen Schmid wrote:

> This module puts the following lines into the html:

| <script type="text/javascript">$("head").append("<style
| type='text/css'>form.rating{display:none}</style>");
| if ($.browser.safari){ $("head").find("style:last").append(" ")
| }</script>

What the validator "sees" is something like this:

| <script type="text/javascript"> (...) <style
| type='text/css'> (...) </style> (...) </script>

> The validator tells me:
> Line 51, Column 71: document type does not allow element
> "style" here.

Yes, that is correct, you cannot have other elements in a
script.  A HTML trick to get around this could be to put
the content of the script into a comment:

| <script type="text/javascript"><!--
| (...) <style type='text/css'> (...) </style> (...)
| //--></script>

For XHTML it's more convoluted, something like:

| <script type="text/javascript"><!--//--><![CDATA[//><!--
| (...) <style type='text/css'> (...) </style> (...)
| //--><!]]></script>

> I would be glad if anybody could tell me, it the script
> is incorrect or if it's a bug in the validator.

The script itself is probably fine, you just cannot
put it "as is" inline into a script-element.  

JFTR an attempt to explain the XHTML magic, but without
guarantee that I get it right:

<script type="text/javascript"><!--//--><![CDATA[//><!--
0..............................1...2.3..4........5..6...

1: At this point the script begins as far as a browser
   is concerned, and browsers know that inline scripts
   need some comment <!-- magic //--> to stay out of
   trouble wrt to HTML.

   But an XHTML browser can follow XML rules, and XML
   rules would eliminate the "script in a comment" magic.

2: // starts a Javascript comment, JS interpreters will
   ignore the rest of the line when they run the script.

3: For XML rules this terminates the HTML comment 1, an
   XHTML browser sees the following <![CDATA[ section (4)

5: Again a JS comment, I'm not sure what it does, likely
   convincing HTML browsers that the <![CDATA[//> can be
   ignored as "unsupported element".

6: Again a HTML comment, HTML browsers looking for the
   terminating </script> ignore the inline script for
   this job, same idea as in (1) after 3..5 handled XML.

//--><!]]></script>
7.8..9.A..B........

7: As far as JS is concerned this line is a comment.
8: Terminate comment started in (6).
9: Pretend that <!]]> is an "unsupported element", maybe
   <]]> without "!" would be bad enough here (untested).
A: Finish <![CDATA[ started in (4).
B: The end of the script element, closing (0), see above. 

 Frank

Received on Thursday, 14 February 2008 20:08:35 UTC