[stringtemplate-interest] nesting one formatted string within another formatted string
Eric B
ebatzdor at gmail.com
Sat Jan 29 21:26:52 PST 2011
Hi All,
I'm building up an HTML string that should ultimately resolve to something
like the following where someString is unknown and could contain special
characters...
<img src="someImage.gif" onClick="javascript:alert('someString');"/>
If someString really has special characters in it, it needs to be escaped
for JS and then entire onClick attribute value needs to be escaped for HTML.
To format the full attribute value as HTML with StringTemplate is no
problem:
escapeTest(someString) ::= <<
<img src="someImage.gif" onClick="$"javascript:alert('" + someString +
"');";format="HTML"$"/>
>>
And to format someString as JS without formatting the HTML is no problem .
I do something like:
escapeTest(someString) ::= <<
<img src="someImage.gif"
onClick="javascript:alert('$someString;format="JS"$');"/>
>>
But I could not figure out a way to use the format option on someString when
it's nested in a formatted expression.
The most graceful way I could figure to do this was to JS escape with
another template... like so:
jsEscape(jsString) ::= <<
$jsString;format="JS"$
>>
escapeTest(someString) ::= <<
<img src="someImage.gif" onClick="$"javascript:alert('" +
jsEscape(someString) + "');";format="HTML"$"/>
>>
Does this seem the best I can do? Or does the syntax support using an
in-line format on someString?
Thanks!,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110129/e872a880/attachment.html
More information about the stringtemplate-interest
mailing list