Skip to content

Commit

Permalink
Reduce number of ignored NumberFormatExceptions in PugJexlArithmetic#…
Browse files Browse the repository at this point in the history
…add operation #32

Also changed subtract to be in line with pugjs
  • Loading branch information
chbloemer committed Jan 10, 2025
1 parent eb1a348 commit c9eadf5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/java/de/neuland/pug4j/jexl3/PugJexlArithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.Collection;

import static org.apache.commons.lang3.StringUtils.isNumeric;

public class PugJexlArithmetic extends JexlArithmetic {

public PugJexlArithmetic(boolean astrict) {
Expand Down Expand Up @@ -41,11 +43,17 @@ public boolean toBoolean(final Object val) {
}
return super.toBoolean(val);
}

@Override
public Object subtract(final Object left, final Object right) {
if ((left instanceof String && !isNumeric((String)left) && !isFloatingPointNumber(left)) || (right instanceof String&& !isNumeric((String)right) && !isFloatingPointNumber(right))) {
return "null";
}
return super.subtract(left, right);
}
@Override
public Object add(Object left, Object right) {
if (left instanceof String && right instanceof String) {
return (String)left + right;
if (left instanceof String || right instanceof String) {
return (left == null ? "" : toString(left)).concat(right == null ? "" : toString(right));
}
return super.add(left, right);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/issues/pug032.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<ul id="menu">
<li><a href="/browse?page=4">Previous page</a></li>
<li><a href="/browse?page=6">Next page</a></li>
</ul>
<ul id="menu2">
<li><a href="/browse?page=4">Previous page</a></li>
<li><a href="/browse?page=51">Next page</a></li>
</ul>
<ul id="menu3">
<li><a href="null">Previous page</a></li>
<li><a href="/browse?page=51">Next page</a></li>
</ul>
<ul id="menu4">
<li><a href="null">Previous page</a></li>
<li><a href="/browse?page=51">Next page</a></li>
</ul>
18 changes: 18 additions & 0 deletions src/test/resources/issues/pug032.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-var page = 5
ul#menu
li: a(href="/browse?page=" + (page - 1)) Previous page
li: a(href="/browse?page=" + (page + 1)) Next page

-page = "5"
ul#menu2
li: a(href="/browse?page=" + (page - 1)) Previous page
li: a(href="/browse?page=" + (page + 1)) Next page
-page = 5
ul#menu3
li: a(href="/browse?page=" + page - 1) Previous page
li: a(href="/browse?page=" + page + 1) Next page

-page= "5"
ul#menu4
li: a(href="/browse?page=" + page - 1) Previous page
li: a(href="/browse?page=" + page + 1) Next page

0 comments on commit c9eadf5

Please sign in to comment.