Skip to content
This repository has been archived by the owner on Nov 24, 2017. It is now read-only.

Commit

Permalink
Fix invalid byte sequence in UTF-8 for some source files
Browse files Browse the repository at this point in the history
The encoding cannot be guaranteed in the file source read in from
IO.foreach.
  • Loading branch information
ckraybill committed Feb 19, 2013
1 parent df39fcd commit 58a2c9d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/juicer/dependency_resolver/css_dependency_resolver.rb
Expand Up @@ -12,7 +12,7 @@ class CssDependencyResolver < DependencyResolver

private
def parse(line, imported_file = nil)
return $2 if line =~ @@import_pattern
return $2 if encoded_line(line) =~ @@import_pattern

# At first sight of actual CSS rules we abort (TODO: This does not take
# into account the fact that rules may be commented out and that more
Expand Down
8 changes: 8 additions & 0 deletions lib/juicer/dependency_resolver/dependency_resolver.rb
Expand Up @@ -58,6 +58,14 @@ def extension
raise NotImplementedError.new
end

def encoded_line(line)
if String.method_defined?(:encode)
line.encode!('UTF-8', 'UTF-8', :invalid => :replace)
else
line
end
end

#
# Carries out the actual work of resolve. resolve resets the internal
# file list and yields control to _resolve for rebuilding the file list.
Expand Down
Expand Up @@ -9,7 +9,7 @@ class JavaScriptDependencyResolver < DependencyResolver

private
def parse(line, imported_file = nil)
return $1 if line =~ @@depends_pattern
return $1 if encoded_line(line) =~ @@depends_pattern

# If we have already skimmed through some @depend/@depends or a
# closing comment we're done.
Expand Down
@@ -1,3 +1,4 @@
# encoding: utf-8
require "test_helper"

class TestCssDependencyResolver < Test::Unit::TestCase
Expand Down Expand Up @@ -33,4 +34,12 @@ def test_load_order
files = @resolver.resolve(path("a1.css")).collect { |file| file.split("/").pop }
assert_equal "d1.cssb1.cssc1.cssa1.css", files.join
end

def test_parse
text = "© Dynamic Drive"
text.force_encoding("us-ascii") if RUBY_VERSION > "1.9"
assert_nothing_raised ArgumentError do
@resolver.send(:parse, text)
end
end
end
@@ -1,3 +1,4 @@
# encoding: utf-8
require "test_helper"

class TestJavaScriptDependencyResolver < Test::Unit::TestCase
Expand Down Expand Up @@ -47,4 +48,12 @@ def test_directory_resolve
actual_files = @resolver.resolve(my_app)
assert_equal expected_files, actual_files
end

def test_parse
text = "© Dynamic Drive"
text.force_encoding("us-ascii") if RUBY_VERSION > "1.9"
assert_nothing_raised ArgumentError do
@resolver.send(:parse, text)
end
end
end

0 comments on commit 58a2c9d

Please sign in to comment.