Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for escaping raw expressions (TVAR) #1085

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor fix to the order of arguments so that result should equal expected instead of the other way around

}

protected Object configureContext(final Object context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy of the Issue294 test suite except uses raw expressions (triple curly brackets)


@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());
}

}