Skip to content

Commit 7236648

Browse files
committed
#210 Correction
1 parent 4cab589 commit 7236648

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

javalite-templator/src/main/java/org/javalite/templator/TemplateParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* TAG_START = '&lt' '#'
25-
* TAG_END = '&lt' '/' '#' LOWER_ALPHA '>'
25+
* TAG_END = '&lt' '/' '#' LOWER_ALPHA '>'
2626
* IF_TAG = TAG_START "if" '(' EXPRESSION ')' '>' LIST_TAG_BODY TAG_END "if" '>'
2727
* </pre>
2828
*
@@ -169,9 +169,11 @@ private Exp _expId() {
169169
ChainedIds chainedIds = _chainedIds();
170170
if (chainedIds == null) { return null; }
171171
_whitespace(); // optional
172+
//TODO: refactor this
173+
int currentIndex = index;
172174
Comparison.Operator operator = _comparisonOperator();
173175
if (operator == null) {
174-
return new BooleanId(chainedIds);
176+
return currentIndex == index ? new BooleanId(chainedIds) : null;
175177
} else {
176178
_whitespace(); // optional
177179
ChainedIds rightOperand = _chainedIds();

javalite-templator/src/test/java/org/javalite/templator/TemplateParserSpec.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77
import static org.javalite.common.Collections.*;
88
import static org.javalite.test.jspec.JSpec.*;
9+
import org.junit.Ignore;
910

1011
/**
1112
* @author Eric Nielsen
@@ -47,23 +48,23 @@ public void shouldParseBuiltIn() throws Exception {
4748
}
4849

4950
@Test
50-
public void shouldParseExpressionWithEq() throws Exception {
51+
public void shouldParseIfTagWithEq() throws Exception {
5152
ParentNode node = (ParentNode) new TemplateParser("<b><<#if(left==right)>tada</#if>></b>").parse();
5253
the(node.children.size()).shouldBeEqual(3);
5354
the(process(node, map("left", "help!", "right", "help!"))).shouldBeEqual("<b><tada></b>");
5455
the(process(node, map("left", "help!", "right", "help?"))).shouldBeEqual("<b><></b>");
5556
}
5657

5758
@Test
58-
public void shouldParseExpressionWithNotNeq() throws Exception {
59+
public void shouldParseIfTagWithNotNeq() throws Exception {
5960
ParentNode node = (ParentNode) new TemplateParser("<#if(!left!=right)>tada</#if>").parse();
6061
the(node.children.size()).shouldBeEqual(1);
6162
the(process(node, map("left", 0, "right", 0))).shouldBeEqual("tada");
6263
the(process(node, map("left", 0, "right", 1))).shouldBeEqual("");
6364
}
6465

6566
@Test
66-
public void shouldParseExpressionWithAndParensOr() throws Exception {
67+
public void shouldParseIfTagWithAndParensOr() throws Exception {
6768
ParentNode node = (ParentNode) new TemplateParser(
6869
"<#if (first > second && (first > third || third <= second))><b>%{first}</b></#if>").parse();
6970
the(node.children.size()).shouldBeEqual(1);
@@ -73,11 +74,26 @@ public void shouldParseExpressionWithAndParensOr() throws Exception {
7374
}
7475

7576
@Test
76-
public void shouldParseExpressionWithBoolean() throws Exception {
77+
public void shouldParseIfTagWithBoolean() throws Exception {
7778
ParentNode node = (ParentNode) new TemplateParser("<#if (foo)>bar</#if>").parse();
7879
the(node.children.size()).shouldBeEqual(1);
7980
the(process(node, map("foo", true))).shouldBeEqual("bar");
8081
the(process(node, map("foo", false))).shouldBeEqual("");
8182
}
8283

84+
@Test
85+
public void shouldNotParseInvalidIfTagWithBoolean() throws Exception {
86+
String source = "<#if (foo=)>bar</#if>";
87+
ParentNode node = (ParentNode) new TemplateParser(source).parse();
88+
the(node.children.size()).shouldBeEqual(1);
89+
the(process(node, map())).shouldBeEqual(source);
90+
}
91+
92+
@Ignore
93+
@Test
94+
public void shouldParseForTag() throws Exception {
95+
ParentNode node = (ParentNode) new TemplateParser("<#for item:list> %{item} </#for>").parse();
96+
the(node.children.size()).shouldBeEqual(1);
97+
the(process(node, map("list", list(1, 2, 3)))).shouldBeEqual(" 1 2 3 ");
98+
}
8399
}

0 commit comments

Comments
 (0)