Fix indent lambda using \n as line break (#16464)

* fix indent lambda using \n as line break

* update tests

* more fix
This commit is contained in:
William Cheng 2023-09-01 09:07:14 +08:00 committed by GitHub
parent 71b33db7ee
commit c74ed98282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View File

@ -109,7 +109,8 @@ public class IndentedLambda implements Mustache.Lambda {
String prefixedIndention = StringUtils.repeat(new String(Character.toChars(spaceCode)), prefixSpaceCount);
StringBuilder sb = new StringBuilder();
String[] lines = text.split(System.lineSeparator());
// use \n instead of System.lineSeparator (e.g. \r\n in Windows) as templates use \n
String[] lines = text.split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
// Mustache will apply correct indentation to the first line of a template (to match declaration location).
@ -121,9 +122,9 @@ public class IndentedLambda implements Mustache.Lambda {
sb.append(line);
// We've split on the system's line separator. We don't want to add an additional trailing line.
// We've split on \n. We don't want to add an additional trailing line.
if (i < lines.length - 1) {
sb.append(System.lineSeparator());
sb.append("\n");
}
}
writer.write(sb.toString());

View File

@ -6,18 +6,17 @@ import org.testng.annotations.Test;
public class IndentedLambdaTest extends LambdaTest {
String lineSeparator = System.lineSeparator();
String lineSeparator = "\n";
@Test
public void defaultIndentTest() {
// Given
Map<String, Object> ctx = context("indented", new IndentedLambda());
String lineSeparator = System.lineSeparator();
// When & Then
// IndentedLambda applies indentation from second line on of a template.
test("first line" + lineSeparator + " second line",
"{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx);
"{{#indented}}first line" + lineSeparator + "second line{{/indented}}", ctx);
}
@Test
@ -28,7 +27,7 @@ public class IndentedLambdaTest extends LambdaTest {
// When & Then
// IndentedLambda applies indentation from second line on of a template.
test("first line" + lineSeparator + " second line",
"{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx);
"{{#indented}}first line" + lineSeparator + "second line{{/indented}}", ctx);
}