[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?

  • From: "Liam R. E. Quin" <liam@fromoldbooks.org>
  • To: Roger L Costello <costello@mitre.org>, "xml-dev@l..." <xml-dev@l...>
  • Date: Fri, 19 Feb 2021 19:07:06 -0500

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!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.