views:

110

answers:

3

So I recently discovered that I could use <>...</> tags in javascript in Firefox, which is handy when defining blocks of HTML or CSS.

GM_addStyle(<><![CDATA[
  .page { display: block }
  /* ... */
  td { vertical-align: top }
]]></>);
//...
div.innerHTML = <><![CDATA[
  <table class="section">
    <!-- ... -->
  </table>
]]></>;

But I'm not exactly sure what's going on, and I like understanding the syntax that I'm using. What exactly does <>...</> return? I noticed that the escaping works better when I enclose the contents in <![CDATA[...]]>, so what's happening there? Is this Firefox only, or cross-browser?

I tried to look this up online, but ran into the normal google/symbol issue. Plus, most of the results for google CDATA javascript didn't seem relevant.

+3  A: 

I believe the empty tags are just a way of writing a root element so that you have something in which to wrap a blob of XML. It's saying "Interpret the children of this root element as XML" and the single child in your case is saying "Interpret this child as a CDATA block."

Robusto
So I can write random XML as a value in Javascript? That seems odd.
rampion
Surprised me, too, when I first saw it. But that's how it works in Flex/Actionscript, and I don't think it's entirely coincidental. Caveat: I said "I believe" because I can't state this definitively.
Robusto
Looks like you're right: ["E4X introduces a native XML object to the JavaScript language, and adds syntax for embedding literal XML documents in JavaScript code."](https://developer.mozilla.org/En/E4X/Processing_XML_with_E4X)
rampion
@rampion: Great, so you use a feature that Firefox supports... what about the other browsers, like IE, Chrome/Safari, and Opera?
R. Bemrose
I don't think some browsers caught on it yet, unless it uses the Gecko Javascript engine, SpiderMonkey (http://en.wikipedia.org/wiki/ECMAScript_for_XML)
The Elite Gentleman
No. You can just drop in a CDATA block. There's no reason to use `<>`
Eli Grey
@R. Bemrose: Well, most of my javascript is GM-only, so I'm ok with it being FF-only. And one of my original questions was whether it was FF-only, so that I'd know in which contexts it was available.
rampion
@Elijah Grey: Yes there is: ["For backwards compatibility, E4X defaults to ignoring comments and CDATA sections. You can add an e4x=1 argument to your <script> tag to disable this restriction"](https://developer.mozilla.org/En/E4X/Processing_XML_with_E4X#Compatibility_issues)
rampion
+2  A: 

There's no reason to use an XMLList literal (<>...</>) with only one child, as it's handled as a single XML item anyways. Why not use just <![CDATA[...]]>? Also, <![CDATA[...]]> just returns an XML text node (<![CDATA[]]>.nodeKind() === "text").

This is all part of E4X, which is implemented by ActionScript 3 and both JavaScript engines.

Eli Grey
Hmmm... just `<![CDATA[]]>` was a syntax error in FF3.6 (just plugging it into the Error Console). I'm not sure how it would evaluate in a Greasemonkey script, but the mozilla docs (https://developer.mozilla.org/En/E4X/Processing_XML_with_E4X) say you need to set a special attribute to prevent <![CDATA[...]]> from being treated specially in a normal <source> tag.
rampion
This is because you are evaluating JavaScript 1.5, not 1.6 in the error console. CDATA problems will not occur in a greasemonkey script as it's not an inline HTML script. Add `;e4x=1` to the end of the script's `type`. Or better yet, use this shell which lets you use E4X: http://code.eligrey.com/shell/shell.html
Eli Grey
+1  A: 

As Elijah said, it's E4X syntax that won't work anywhere but Mozilla. You don't appear to be using it for anything to do with XML, just relying on the implicit toString method of the XML object being the same as the original markup. ECMA-357 (the E4X spec) doesn't specify the exact parse and serialisation rules for XML, so it doesn't guarantee eg. to remove the <![CDATA[ markers for you. IMO relying on this even just on Firefox is questionable.

In any case it doesn't really solve the problems of escaping content for use inside a script block... in particular, the </ sequence is still invalid in HTML4, the whole lot is invalid in XHTML, and you would still have to worry about the sequences </script and ]]> in the content. So you haven't really gained a lot... at best, you've got yourself a multi-line string in external scripts, at the expense of support for all the other browsers. I don't think it's really worth it.

bobince