Skip to content

Commit

Permalink
Fix bug in conditional expression lexer. Switch to MIT license.
Browse files Browse the repository at this point in the history
  • Loading branch information
PandoTom committed Jan 1, 2022
1 parent 9fc3d8d commit d8ed7ce
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Release History

3.6.2 - 2021-12-31 tmcclure

Fix bug in conditional expression tokenizer
Switch to MIT license

3.6.1 - 2021-03-21 tmcclure

json-smart is now an optional dependency, no longer required in pom.xml
Add support for jackson-databind to unlock json macro features

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Chunk is compact and speedy, easy to learn yet very powerful.

Chunk templates provide a rich featureset: in-tag filters, in-tag default values, includes, looping and branching, defining multiple snippets per template file, layered themes, macros, and much much more.

Free to use, copyright waived. MIT License.

Quick Start
===========

Expand All @@ -23,7 +25,7 @@ Available from Maven Central:
<dependency>
<groupId>com.x5dev</groupId>
<artifactId>chunk-templates</artifactId>
<version>3.6.1</version>
<version>3.6.2</version>
</dependency>
```

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>com.x5dev</groupId>
<artifactId>chunk-templates</artifactId>
<packaging>jar</packaging>
<version>3.6.1</version>
<version>3.6.2</version>
<name>Chunk Templates</name>
<description>Chunk Template Engine for Java</description>
<url>http://www.x5software.com/chunk/</url>
Expand All @@ -20,8 +20,8 @@

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/x5/template/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@
* Updates: <A href="http://www.x5software.com/chunk/">Chunk Documentation</A><BR>
*
* @author Tom McClure
* @version 3.6.1
* @version 3.6.2
*/

public class Chunk implements Map<String,Object>
{
public static final int HASH_THRESH = 8;
public static final int DEPTH_LIMIT = 17;

public static final String VERSION = "3.6.1";
public static final String VERSION = "3.6.2";

public static final String TRUE = "TRUE";

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/x5/template/CondLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private Iterator<String> lexLogicTokens()
// not a logic token, must be the start of a comparison/test.
int j = endOfComparison(conditional, i);
tokens.add(conditional.substring(i,j));
i = j;
i = j-1;
}

return tokens.iterator();
Expand Down Expand Up @@ -105,6 +105,9 @@ private int endOfComparison(String s, int start) {
}
continue;
}
if (exprCount > 0 && (c == '&' || c == '|' || c == ')')) {
return i;
}
if (exprCount > 0 && (c == '=' || c == '!')) {
if (i+1 == len) {
return len;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/x5/template/IfTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ private String stripCasing(String params)
int exprPos = params.indexOf("f") + 1; // if or elseIf
String test = params.substring(exprPos).trim();
if (test.charAt(0) == '(' && test.charAt(test.length()-1) == ')') {
test = test.substring(1,test.length()-1);
// DON'T strip outer parens if there are ANY inner parens
// in fact, maybe never strip outer parens anymore,
// ie now that the cond expr parser can handle parens?
int innerParenPos = test.indexOf(')', 1);
if (innerParenPos == test.length()-1) {
test = test.substring(1, test.length() - 1);
}
}

return test;
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/com/x5/template/IfTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,62 @@ public void testGrouping()
assertEquals("fail", c.toString());
}

@Test
public void testAndWithBoolAndInequality()
{
Chunk c = new Chunk();
c.append("{%if $a && $b != 0.0 %}pass{% else %}fail{% endif %}");
c.set("a", "A");
c.set("b", 1);

assertEquals("pass", c.toString());

c.unset("a");

assertEquals("fail", c.toString());

c.set("a", "A");
c.set("b", "0.0");

assertEquals("fail", c.toString());
}

@Test
public void testAndWithBoolAndInequalityAndGrouping()
{
Chunk c = new Chunk();
c.append("{%if $a && ($b != 0.0) %}pass{% else %}fail{% endif %}");
c.set("a", "A");
c.set("b", 1);

assertEquals("pass", c.toString());

c.resetTemplate();
c.append("{%if ($a) && ($b != 0.0) %}pass{% else %}fail{% endif %}");

assertEquals("pass", c.toString());

c.unset("a");
assertEquals("fail", c.toString());

c.set("a", "A");
c.set("b", "0.0");
assertEquals("fail", c.toString());

c.resetTemplate();
c.append("{%if (($a) && ($b != 0.0)) %}pass{% else %}fail{% endif %}");

c.set("b", 1);
assertEquals("pass", c.toString());

c.unset("a");
assertEquals("fail", c.toString());

c.set("a", "A");
c.set("b", "0.0");
assertEquals("fail", c.toString());
}

@Test
public void testIfTagWithConstants()
{
Expand Down

0 comments on commit d8ed7ce

Please sign in to comment.