From cf9031c76ffad93ac5358b66480ed41c53868820 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Sat, 14 Jan 2012 10:38:40 -0800 Subject: [PATCH] Preserve empty newlines --- mustache.js | 7 +++++-- spec/_files/recursion_with_same_names.txt | 1 + spec/_files/whitespace.js | 4 ++++ spec/_files/whitespace.mustache | 4 ++++ spec/_files/whitespace.txt | 4 ++++ 5 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 spec/_files/whitespace.js create mode 100644 spec/_files/whitespace.mustache create mode 100644 spec/_files/whitespace.txt diff --git a/mustache.js b/mustache.js index 29e8053..fff1b67 100644 --- a/mustache.js +++ b/mustache.js @@ -216,13 +216,14 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {}; '\nsend("' ]; - var spaces = [], // indices of whitespace in code for the current line + var spaces = [], // indices of whitespace in code on the current line + hasTag = false, // is there a {{tag}} on the current line? nonSpace = false; // is there a non-space char on the current line? // Strips all space characters from the code array for the current line // if there was a {{tag}} on it and otherwise only spaces. var stripSpace = function () { - if (!nonSpace && !options.space) { + if (hasTag && !nonSpace && !options.space) { while (spaces.length) { code.splice(spaces.pop(), 1); } @@ -230,6 +231,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {}; spaces = []; } + hasTag = false; nonSpace = false; }; @@ -330,6 +332,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {}; updateLine = '\nline = ' + line + ';'; nextOpenTag = openTag; nextCloseTag = closeTag; + hasTag = true; switch (c) { case "!": // comment diff --git a/spec/_files/recursion_with_same_names.txt b/spec/_files/recursion_with_same_names.txt index c23bb65..cb15d75 100644 --- a/spec/_files/recursion_with_same_names.txt +++ b/spec/_files/recursion_with_same_names.txt @@ -1,5 +1,6 @@ name desc + t1 0 t2 diff --git a/spec/_files/whitespace.js b/spec/_files/whitespace.js new file mode 100644 index 0000000..97f53c1 --- /dev/null +++ b/spec/_files/whitespace.js @@ -0,0 +1,4 @@ +var whitespace = { + tag1: "Hello", + tag2: "World" +}; diff --git a/spec/_files/whitespace.mustache b/spec/_files/whitespace.mustache new file mode 100644 index 0000000..aa76e08 --- /dev/null +++ b/spec/_files/whitespace.mustache @@ -0,0 +1,4 @@ +{{tag1}} + + +{{tag2}}. diff --git a/spec/_files/whitespace.txt b/spec/_files/whitespace.txt new file mode 100644 index 0000000..851fa74 --- /dev/null +++ b/spec/_files/whitespace.txt @@ -0,0 +1,4 @@ +Hello + + +World.