Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/erb/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def initialize(trim_mode)
attr_accessor :content

def detect_magic_comment(s, enc = nil)
re = @percent ? /\G(?:<%#(.*)%>|%#(.*)\n)/ : /\G<%#(.*)%>/
re = @percent ? /\G(?:<%#(.*?)%>|%#(.*)\n)/ : /\G<%#(.*?)%>/
frozen = nil
s.scan(re) do
comment = $+
Expand Down
7 changes: 7 additions & 0 deletions test/erb/test_erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,13 @@ def test_frozen_string_literal
end
end

def test_frozen_string_literal_after_encoding_comment
[nil, '%'].each do |trim_mode|
erb = @erb.new("<%# coding: utf-8 %><%# frozen_string_literal: true %><%= ''.frozen? %>", trim_mode: trim_mode)
assert_equal("true", erb.result)
end
end

def test_result_with_hash
erb = @erb.new("<%= foo %>")
assert_equal("1", erb.result_with_hash(foo: "1"))
Expand Down
7 changes: 7 additions & 0 deletions test/erb/test_erb_m17n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def test_recognize_magic_comment
assert_equal Encoding::EUC_KR, erb.result.encoding
end

def test_ignore_magic_comment_after_code
[nil, '%'].each do |trim_mode|
erb = ERB.new("<%# hello %><%= 1 %><%# coding: euc-jp %>", trim_mode: trim_mode)
assert_equal Encoding::UTF_8, erb.encoding
end
end

module M; end
def test_method_with_encoding
obj = Object.new
Expand Down
Loading