473,761 Members | 10,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HTML and CDATA produced by Rails

Hi,

I am experimenting with some of the Ruby on Rails JavaScript generators
and see something I haven't before. Maybe it is worthwhile?

In the page below the script is enclosed in

//<![CDATA[
//]]>

Is this trick grounded in any real information about HTML vs XHTML? I
think what they are trying to achieve is a way to generate a script
block that can be used in either HTML or XHTML. Have they managed that
goal? It looks fine for HTML but will the double slashes be ok in
XHTML?

Thanks,
Peter
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Fork JavaScript</title>
</head>

<body>

<script type="text/javascript">
//<![CDATA[
new Form.Element.Ob server('search' , 0.5, function(elemen t, value) {new
Ajax.Updater('r esults', '/cont/search', {asynchronous:t rue,
evalScripts:tru e, parameters:valu e})})
//]]>
</script>
</div>

</body>
</html>

Dec 10 '06 #1
12 4011

Peter Michaux wrote:
//<![CDATA[
//]]>

Is this trick grounded in any real information about HTML vs XHTML? I
think what they are trying to achieve is a way to generate a script
block that can be used in either HTML or XHTML. Have they managed that
goal? It looks fine for HTML but will the double slashes be ok in
XHTML?
Yes, this is not anything unique to Rails. This is SGML for "ignore
whatever comes between these marks". The leading slashes are OK
because they have no significance in SGML syntax. This is supposed to
prevent the parser from misreading any characters that would otherwise
be read as markup, such as angle brackets (and <) which have a
different meaning in javascript:

http://www.w3.org/TR/REC-xml/#sec-cdata-sect

David

Dec 10 '06 #2
Peter Michaux wrote:
Hi,

I am experimenting with some of the Ruby on Rails JavaScript
generators and see something I haven't before. Maybe it is worthwhile?

In the page below the script is enclosed in

//<![CDATA[
//]]>

Is this trick grounded in any real information about HTML vs XHTML?
Not really, It is a cargo cult/mystical incantation thing. In HTML the
contents of SCRIPT elements are CDATA anyway and the two lines posted
are just end of line comments. In XHTML the contents of SCRIPT elements
are PCDATA, but there is no need to attempt to comment out the CDATA
block mark-up. And as it is largely impractical to write non-trivial
scripts that will function with both HTML DOMs and XHTML DOMs there is
no need for a construct to 'normalise' SCRIPT element contents to CDATA
for use with both types of DOM.
I think what they are trying to achieve is a way to
generate a script block that can be used in either
HTML or XHTML.
They may think that they are trying to do that, but probably without any
understanding of the differences between an XHTML DOM and an HTML DOM,
and so no appreciation of the worthlessness of the exercise.
Have they managed that goal?
Yes, but the scripts being inserted into that construct are extremely
unlikely to work if the mark-up that contains them is ever interpreted
as XHTML and an XHTML DOM exposed to the script. Which renders the
effort moot.
It looks fine for HTML but will the double slashes be
ok in XHTML?
They will be fine, it is the scripts they contain that will likely go
belly-up if the documents are ever interpreted as XHTML by a web
browser.

Richard.
Dec 10 '06 #3
David Golightly wrote:
Peter Michaux wrote:
>//<![CDATA[
//]]>

Is this trick grounded in any real information about HTML vs
XHTML? I think what they are trying to achieve is a way to
generate a script block that can be used in either HTML or
XHTML. Have they managed that goal? It looks fine for HTML
but will the double slashes be ok in XHTML?

Yes, this is not anything unique to Rails.
That is certainly true. Rails is not the only system that includes
spurious constructs in mark-up for no particularly good reason.
This is SGML for "ignore
whatever comes between these marks".
Nonsense. The <![CDATA[ ... ]]mark up in XML is adopted directly form
SGML and has exactly the same meaning in both. In the context of the
contents of an HTML SCRIPT element, which is already CDATA, the
construct has no significance as such contents are not parsed for
mark-up, beyond identifying where they end.
The leading slashes are OK
because they have no significance in SGML syntax.
No, they are OK because the contents of an HTML SCRIPT element are CDATA
and so not mark-up at all, and the contents of an XHTML script element
are PCDAT and so the slashes outside the <![CDATA[ and ]]delimiters
will be parsed and the (unchanged) results used as part of the script
source code, with the unparsed contents of the CDATA block inserted in
place of the CDATA delimiters.
This is supposed to prevent the parser from misreading any
characters that would otherwise be read as markup, such as
angle brackets (and <) which have a different meaning in
javascript:
And is worthless in an HTML document where no such misinterpretati on is
possible, while in an XHTML document there is no need for the end of
line comment syntax.

Richard.
Dec 10 '06 #4

Richard Cornford wrote:
Peter Michaux wrote:
Hi,

I am experimenting with some of the Ruby on Rails JavaScript
generators and see something I haven't before. Maybe it is worthwhile?

In the page below the script is enclosed in

//<![CDATA[
//]]>

Is this trick grounded in any real information about HTML vs XHTML?

Not really, It is a cargo cult/mystical incantation thing. In HTML the
contents of SCRIPT elements are CDATA anyway and the two lines posted
are just end of line comments. In XHTML the contents of SCRIPT elements
are PCDATA, but there is no need to attempt to comment out the CDATA
block mark-up. And as it is largely impractical to write non-trivial
scripts that will function with both HTML DOMs and XHTML DOMs there is
no need for a construct to 'normalise' SCRIPT element contents to CDATA
for use with both types of DOM.
I think what they are trying to achieve is a way to
generate a script block that can be used in either
HTML or XHTML.

They may think that they are trying to do that, but probably without any
understanding of the differences between an XHTML DOM and an HTML DOM,
and so no appreciation of the worthlessness of the exercise.
I haven't read anything about how a script would have to be written
differently depending if it is in an HTML document or an XHTML document
(being correctly interpreted as XHTML).

Do you have a tiny example or pointer to some information about this?

Thanks,
Peter

Dec 10 '06 #5

Richard Cornford wrote:
David Golightly wrote:
This is SGML for "ignore
whatever comes between these marks".

Nonsense. The <![CDATA[ ... ]]mark up in XML is adopted directly form
SGML and has exactly the same meaning in both. In the context of the
contents of an HTML SCRIPT element, which is already CDATA, the
construct has no significance as such contents are not parsed for
mark-up, beyond identifying where they end.
Richard - Read once more what I wrote, then what you wrote, then cool
down for once, then tell me how they disagree with each other.

Dec 10 '06 #6

Richard Cornford wrote:
David Golightly wrote:
Yes, this is not anything unique to Rails.
The leading slashes are OK
because they have no significance in SGML syntax.

No, they are OK because the contents of an HTML SCRIPT element are CDATA
and so not mark-up at all, and the contents of an XHTML script element
are PCDAT and so the slashes outside the <![CDATA[ and ]]delimiters
will be parsed and the (unchanged) results used as part of the script
source code, with the unparsed contents of the CDATA block inserted in
place of the CDATA delimiters.
That's exactly what I said. Thanks for reiterating.
>
This is supposed to prevent the parser from misreading any
characters that would otherwise be read as markup, such as
angle brackets (and <) which have a different meaning in
javascript:

And is worthless in an HTML document where no such misinterpretati on is
possible, while in an XHTML document there is no need for the end of
line comment syntax.
Sorry Richard, you're off base on this one. Case in point: closing
tags in script strings. Consider the following code:

<script type="text/javascript">
//<!--
var myScriptTag = '<script type="text/javascript
src="foo.js"></script>';
//-->
</script>

As we all know, older HTML parses will barf at the literal closing
</scripttag in the string. That's why you see strings broken up
like:

'<'+'/script' etc.

so browsers don't mistake this for an HTML element and parse it
mistakenly. (See Flanagan, javascript: TDG, 5th Edition, page 247).
With a CDATA section explicitly defined, this problem doesn't occur and
coders don't have to worry about this mistake. This alone, and the
fact that the CDATA tag doesn't add any other drawbacks over the
conventional <!-- --comment, make this a preferred approach for
commenting out script sections.

Dec 10 '06 #7

Peter Michaux wrote:
Hi,

I am experimenting with some of the Ruby on Rails JavaScript generators
and see something I haven't before. Maybe it is worthwhile?

In the page below the script is enclosed in

//<![CDATA[
//]]>

Is this trick grounded in any real information about HTML vs XHTML?
There is a possibility that if a parser encounters "</" or "</script>"
within a script it may be interpreted as an end of script element tag,
and anything after may be thought of as markup.

The simple solution is to use external scripts. If the script is
included in the page, wherever "</" might be encountered either:

1. separate the "<" and "/" with a space: "< /"
2. use a backslash to quote it: "<\/"

as appropriate. I can't think of a case to use the former as I don't
think it's legal ECMAScript syntax (but it might occur in some other
script language), the latter can be used where HTML is included in the
script, say as innerHTML or with document.write.

ISTM much better to deal with those cases as they occur, rather than
with the blanket inclusion of comment delimiters which are otherwise
pointless.
--
Rob

Dec 10 '06 #8
Peter Michaux wrote:
Richard Cornford wrote:
>Peter Michaux wrote:
<snip>
>>In the page below the script is enclosed in

//<![CDATA[
//]]>
<snip>
>>I think what they are trying to achieve is a way to
generate a script block that can be used in either
HTML or XHTML.

They may think that they are trying to do that, but probably
without any understanding of the differences between an XHTML
DOM and an HTML DOM, and so no appreciation of the
worthlessnes s of the exercise.

I haven't read anything about how a script would have to be
written differently depending if it is in an HTML document
or an XHTML document (being correctly interpreted as XHTML).
Why would you expect to? There is no commercial application for
client-side scripting of XHTML for as long as IE is not capable of
interpreting XHTML and creating an XHTML DOM to be scripted.
Do you have a tiny example or pointer to some information
about this?
Examples are posted to the group, maybe not frequently but regularly
enough to make it obvious that most who wander into XHTML DOM scripting
are not expecting what they find. An archive search (probably restricted
to the last 5 years or so) with keywords corresponding to the namespace
qualified DOM menthols (such as - createElementNS - or -
setAttributeNS -) should turn a number of real life examples (as
namespace qualified methods need to be used with XHTML DOMs).

Richard.
Dec 10 '06 #9
David Golightly wrote:
Richard Cornford wrote:
>David Golightly wrote:
>>This is SGML for "ignore
whatever comes between these marks".

Nonsense. The <![CDATA[ ... ]]mark up in XML is adopted
directly form SGML and has exactly the same meaning in both.
In the context of the contents of an HTML SCRIPT element,
which is already CDATA, the construct has no significance as
such contents are not parsed for mark-up, beyond identifying
where they end.

Richard - Read once more what I wrote,
I did read what you wrote, it was, "ignore whatever comes between theses
marks", and that is very much not true. SGML includes the marked section
keyword IGNORE so that if it does need to "ignore whatever comes between
theses marks" that facility exists. CDATA is not ignored it is just not
parsed (beyond scanning for its closing delimiter).
then what you wrote, then cool
down for once, then tell me how they disagree with
each other.
The CDATA marked section is not ignored in XHTML as its contents must
contribute to the source characters that will be interpreted as script,
and in HTML there is no CDATA marked section within the SCRIPT element
as the SCRIPT element's contents are already CDATA and so the
<![CDATA[ and ]]delimiters are just sequences of text characters. That
is; in XHTML they are not ignored and in HTML they never existed as
mark-up and so could not mark their contents as to be ignored.

Richard.
Dec 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6577
by: Isaac Gerg | last post by:
I have a server running PHP on linux. For a website, I want to have the webdocuments stored in XML, then parsed by PHP code to add the appropriate titles, headers, menus, etc. So, I want to embed some HTML into XML. Can I do this? If so, what functions do I use to parse this? Below is an example XML doc. <xml> <title>Document Title</title> <menu level="2">
2
5117
by: Chris | last post by:
Wondering if anyone can help me... I have a text node that has been HTML encoded. The text is has CDATA tags around it. The problem is I can't seem to decode the text. When the CDATA is gone, I can decode it fine. When it's not HTML encoded, then obviously I won't have to decode it. But when it's encoded and has CDATA tags I can't seem to do anything with it! I can't change the XML so can anyone think of a way I can work with this text...
4
8406
by: VK | last post by:
09/30/03 Phil Powell posted his "Radio buttons do not appear checked" question. This question led to a long discussion about the naming rules applying to variables, objects, methods and properties in JavaScript/JScript and HTML/XML elements. Without trying to get famous :-) but thinking it would be interesting to others I decided to post the following summary: 1. Variable names in JavaScript/JScript
10
1932
by: Andrew Poulos | last post by:
While this works on IE 6: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>aiff</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script language="JavaScript" type="text/javascript"> //<!]>
7
9280
by: AndrewMBaldwin | last post by:
So I am quite upset that after working for a few hours on getting an XML file format and XSL file that formats the XML data appropriatly, only to find that if you store HTML code in your XML file (even in a CDATA block), after the XML file is rendered, the HTML that was stored in the XML file is not rendered, essentially put into the page as if it had <pre> tags around it. I know that someone is going to yell at me and say that the XML...
1
4834
by: Mike | last post by:
I am consuming a web service hosted by one of our clients. One of the string properties of the object I am creating to pass to their web service is called CustomerName. The XML they re receiving I just a standatd xml elemnt: <CustomerName>Joe Friday</CustomerName> The client has told me that the customer name, Joe Firday, needs to have CDATA tags around it:
3
2487
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my code below it replace the <, and > with &lt; and &gt;. Thus it replaces IBM with: &lt;acronym title="International Business Machines"&gt;IBM&lt;/acronym&gt;
6
6861
by: =?Utf-8?B?QmlsbEF0V29yaw==?= | last post by:
Hi, I'm defining a report layout using an XML document, plugging data values into fields before outputting the whole doc as an HTML page. I wrap each of the data fields in CDATA section (in case the data contains reserved chars like "&"). Currently, I perform a simple search-and-replace to strip out the CDATA tags before outputting the document. It works but it's not as elegant or reliable as I would wish. Is there a better way of...
10
7431
dj12345
by: dj12345 | last post by:
Hi, (Asp.net + Ajax) I am creating a page which will fetch data from server without postbak of a page.. I have 2 controls on this page TextBox and Lable. I have assigned TextBoxWatermark extender to TextBox. Inside a Update panel i have Lable.. and the update panel refreshes each 3 seconds and it fetches data and displays it. But my problem is whenever the data is fetched from Server the textbox watermark flickers. i dont know why it...
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.