Repository: PebbleTemplates/pebble
Pull request #396 (2018-08-28 14:38:07)

Add json escape strategy as an available strategy (#395)


Patch:
diff --git a/pebble/src/main/java/com/mitchellbosecke/pebble/extension/escaper/EscapeFilter.java b/pebble/src/main/java/com/mitchellbosecke/pebble/extension/escaper/EscapeFilter.java
index 5a3ca6a6..3d2ad056 100644
--- a/pebble/src/main/java/com/mitchellbosecke/pebble/extension/escaper/EscapeFilter.java
+++ b/pebble/src/main/java/com/mitchellbosecke/pebble/extension/escaper/EscapeFilter.java
@@ -20,11 +20,18 @@ import java.util.Map;
 import org.unbescape.css.CssEscape;
 import org.unbescape.html.HtmlEscape;
 import org.unbescape.javascript.JavaScriptEscape;
+import org.unbescape.json.JsonEscape;
 import org.unbescape.uri.UriEscape;
 
 public class EscapeFilter implements Filter {
 
-  private String defaultStrategy = "html";
+  public static final String HTML_ESCAPE_STRATEGY = "html";
+  public static final String JAVASCRIPT_ESCAPE_STRATEGY = "js";
+  public static final String CSS_ESCAPE_STRATEGY = "css";
+  public static final String URL_PARAM_ESCAPE_STRATEGY = "url_param";
+  public static final String JSON_ESCAPE_STRATEGY = "json";
+
+  private String defaultStrategy = HTML_ESCAPE_STRATEGY;
 
   private final List<String> argumentNames = new ArrayList<>();
 
@@ -36,10 +43,11 @@ public class EscapeFilter implements Filter {
   }
 
   private void buildDefaultStrategies() {
-    this.strategies.put("html", HtmlEscape::escapeHtml4Xml);
-    this.strategies.put("js", JavaScriptEscape::escapeJavaScript);
-    this.strategies.put("css", CssEscape::escapeCssIdentifier);
-    this.strategies.put("url_param", UriEscape::escapeUriQueryParam);
+    this.strategies.put(HTML_ESCAPE_STRATEGY, HtmlEscape::escapeHtml4Xml);
+    this.strategies.put(JAVASCRIPT_ESCAPE_STRATEGY, JavaScriptEscape::escapeJavaScript);
+    this.strategies.put(CSS_ESCAPE_STRATEGY, CssEscape::escapeCssIdentifier);
+    this.strategies.put(URL_PARAM_ESCAPE_STRATEGY, UriEscape::escapeUriQueryParam);
+    this.strategies.put(JSON_ESCAPE_STRATEGY, JsonEscape::escapeJson);
   }
 
   @Override


Test patch:
diff --git a/pebble/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java b/pebble/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java
index ea3dd480..de0ac442 100644
--- a/pebble/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java
+++ b/pebble/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java
@@ -8,6 +8,7 @@
  */
 package com.mitchellbosecke.pebble;
 
+import static com.mitchellbosecke.pebble.extension.escaper.EscapeFilter.JSON_ESCAPE_STRATEGY;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 
@@ -273,6 +274,25 @@ public class EscaperExtensionTest {
     assertEquals("true", writer.toString());
   }
 
+  @Test
+  public void testEscapeJson() throws PebbleException, IOException {
+    PebbleEngine pebble = new PebbleEngine.Builder()
+        .loader(new StringLoader())
+        .strictVariables(false)
+        .defaultEscapingStrategy(JSON_ESCAPE_STRATEGY)
+        .build();
+
+    PebbleTemplate template = pebble.getTemplate("{{ text }}");
+
+    Map<String, Object> context = new HashMap<>();
+    context.put("text", "{\"a\": \"a/b/c\"}");
+
+    Writer writer = new StringWriter();
+    template.evaluate(writer, context);
+
+    assertEquals("{\\\"a\\\": \\\"a/b/c\\\"}", writer.toString());
+  }
+
   public static class TestExtension extends AbstractExtension {
 
     @Override