From: Matt Kruse on
In my Greasemonkey code written specifically for Firefox, I use this
"heredoc" syntax a lot:

var myBigBlob = (<><![CDATA[

... insert a bunch of free-form text here ...

]]></>).toString();

Now that Chrome offers built-in Greasemonkey support, I'd like to
support it as well, but it breaks on this syntax.

Does anyone know of a better method that will work in both browsers?

Matt Kruse
From: Thomas 'PointedEars' Lahn on
Matt Kruse wrote:

> In my Greasemonkey code written specifically for Firefox, I use this
> "heredoc" syntax a lot:
>
> var myBigBlob = (<><![CDATA[
>
> ... insert a bunch of free-form text here ...
>
> ]]></>).toString();
>
> Now that Chrome offers built-in Greasemonkey support, I'd like to
> support it as well, but it breaks on this syntax.
>
> Does anyone know of a better method that will work in both browsers?

You mean in both *ECMAScript implementations* (Mozilla.org Gecko's
SpiderMonkey/TraceMonkey, and Google Chrome's V8).

The syntax you have used to date apparently makes use of a bug in Gecko's
ECMAScript for XML (E4X, ECMA-357) implementation. V8 breaks on this
syntax because syntactically valid ECMAScript for XML is expected, and `<>'
is not well-formed XML:

,-<http://www.w3.org/TR/REC-xml/#sec-starttags>
|
| [40] STag ::= '<' Name (S Attribute)* S? '>'
| [...]
| [5] Name ::= NameStartChar (NameChar)*

(As you can see, the `Name' is not optional.)

In particular, `<>' cannot be produced by the following productions in E4X,
section 8.3:

| InputElementXMLTag ::
| XMLTagCharacters
| XMLTagPunctuator
| XMLAttributeValue
| XMLWhitespace
| {
| [...]
| XMLTagCharacters ::
| SourceCharacters but no embedded XMLTagPunctuator
| or left-curly { or quote ' or double-quote " or forward-slash /
| or XMLWhitespaceCharacter
| [...]
| XMLTagPunctuator :: one of
| =
| >
| />

Therefore, the following should work with both implementations:

var myBigBlob = (<foo><![CDATA[
...
]]></foo>).toString();

(see also ECMA-357, 10.1.1). This also points out that the text in-between
is _not_ free-form; it must not contain `]]>' if the code following it is
not well-formed.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Matt Kruse on
On Feb 2, 12:23 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> You mean in both *ECMAScript implementations* (Mozilla.org Gecko's
> SpiderMonkey/TraceMonkey, and Google Chrome's V8).

blah blah blah

> The syntax you have used to date apparently makes use of a bug in Gecko's
> ECMAScript for XML (E4X, ECMA-357) implementation.  V8 breaks on this
> syntax because syntactically valid ECMAScript for XML is expected

No.

> Therefore, the following should work with both implementations:
>   var myBigBlob = (<foo><![CDATA[
>     ...
>   ]]></foo>).toString();

Did you try it? It doesn't work.

Matt Kruse
From: Thomas 'PointedEars' Lahn on
Matt Kruse wrote:

> Thomas 'PointedEars' Lahn wrote:
>> You mean in both *ECMAScript implementations* (Mozilla.org Gecko's
>> SpiderMonkey/TraceMonkey, and Google Chrome's V8).
>
> blah blah blah

I like you, too.

>> The syntax you have used to date apparently makes use of a bug in
>> Gecko's ECMAScript for XML (E4X, ECMA-357) implementation. V8 breaks
>> on this syntax because syntactically valid ECMAScript for XML is
>> expected
>
> No.

Then I am at a loss to explain why it breaks in Chrome, unless V8 would not
support E4X.

>> Therefore, the following should work with both implementations:
>> var myBigBlob = (<foo><![CDATA[
>> ...
>> ]]></foo>).toString();
>
> Did you try it?

No. I would have worded my reply differently if I had the opportunity to
try it, wouldn't I?

> It doesn't work.

Perhaps "it doesn't work" (read the FAQ Notes about proper error reporting)
because Chrome's V8 would not support E4X to begin with, and Greasemonkey
would need to use the underlying scripting language. In that case you
would need to find another way, like escaped newlines in string literals,
should V8 support them already.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Lasse Reichstein Nielsen on
Thomas 'PointedEars' Lahn <PointedEars(a)web.de> writes:

> Then I am at a loss to explain why it breaks in Chrome, unless V8 would not
> support E4X.

It doesn't. Nor does SquirrelFish, Trident or Carakan (so far).
E4X is, afaik, only implemented by Mozilla and Adobe.

And XML sucks, so I hope it stays that way! :)

/L 'If you are writing XML by hand, you are doing something wrong!'
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'