Skip to content

Commit

Permalink
Merge pull request #6433 from jekyll/potential-utf8-bom-fix-parkr
Browse files Browse the repository at this point in the history
Fix BOM-related read problems
  • Loading branch information
parkr committed Oct 18, 2017
2 parents c5c4201 + 6d20916 commit 68072c7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/jekyll/site.rb
Expand Up @@ -445,6 +445,7 @@ def configure_include_paths
def configure_file_read_opts
self.file_read_opts = {}
self.file_read_opts[:encoding] = config["encoding"] if config["encoding"]
self.file_read_opts = Jekyll::Utils.merged_file_read_opts(self, {})
end

private
Expand Down
3 changes: 3 additions & 0 deletions lib/jekyll/utils.rb
Expand Up @@ -301,6 +301,9 @@ def safe_glob(dir, patterns, flags = 0)
# and a given param
def merged_file_read_opts(site, opts)
merged = (site ? site.file_read_opts : {}).merge(opts)
if merged[:encoding] && !merged[:encoding].start_with?("bom|")
merged[:encoding] = "bom|#{merged[:encoding]}"
end
if merged["encoding"] && !merged["encoding"].start_with?("bom|")
merged["encoding"] = "bom|#{merged["encoding"]}"
end
Expand Down
13 changes: 9 additions & 4 deletions test/test_utils.rb
Expand Up @@ -386,16 +386,21 @@ class TestUtils < JekyllUnitTest
should "ignore encoding if it's not there" do
opts = Utils.merged_file_read_opts(nil, {})
assert_nil opts["encoding"]
assert_nil opts[:encoding]
end

should "add bom to encoding" do
opts = Utils.merged_file_read_opts(nil, { "encoding" => "utf-8" })
assert_equal "bom|utf-8", opts["encoding"]
opts = { "encoding" => "utf-8", :encoding => "utf-8" }
merged = Utils.merged_file_read_opts(nil, opts)
assert_equal "bom|utf-8", merged["encoding"]
assert_equal "bom|utf-8", merged[:encoding]
end

should "preserve bom in encoding" do
opts = Utils.merged_file_read_opts(nil, { "encoding" => "bom|utf-8" })
assert_equal "bom|utf-8", opts["encoding"]
opts = { "encoding" => "bom|another", :encoding => "bom|another" }
merged = Utils.merged_file_read_opts(nil, opts)
assert_equal "bom|another", merged["encoding"]
assert_equal "bom|another", merged[:encoding]
end
end
end

0 comments on commit 68072c7

Please sign in to comment.