Skip to content

Commit

Permalink
Add support for escaping raw expressions (TVAR)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonMcPherson committed Nov 29, 2023
1 parent ae4e84a commit b3b7c7f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ lexer grammar HbsLexer;
}

private boolean varEscape(final String start, final String end) {
// Support escaping raw expressions (TVar) and normal expressions (VAR)
return consumeVarEscape(start + "{", end + "}") || consumeVarEscape(start, end);
}

private boolean consumeVarEscape(final String start, final String end) {
if (ahead("\\" + start)) {
int offset = start.length();
while (!isEOF(offset)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void shouldCompileTo(final String template, final Object context,
throws IOException {
Template t = compile(template, helpers, partials);
String result = t.apply(configureContext(context));
assertEquals("'" + expected + "' should === '" + result + "': " + message, expected, result);
assertEquals("'" + result + "' should === '" + expected + "': " + message, expected, result);
}

protected Object configureContext(final Object context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.jknack.handlebars.i1084;

import com.github.jknack.handlebars.AbstractTest;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class Issue1084 extends AbstractTest {

@Test
public void escapeRawVars() throws IOException {
shouldCompileTo("\\{{{foo}}}", $, "{{{foo}}}");
}

@Test
public void escapeRawVarsWithText() throws IOException {
shouldCompileTo("before \\{{{foo}}} after", $, "before {{{foo}}} after");
}

@Test
public void escapeRawVsUnescape() throws IOException {
shouldCompileTo("\\{{{foo}}} {{{foo}}}", $("foo", "bar"), "{{{foo}}} bar");
}

@Test
public void escapeRawMultiline() throws IOException {
shouldCompileTo("\\{{{foo\n}}}", $("foo", "bar"), "{{{foo\n}}}");
}

@Test
public void rawBlockEscape() throws IOException {
shouldCompileTo("\\{{{#foo}}}", $("foo", "bar"), "{{{#foo}}}");
}

@Test
public void rawBlockEscapeWithParams() throws IOException {
shouldCompileTo("\\{{{#foo x a x}}}", $("foo", "bar"), "{{{#foo x a x}}}");
}

@Test
public void escapeRawVarToText() throws IOException {
assertEquals("\\{{{foo}}}", compile("\\{{{foo}}}").text());
}

}

0 comments on commit b3b7c7f

Please sign in to comment.