From b234db3945325534e2a5459f60580622a028806e Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Fri, 14 Jan 2022 12:11:02 +0100 Subject: [PATCH] Qute - relax infix notation syntax - ignore redundant blank space before infix method and before infix method param Co-authored-by: George Gastaldi --- .../main/java/io/quarkus/qute/Expressions.java | 16 +++++++++++++--- .../java/io/quarkus/qute/ExpressionTest.java | 11 +++++++++++ .../test/java/io/quarkus/qute/SimpleTest.java | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/Expressions.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/Expressions.java index 5eaddcd8c065c..fa0e72a7e56dc 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/Expressions.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/Expressions.java @@ -100,10 +100,20 @@ public static List splitParts(String value, SplitConfig splitConfig) { } // Non-separator char if (!literal) { + // Not inside a string/type literal if (brackets == 0 && c == ' ' && splitConfig.isInfixNotationSupported()) { - // Not inside a virtual method - if (infix == 1) { - // The second space after the infix method + // Infix supported, blank space and not inside a virtual method + if (separator == 0 + && (buffer.length() == 0 || buffer.charAt(buffer.length() - 1) == '(')) { + // Skip redundant blank space: + // 1. before the infix method + // foo or bar + // ----^ + // 2. before an infix method parameter + // foo or bar + // -------^ + } else if (infix == 1) { + // The space after the infix method // foo or bar // ------^ buffer.append(LEFT_BRACKET); diff --git a/independent-projects/qute/core/src/test/java/io/quarkus/qute/ExpressionTest.java b/independent-projects/qute/core/src/test/java/io/quarkus/qute/ExpressionTest.java index 19e919b31726e..9165decf7a431 100644 --- a/independent-projects/qute/core/src/test/java/io/quarkus/qute/ExpressionTest.java +++ b/independent-projects/qute/core/src/test/java/io/quarkus/qute/ExpressionTest.java @@ -68,6 +68,17 @@ public void testExpressions() throws InterruptedException, ExecutionException { virtualMethod("or", ExpressionImpl.literalFrom(-1, "null"))); } + @Test + public void testInfixNotationRedundantSpace() throws InterruptedException, ExecutionException { + verify("name or 'John'", null, null, name("name"), virtualMethod("or", ExpressionImpl.from("'John'"))); + verify("name or 'John'", null, null, name("name"), virtualMethod("or", ExpressionImpl.from("'John'"))); + verify("name or 'John'", null, null, name("name"), virtualMethod("or", ExpressionImpl.from("'John'"))); + verify("name or 'John' or 1", null, null, name("name"), virtualMethod("or", ExpressionImpl.from("'John'")), + virtualMethod("or", ExpressionImpl.literalFrom(-1, "1"))); + verify("name or 'John' or 1", null, null, name("name"), virtualMethod("or", ExpressionImpl.from("'John'")), + virtualMethod("or", ExpressionImpl.literalFrom(-1, "1"))); + } + @Test public void testNestedVirtualMethods() { assertNestedVirtualMethod(ExpressionImpl.from("movie.findServices(movie.name,movie.toNumber(movie.getName))")); diff --git a/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java b/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java index 59b554299dd12..8f37ecb93e6f7 100644 --- a/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java +++ b/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java @@ -83,6 +83,7 @@ public void testOrElseResolver() { data.put("nameOptional", Optional.of("BUG")); assertEquals("John Bug", engine.parse("{name.or('John')} {surname.or('John')}").render(data)); assertEquals("John Bug", engine.parse("{name ?: 'John'} {surname or 'John'}").render(data)); + assertEquals("John Bug", engine.parse("{name ?: 'John'} {surname or 'John'}").render(data)); assertEquals("John Bug", engine.parse("{name ?: \"John Bug\"}").render(data)); assertEquals("Is null", engine.parse("{foo ?: 'Is null'}").render(data)); assertEquals("10", engine.parse("{foo.age.limit ?: 10}").render(data));