[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Why embed a fake comment inside an element?
On Fri, 2021-02-19 at 19:25 +0000, Roger L Costello wrote: > Hi Folks, > > I am processing a bunch of XHTML documents. Some XHTML documents > contain things like this: > > <style> > <![CDATA[ > <!-- > @font-face > {font-family:"Cambria Math"; > panose-1:2 4 5 3 5 4 6 3 2 4;} > --> > ]]> > </style> > > [...] > Question: Why would someone would do this? Is there a benefit to > embedding a fake comment inside an element? Short answer, to make sure fragments of script or CSS aren't shown to users but are still interpreted. Early Web browsers would display the contents of style elements because they didn't know about them, and HTML 2 said that the first unknown element ended the HTML HEAD. So you had to hide CSS in the document from browsers that didn't know about it. THe usual way to do that was to put it in an HTML comment, <style type="text/css"><!-- CSS rules here --></style> At the same time something similar happened with the script element - rather than escape every < sign, you could write <script type="text/JavaScript"><![CDATA[ for (i = 0; i < 10; i++) { for (j = 0; j < 10; i++) { if (j < i && randomColours[data[j]]>0) { break; } } } ]]></script> (actually this example didn't work and still doesn't, and is one of several reasons why CDATA sections were not the right answer, but that's what people used). Unfortunately, script elements would also end the HEAD, and people ended up using a mix of CDATA sections and comments, and you see things like <script><!-- /* <![CDATA[ */ stuff here /* ]]> */ --></script> in a confused attempt to hide the script from older browsers. So that's likely what's going on here. With XHTML people had to go one step further, and you will see, <script><!--//--><![CDATA[//>!-- . . . //--><!--!]]> There's an explaation of this here [1] although incorrectly calls <!...> an HTM processing instruction. [1] https://www.sitepoint.com/community/t/cdata-comment/2072/7 -- Liam Quin, https://www.delightfulcomputing.com/ Available for XML/Document/Information Architecture/XSLT/ XSL/XQuery/Web/Text Processing/A11Y training, work & consulting. Barefoot Web-slave, antique illustrations: http://www.fromoldbooks.org
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] |
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|