Skip to content

Commit bc445ec

Browse files
committed
Allow to chain escapers fix #521
1 parent 379fdd0 commit bc445ec

4 files changed

Lines changed: 72 additions & 9 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/EscapingStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public interface EscapingStrategy {
4949
* @author edgar
5050
* @since 4.0.4
5151
*/
52-
public class Hbs implements EscapingStrategy {
52+
class Hbs implements EscapingStrategy {
5353

5454
/** EMPTY. */
5555
private static final String EMPTY = "";

handlebars/src/main/java/com/github/jknack/handlebars/Handlebars.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,16 @@ public Handlebars with(final EscapingStrategy escapingStrategy) {
10421042
return this;
10431043
}
10441044

1045+
/**
1046+
* Set a new {@link EscapingStrategy}.
1047+
*
1048+
* @param chain The escaping strategy. Required.
1049+
* @return This handlebars object.
1050+
*/
1051+
public Handlebars with(final EscapingStrategy... chain) {
1052+
return with(newEscapeChain(chain));
1053+
}
1054+
10451055
/**
10461056
* @return A formatter chain.
10471057
*/
@@ -1381,4 +1391,22 @@ public Handlebars setCharset(final Charset charset) {
13811391
public Charset getCharset() {
13821392
return charset;
13831393
}
1394+
1395+
/**
1396+
* Chain escape strategies.
1397+
*
1398+
* @param chain Escape to chain.
1399+
* @return Composite escape strategy.
1400+
*/
1401+
private static EscapingStrategy newEscapeChain(final EscapingStrategy[] chain) {
1402+
return new EscapingStrategy() {
1403+
@Override public CharSequence escape(final CharSequence value) {
1404+
CharSequence result = value;
1405+
for (EscapingStrategy escape : chain) {
1406+
result = escape.escape(result);
1407+
}
1408+
return result;
1409+
}
1410+
};
1411+
}
13841412
}
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
package com.github.jknack.handlebars.issues;
22

3+
import com.github.jknack.handlebars.EscapingStrategy;
4+
import com.github.jknack.handlebars.Handlebars;
35
import com.github.jknack.handlebars.v4Test;
46
import org.junit.Test;
57

68
import java.io.IOException;
79

810
public class Issue478 extends v4Test {
911

10-
@Test
11-
public void whiteSpaceControlShouldWorkOnElse() throws IOException {
12-
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{else}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>\n");
13-
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{else}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "\n<i>");
12+
public static class Foo implements EscapingStrategy {
13+
14+
@Override public CharSequence escape(CharSequence value) {
15+
return value.toString().replace("foo", "bar");
16+
}
17+
}
18+
19+
public static class Bar implements EscapingStrategy {
1420

15-
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~else~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>");
16-
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~else~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "<i>");
21+
@Override public CharSequence escape(CharSequence value) {
22+
return value.toString().replace("bar", "$bar$");
23+
}
24+
}
25+
26+
@Override protected void configure(Handlebars handlebars) {
27+
handlebars.with(new Foo(), new Bar());
28+
}
1729

18-
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~^~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>");
19-
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~^~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "<i>");
30+
@Test
31+
public void shouldAllowToChainEscapeStrategy() throws IOException {
32+
shouldCompileTo("{{var}}", $("hash", $("var", "foo")), "$bar$");
2033
}
2134

2235
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.v4Test;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
public class Issue521 extends v4Test {
9+
10+
@Test
11+
public void whiteSpaceControlShouldWorkOnElse() throws IOException {
12+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{else}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>\n");
13+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{else}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "\n<i>");
14+
15+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~else~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>");
16+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~else~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "<i>");
17+
18+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~^~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", true)), "<b>");
19+
shouldCompileTo("\n{{~#if bold~}}\n<b>\n{{~^~}}\n<i>\n{{~/if~}}\n", $("hash", $("bold", false)), "<i>");
20+
}
21+
22+
}

0 commit comments

Comments
 (0)