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

Commit

Permalink
Trim trailing whitespace in Sprockets::SourceLine#to_s
Browse files Browse the repository at this point in the history
  • Loading branch information
sstephenson committed Feb 16, 2009
1 parent 7fc1208 commit 95aad14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/sprockets/source_line.rb
Expand Up @@ -57,14 +57,26 @@ def inspect
end

def to_s(constants = source_file.environment.constants)
line.chomp.gsub(/<%=(.*?)%>/) do
constant = $1.strip
if value = constants[constant]
value
else
raise UndefinedConstantError, "couldn't find constant `#{constant}' in #{inspect}"
end
end + $/
result = line.chomp
interpolate_constants!(result, constants)
strip_trailing_whitespace!(result)
result + $/
end

protected
def interpolate_constants!(result, constants)
result.gsub!(/<%=(.*?)%>/) do
constant = $1.strip
if value = constants[constant]
value
else
raise UndefinedConstantError, "couldn't find constant `#{constant}' in #{inspect}"
end
end
end

def strip_trailing_whitespace!(result)
result.gsub!(/\s+$/, "")
end
end
end
4 changes: 4 additions & 0 deletions test/test_source_line.rb
Expand Up @@ -82,4 +82,8 @@ def test_interpolation_of_missing_constant_raises_undefined_constant_error
source_line('<%= NONEXISTENT %>').to_s("VERSION" => "1.0")
end
end

def test_to_s_should_strip_trailing_whitespace_before_adding_line_ending
assert_equal "hello();\n", source_line("hello(); \t \r\n").to_s({})
end
end

0 comments on commit 95aad14

Please sign in to comment.