Skip to content
Browse files

Make the DirectiveProcessor encoding aware

In Ruby 1.9, the directive_processor.rb source file defaults to US-ASCII
as it doesn't contain a magic comment, meaning the String initialised in
@result gets a US-ASCII encoding.

This means that when content is concatenated onto @result (e.g. in
process_source), Ruby will maintain the US-ASCII encoding, provided the
content being concatenated contains no non-ascii chars.

The changes in this commit force the encoding of the resulting string to
be that of the body, because conceptually that is the string we are
"concatenating" with.
  • Loading branch information...
1 parent 18fca5c commit 83b5f115ab2173190edb2671d35d318a1d0d7440 Lewis Marshall committed Mar 10, 2013
Showing with 16 additions and 0 deletions.
  1. +2 −0 lib/sprockets/directive_processor.rb
  2. +1 −0 test/fixtures/context/utf8.js.erb
  3. +13 −0 test/test_directive_processor.rb
View
2 lib/sprockets/directive_processor.rb
@@ -92,6 +92,8 @@ def evaluate(context, locals, &block)
@context = context
@result = ""
+ @result.force_encoding(body.encoding) if body.respond_to?(:encoding)
+
@has_written_body = false
process_directives
View
1 test/fixtures/context/utf8.js.erb
@@ -0,0 +1 @@
+console.log("Snowman: <%= "\xe2\x98\x83" %>")
View
13 test/test_directive_processor.rb
@@ -108,6 +108,19 @@ def directives(offset = 0)
end
end
+if "".respond_to?(:encoding)
+ class TestDirectiveProcessorEncoding < Sprockets::TestCase
+ def setup
+ @env = Sprockets::Environment.new
+ @env.append_path(fixture_path('context'))
+ end
+
+ test "encoding of evaluated string" do
+ assert_equal Encoding::UTF_8, @env['utf8.js'].source.encoding
+ end
+ end
+end
+
class TestCustomDirectiveProcessor < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new

0 comments on commit 83b5f11

Please sign in to comment.
Something went wrong with that request. Please try again.