I needed to escape some strings pulled from database for Javascript, and it
turned out to be surprisingly difficult. AFAIK, none of the Tapestry/Tacos
markup writers/character translators escapes a single quote ', so I found
myself writing:
    public class JavascriptCharacterTranslator extends
MarkupCharacterTranslator {
        public String translate(char ch) {
            return ch == '\'' ? "'" : super.translate(ch);
        }
    }

and then in the component:
        PrintWriter writer = new PrintWriter(out);
        MarkupFilterUtils.print(writer, text.toCharArray(), 0, text.length(),
true, new JavascriptCharacterTranslator() );
        writer.flush();

note that the MarkupFilterUtils escapes double quotes when escapeQuotes
parameter is set to true, but not the single quote. Maybe I just missed that
little helper util that does the whole she-bang, anybody know if there is
one? Using T4.1.3 & Tacos 4.1.0.

Kalle

Reply via email to