Skip to content

Commit d32e3a9

Browse files
authored
Merge pull request #68 from jondkinney/feature/support-unicode-chars
Allow unicode chars, force_encoding('UTF-8')
2 parents 0fb12c3 + 7a80fa8 commit d32e3a9

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ jobs:
2525
run: bin/setup
2626
- name: Run test
2727
run: "bundle exec rake"
28+
- name: Run test
29+
run: "env LANG=C LC_ALL=C bundle exec rake"

lib/erb/formatter.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ class Error < StandardError; end
2929
SPACES = /\s+/m
3030

3131
# https://stackoverflow.com/a/317081
32-
ATTR_NAME = %r{[^\r\n\t\f\v= '"<>]*[^\r\n\t\f\v= '"<>/]} # not ending with a slash
33-
UNQUOTED_VALUE = %r{[^<>'"\s]+}
34-
UNQUOTED_ATTR = %r{#{ATTR_NAME}=#{UNQUOTED_VALUE}}
35-
SINGLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}='[^']*?')}m
36-
DOUBLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}="[^"]*?")}m
37-
BAD_ATTR = %r{#{ATTR_NAME}=\s+}
32+
ATTR_NAME = %r{[^\r\n\t\f\v= '"<>]*[^\r\n\t\f\v= '"<>/]}u # not ending with a slash
33+
UNQUOTED_VALUE = %r{[^<>'"\s]+}u
34+
UNQUOTED_ATTR = %r{#{ATTR_NAME}=#{UNQUOTED_VALUE}}u
35+
SINGLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}='[^']*?')}mu
36+
DOUBLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}="[^"]*?")}mu
37+
BAD_ATTR = %r{#{ATTR_NAME}=\s+}u
3838
QUOTED_ATTR = Regexp.union(SINGLE_QUOTE_ATTR, DOUBLE_QUOTE_ATTR)
3939
ATTR = Regexp.union(SINGLE_QUOTE_ATTR, DOUBLE_QUOTE_ATTR, UNQUOTED_ATTR, UNQUOTED_VALUE)
4040
MULTILINE_ATTR_NAMES = %w[class data-action]
4141

4242
ERB_TAG = %r{(<%(?:==|=|-|))\s*(.*?)\s*(-?%>)}m
4343
ERB_PLACEHOLDER = %r{erb[a-z0-9]+tag}
4444

45-
TAG_NAME = /[a-z0-9_:-]+/
45+
TAG_NAME = /[a-z0-9_:-]+/u
4646
TAG_NAME_ONLY = /\A#{TAG_NAME}\z/
4747
HTML_ATTR = %r{\s+#{SINGLE_QUOTE_ATTR}|\s+#{DOUBLE_QUOTE_ATTR}|\s+#{UNQUOTED_ATTR}|\s+#{ATTR_NAME}}m
4848
HTML_TAG_OPEN = %r{<(#{TAG_NAME})((?:#{HTML_ATTR})*)(\s*?)(/>|>)}m
@@ -79,11 +79,14 @@ def self.format(source, filename: nil)
7979
end
8080

8181
def initialize(source, line_width: 80, single_class_per_line: false, filename: nil, css_class_sorter: nil, debug: $DEBUG)
82-
@original_source = source
82+
@original_source = source.to_s
83+
@original_source = +@original_source if @original_source.frozen?
84+
@original_source.force_encoding('UTF-8')
85+
8386
@filename = filename || '(erb)'
8487
@line_width = line_width
85-
@source = remove_front_matter source.dup
86-
@html = +""
88+
@source = remove_front_matter @original_source.dup
89+
@html = +"".force_encoding('UTF-8')
8790
@debug = debug
8891
@single_class_per_line = single_class_per_line
8992
@css_class_sorter = css_class_sorter

test/erb/test_formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def test_simple_tag
1212
end
1313

1414
def test_fixtures
15-
Dir["#{__dir__}/../fixtures/*.html.erb"].each do |erb_path|
15+
Dir["#{__dir__}/../fixtures/*.html.erb"].shuffle.each do |erb_path|
1616
expected_path = erb_path.chomp('.erb') + '.expected.erb'
1717

1818
# File.write expected_path, ERB::Formatter.format(File.read(erb_path))
19-
assert_equal(File.read(expected_path), ERB::Formatter.new(File.read(erb_path)).to_s, "Formatting of #{erb_path} failed")
19+
assert_equal(File.read(expected_path, encoding: "UTF-8"), ERB::Formatter.new(File.read(erb_path)).to_s, "Formatting of #{erb_path} failed")
2020
end
2121
end
2222

test/fixtures/utf8.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h3>Hi <%= @merchant.first_name %></h3>
1+
<h3>Hi <%= @merchant.first_name %> 🙌 ↯</h3>
22
<h1>SOS! Deine PEND-Anbindung wurde SUSPENDIERT!</h1>
33
<p>Die PEND-Anbindung von <%= @gateway.title %> auf deinem Shop <%= @shop.name %> wurde suspendiert!</p>
44

@@ -16,4 +16,7 @@
1616
<%= form_with model: model, url: path, method: :patch do |f| %>
1717
<%= f.select :type, [["не выбрано", nil], %w[Тип1 type1], %w[Тип2 type2], %w[Тип3 type3], %w[Тип4 type4], %w[Тип5 type5]], {}, { class: "select" } %>
1818
<%= f.submit "Сохранить" %>
19+
The dash: more—effortlessly
20+
The copyright: ©
21+
The quote: Figma and Tailwind ” are used
1922
<% end %>

test/fixtures/utf8.html.expected.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<h3>Hi
2-
<%= @merchant.first_name %></h3>
2+
<%= @merchant.first_name %>
3+
🙌 ↯</h3>
34
<h1>SOS! Deine PEND-Anbindung wurde SUSPENDIERT!</h1>
45
<p>Die PEND-Anbindung von
56
<%= @gateway.title %>
@@ -38,4 +39,6 @@
3839
{},
3940
{ class: "select" } %>
4041
<%= f.submit "Сохранить" %>
42+
The dash: more—effortlessly The copyright: © The quote: Figma and Tailwind ”
43+
are used
4144
<% end %>

0 commit comments

Comments
 (0)