diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index 2a09c43..2c69f41 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -336,14 +336,14 @@ public class Mustache int v; while ((v = nextChar()) != -1) { 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; 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 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; continue; } @@ -619,7 +619,7 @@ public class Mustache } 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( "Invalid tag name: contains newline '" + tag + "'", line); } diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index bba2bf0..d57024f 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -447,6 +447,25 @@ public class MustacheTest "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 () { // 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" +