Skip to content

Commit

Permalink
Merge pull request #22036 from mkouba/issue-21858
Browse files Browse the repository at this point in the history
Qute - fix section parameters parser
  • Loading branch information
mkouba authored Dec 9, 2021
2 parents 32773ed + 7e5f3e4 commit 07e923a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ static Iterator<String> splitSectionParams(String content, Function<String, Runt

boolean stringLiteral = false;
short composite = 0;
byte brackets = 0;
boolean space = false;
List<String> parts = new ArrayList<>();
StringBuilder buffer = new StringBuilder();
Expand All @@ -648,7 +649,7 @@ static Iterator<String> splitSectionParams(String content, Function<String, Runt
char c = content.charAt(i);
if (c == ' ') {
if (!space) {
if (!stringLiteral && composite == 0) {
if (!stringLiteral && composite == 0 && brackets == 0) {
if (buffer.length() > 0) {
parts.add(buffer.toString());
buffer = new StringBuilder();
Expand All @@ -669,6 +670,12 @@ && isCompositeStart(c) && (i == 0 || space || composite > 0
} else if (!stringLiteral
&& isCompositeEnd(c) && composite > 0) {
composite--;
} else if (!stringLiteral
&& Parser.isLeftBracket(c)) {
brackets++;
} else if (!stringLiteral
&& Parser.isRightBracket(c) && brackets > 0) {
brackets--;
}
space = false;
buffer.append(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ public void testExpressions() throws InterruptedException, ExecutionException {

@Test
public void testNestedVirtualMethods() {
Expression exp = ExpressionImpl.from("movie.findServices(movie.name,movie.toNumber(movie.getName))");
assertNestedVirtualMethod(ExpressionImpl.from("movie.findServices(movie.name,movie.toNumber(movie.getName))"));
assertNestedVirtualMethod(ExpressionImpl.from("movie.findServices(movie.name, movie.toNumber(movie.getName) )"));
}

private void assertNestedVirtualMethod(Expression exp) {
assertNull(exp.getNamespace());
List<Expression.Part> parts = exp.getParts();
assertEquals(2, parts.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public void testSectionParameters() {
"false");
assertParams("(item.name == 'foo') and (item.name is false)", "(item.name == 'foo')", "and", "(item.name is false)");
assertParams("(item.name != 'foo') || (item.name == false)", "(item.name != 'foo')", "||", "(item.name == false)");
assertParams("foo.codePointCount(0, foo.length) baz=bar", "foo.codePointCount(0, foo.length)", "baz=bar");
assertParams("foo.codePointCount( 0 , foo.length( 1)) baz=bar", "foo.codePointCount( 0 , foo.length( 1))", "baz=bar");
}

@Test
Expand Down Expand Up @@ -372,6 +374,15 @@ public void testInvalidParamDeclaration() {
"Parser error on line 1: invalid parameter declaration {@\n}", 1);
}

@Test
public void testUserTagVirtualMethodParam() {
Engine engine = Engine.builder().addDefaults().addValueResolver(new ReflectionValueResolver())
.addSectionHelper(new UserTagSectionHelper.Factory("form", "form-template")).build();
engine.putTemplate("form-template", engine.parse("{it}"));
Template foo = engine.parse("{#form foo.codePointCount(0, foo.length) /}");
assertEquals("3", foo.data("foo", "foo").render());
}

public static class Foo {

public List<Item> getItems() {
Expand Down

0 comments on commit 07e923a

Please sign in to comment.