Skip to content

Commit 317f4a0

Browse files
abersnazejknack
authored andcommitted
allow the formatter to emit a SafeString to opt out of escaping. fix for #682
1 parent 89ec4a3 commit 317f4a0

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/internal/Variable.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,13 @@ protected void collect(final Collection<String> result, final TagType tagType) {
238238
* @return Formatted and escaped value.
239239
*/
240240
protected CharSequence formatAndEscape(final Object value, final Formatter.Chain formatter) {
241-
CharSequence formatted = formatter.format(value).toString();
242-
if (value instanceof Handlebars.SafeString) {
243-
return formatted;
241+
Object formatted = formatter.format(value);
242+
CharSequence formattedString = formatted.toString();
243+
if (formatted instanceof Handlebars.SafeString) {
244+
return formattedString;
244245
}
245-
return escapingStrategy.escape(formatted);
246+
CharSequence escapedString = escapingStrategy.escape(formattedString);
247+
return escapedString;
246248
}
247249

248250
@Override
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.jknack.handlebars;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import java.io.IOException;
5+
import org.junit.Test;
6+
7+
public class Issue682 extends AbstractTest {
8+
@Override
9+
protected void configure(final Handlebars handlebars) {
10+
handlebars.with((value, chain) -> {
11+
return new Handlebars.SafeString(value.toString());
12+
});
13+
}
14+
15+
@Test
16+
public void dowork() throws IOException {
17+
Template t = compile("{{this}}");
18+
// the formatter (wrongly) trusts all values in the context but proves that it's working
19+
assertEquals("as\"df", t.apply("as\"df"));
20+
}
21+
}

0 commit comments

Comments
 (0)