Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ private static Map<CharSequence, CharSequence> escapeMap(final String[][] table)
EscapingStrategy JS = value ->
value == null ? null : StringEscapeUtils.escapeEcmaScript(value.toString());

/** Escape variable for JSON. */
EscapingStrategy JSON = value ->
value == null ? null : StringEscapeUtils.escapeJson(value.toString());

/** NOOP escaping. */
EscapingStrategy NOOP = value -> value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.jknack.handlebars;

import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class EscapingStrategyTest {
@Test
public void shouldEscapeVariableForJSON() throws IOException {
Template template = new Handlebars().with(EscapingStrategy.JSON).compileInline("{{this}}");

assertEquals("\\\"", template.apply("\""));
assertEquals("\\\\", template.apply("\\"));
assertEquals("\\/", template.apply("/"));
}
}