Handle CRLF in skipNewline mode.

This commit is contained in:
Michael Bayne
2014-12-01 16:35:22 -08:00
parent f838c91f99
commit 614bb82b17
2 changed files with 25 additions and 6 deletions
@@ -336,14 +336,14 @@ public class Mustache
int v; int v;
while ((v = nextChar()) != -1) { while ((v = nextChar()) != -1) {
char c = (char)v; char c = (char)v;
if (c == '\n') { if (c == '\r' && skipNewline) {
// skip this \r and stay in skip newline mode
continue;
} else if (c == '\n') {
column = 0; column = 0;
line++; line++;
// skip this newline character if we're configured to do so; TODO: handle CR // skip this newline character if we're configured to do so
if (skipNewline) { if (skipNewline) {
// if the preceding character is '\r', strip that off too
int lastIdx = text.length()-1;
if (lastIdx >= 0 && text.charAt(lastIdx) == '\r') text.setLength(lastIdx);
skipNewline = false; skipNewline = false;
continue; continue;
} }
@@ -619,7 +619,7 @@ public class Mustache
} }
protected static void requireNoNewlines (String tag, int line) { protected static void requireNoNewlines (String tag, int line) {
if (tag.indexOf("\n") != -1 || tag.indexOf("\r") != -1) { if (tag.indexOf('\n') != -1 || tag.indexOf('\r') != -1) {
throw new MustacheParseException( throw new MustacheParseException(
"Invalid tag name: contains newline '" + tag + "'", line); "Invalid tag name: contains newline '" + tag + "'", line);
} }
@@ -447,6 +447,25 @@ public class MustacheTest
"endlist", tmpl, context("items", Collections.emptyList())); "endlist", tmpl, context("items", Collections.emptyList()));
} }
@Test public void testNewlineSkippingCRLF () {
String tmpl = "list:\r\n" +
"{{#items}}\r\n" +
"{{this}}\r\n" +
"{{/items}}\r\n" +
"{{^items}}\r\n" +
"no items\r\n" +
"{{/items}}\r\n" +
"endlist";
test("list:\r\n" +
"one\r\n" +
"two\r\n" +
"three\r\n" +
"endlist", tmpl, context("items", Arrays.asList("one", "two", "three")));
test("list:\r\n" +
"no items\r\n" +
"endlist", tmpl, context("items", Collections.emptyList()));
}
@Test public void testNewlineNonSkipping () { @Test public void testNewlineNonSkipping () {
// only when a section tag is by itself on a line should we absorb the newline following it // only when a section tag is by itself on a line should we absorb the newline following it
String tmpl = "thing?: {{#thing}}yes{{/thing}}{{^thing}}no{{/thing}}\n" + String tmpl = "thing?: {{#thing}}yes{{/thing}}{{^thing}}no{{/thing}}\n" +