A JavaScript heredoc

JavaScript lacks PHP’s concept of heredoc:

Another way to delimit strings is by using heredoc syntax (<<<). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation.

Here is a little function that can be used to emulate it. But it only works in IE (sigh). If you know how to do it also in Firefox, I’ll be very glad to update this post.

function heredoc( fn, from, top, sep ) {
	var fnBody = /functions*(s*)s*{((?:.|n)*)}/g.exec( fn )[1];
	//line number delimiters
	if( typeof from == "number" && typeof top == "number" ) {
		try {
			fnBody = fnBody.split( /r?n/ ).slice( from, top ).join( sep ? sep : "n" );
		} 
		catch( e ) {
		}
	}
	//string delimiters
	else if( typeof from == "string" && typeof top == "string" ) {
		try {
			fnBody = fnBody.split( from, 2 )[1].split( top, 2 )[0];
		} 
		catch( e ) {
		}
	}
	//variable substitution
	fnBody = fnBody.replace( /$([A-Za-z_$][w$]*)/g, function() { 
		var substitution = "";
		try	{
			substitution += eval( arguments[1] );
		}
		catch( e ) {
			substitution = arguments[0];
		}
		return substitution;
	} );
	return fnBody;
}

Really it should be called herescript, because fn (the mandatory argument) is a function declaration, so the text must be syntactically correct JavaScript code, not just plain text. This feature is very convenient when I want to defer the execution of some statements while having them highlighted by my editor, but it bothers a little when I want just a plain text.

To solve this, the function also accepts the optional arguments from and top. If I supply two numbers, the heredoc text will be all the lines between from included and top excluded. In this case I can supply also an optional separator to put in between lines. If I supply two strings, the heredoc text will be all the characters between from and top, both excluded.

myHereDoc1: when the content is a script

myHereDoc1 = heredoc( function() {
alert( 
	  "this text is"
	+ " NOT "
	+ "the content returned" 
);
} );

myHereDoc2: when the content is text, delimited by line number

myHereDoc2 = heredoc( function() { /*
this text is
the content returned
*/ }, 1, -1, "<br />" );

myHereDoc3: when the content is text, delimited by substrings

myHereDoc3 = heredoc( function() { /*
START
this text is the content returned
STOP
but this text is not
START
nor is this
STOP
*/ }, "START", "STOP" );

To try all these examples together, we can write this: document.open(); document.write( "

" + "myHereDoc1 = |" + myHereDoc1 + "|" + "
alert( myHereDoc1 );".link( "javascript:alert( myHereDoc1 );" ) + "
eval( myHereDoc1 );".link( "javascript:eval( myHereDoc1 );" ) + "

myHereDoc2 = |" + myHereDoc2 + "|" + "
alert( myHereDoc2 );".link( "javascript:alert( myHereDoc2 );" ) + "

myHereDoc3 = |" + myHereDoc3 + "|" + "
alert( myHereDoc3 );".link( "javascript:alert( myHereDoc3 );" ) + "

" ); document.close();

to get this:

But cha-chaaa, here goes a bonus. Using a heredoc like this: document.open(); document.write( "

" + heredoc( function() { /* myHereDoc1 = |$myHereDoc1| alert( myHereDoc1 ); eval( myHereDoc1 ); myHereDoc2 = |$myHereDoc2| alert( myHereDoc2 ); myHereDoc3 = |$myHereDoc3| alert( myHereDoc3 ); */ }, 1, -1, "
" ) .replace( /alert( myHereDocd );/g, "$&".link( "javascript:$&" ) ) .replace( /eval( myHereDoc1 );/, "$&".link( "javascript:$&" ) ) + "

" ); document.close();

the same result is achieved, but the javascript code is much cleaner:

3 Replies to “A JavaScript heredoc”

  1. I know this post is old but I have the answer to your firefox dilema if you are interested. Just shoot me an email letting me know how to get this working in firefox and I’ll reply with the code.
    Great article btw…

Leave a Reply

Your email address will not be published. Required fields are marked *