G
Garrett Smith
Stefan said: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?
Sorry, I don't. I've always wondered why multi-line string literals
aren't possible in JavaScript. AFAICS, there's nothing ambiguous about
this syntax:
var heredoc = "I am a multi-line
string; I end when the tokenizer
sees a double quote";
This is standard practice in many other programming languages. Maybe ES4
had something like this in the queue, but with ES5 geared towards
backwards compatibility, we probably won't see it for a long time.
The two usual workarounds are
var str = "I wish\n"
+ "I was\n"
+ "a multi-line string";
and
var str = "I am the closest thing\
to multi-line strings that we can get\
with JavaScript";
I don't know how well supported the second version is. At least the
first version will can optimized into a single string when it's
processed with a minimizer.
Says in Ecma 262r3:
7.3 Line Terminators
A line terminator cannot occur within any token, not even a string.
It seems unsafe to rely on that. It might produce the desired outcome,
but if that failed, then it would be your fault.
Seems to have changed in ES5.
Ecma 262r5
7.3
| Line terminators may only occur within a StringLiteral token as part
| of a LineContinuation.
And
| A line terminator character cannot appear in a string literal, except
| as part of a LineContinuation to produce the empty character sequence.
| The correct way to cause a line terminator character to be part of the
| String value of a string literal is to use an escape sequence such as
| \n or \u000A.
And
| The SV of LineContinuation :: \ LineTerminatorSequence is the empty
| character sequence.
Good to see you back, BTW.