From 007a209eb36c66c6fbe25ad0adfc287323046440 Mon Sep 17 00:00:00 2001 From: Fabio Trabucchi Date: Tue, 12 Apr 2016 09:31:37 -0500 Subject: [PATCH] remove newlines and whitespaces between html tags --- lib/ejs.rb | 6 ++++++ test/test_ejs.rb | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/ejs.rb b/lib/ejs.rb index 686649a..7a87754 100644 --- a/lib/ejs.rb +++ b/lib/ejs.rb @@ -34,6 +34,7 @@ class << self def compile(source, options = {}) source = source.dup + remove_html_trailing_whitespaces!(source) js_escape!(source) replace_escape_tags!(source, options) replace_interpolation_tags!(source, options) @@ -57,6 +58,11 @@ def evaluate(template, locals = {}, options = {}) end protected + def remove_html_trailing_whitespaces!(source) + source.gsub!(/>[[:space:]]*$[[:space:]]*<') + source + end + def js_escape!(source) source.gsub!(JS_ESCAPE_PATTERN) { |match| '\\' + JS_ESCAPES[match] } source diff --git a/test/test_ejs.rb b/test/test_ejs.rb index ed760da..fba4dd1 100644 --- a/test/test_ejs.rb +++ b/test/test_ejs.rb @@ -122,6 +122,11 @@ class EJSEvaluationTest < Test::Unit::TestCase assert_equal "This\n\t\tis: that.\n\tok.\nend.", EJS.evaluate(template, :x => "that") end + test "newlines and whitespaces between html tags" do + template = "
\t \t \n\t
\t\t\n\t < span > <%= x %> \t\t\n\t <% if(y) { %>\n\t that text spaces and \n multilines \n\t\n should be kept \t\n\t intact\n\t <% } %> \t\t\n
\t\t\n\
\n" + assert_equal "
< span > this that text spaces and \n multilines \n\t\n should be kept \t\n\t intact\n\t
\n", EJS.evaluate(template, :x => "this", :y => true) + end + test "braced iteration" do template = ""