Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

remove newlines and whitespaces between html tags #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/ejs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -57,6 +58,11 @@ def evaluate(template, locals = {}, options = {})
end

protected
def remove_html_trailing_whitespaces!(source)
source.gsub!(/>[[:space:]]*$[[:space:]]*</im, '><')
source
end

def js_escape!(source)
source.gsub!(JS_ESCAPE_PATTERN) { |match| '\\' + JS_ESCAPES[match] }
source
Expand Down
5 changes: 5 additions & 0 deletions test/test_ejs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<div>\t \t \n\t<div class=\"test\">\t\t\n\t < span ><strong> <%= x %> </strong ></span> \t\t\n\t <% if(y) { %>\n\t <span>that</span> text spaces and \n multilines \n\t\n should be kept \t\n\t intact\n\t <% } %> \t\t\n </div>\t\t\n\</div>\n"
assert_equal "<div><div class=\"test\">< span ><strong> this </strong ></span><span>that</span> text spaces and \n multilines \n\t\n should be kept \t\n\t intact\n\t </div></div>\n", EJS.evaluate(template, :x => "this", :y => true)
end


test "braced iteration" do
template = "<ul>{{ for (var i = 0; i < people.length; i++) { }}<li>{{= people[i] }}</li>{{ } }}</ul>"
Expand Down