Skip to content

Commit 79b2c97

Browse files
authored
Support UTF-8 characters in ERB blocks when pretty printing AST (#93)
Closes #92
1 parent 66b5789 commit 79b2c97

6 files changed

+78
-1
lines changed

β€Žlib/herb/token.rbβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def to_json(*args)
2525
end
2626

2727
def tree_inspect
28-
%("#{value}" #{location.tree_inspect})
28+
%("#{value.force_encoding("utf-8")}" #{location.tree_inspect})
2929
end
3030

3131
def value_inspect

β€Žtest/parser/utf8_test.rbβ€Ž

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
module Parser
6+
class UTF8Test < Minitest::Spec
7+
include SnapshotUtils
8+
9+
test "opening guillemet" do
10+
assert_parsed_snapshot(<<~ERB)
11+
<%= link_to 'Β«', url %>
12+
ERB
13+
end
14+
15+
test "closing guillemet" do
16+
assert_parsed_snapshot(<<~ERB)
17+
<%= link_to 'Β»', url %>
18+
ERB
19+
end
20+
21+
test "single opening guillemet" do
22+
assert_parsed_snapshot(<<~ERB)
23+
<%= link_to 'β€Ή', url %>
24+
ERB
25+
end
26+
27+
test "single closing guillemet" do
28+
assert_parsed_snapshot(<<~ERB)
29+
<%= link_to 'β€Ί', url %>
30+
ERB
31+
end
32+
end
33+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@ DocumentNode (location: (1:0)-(2:0))
2+
└── children: (2 items)
3+
β”œβ”€β”€ @ ERBContentNode (location: (1:0)-(1:24))
4+
β”‚ β”œβ”€β”€ tag_opening: "<%=" (location: (1:0)-(1:3))
5+
β”‚ β”œβ”€β”€ content: " link_to 'Β«', url " (location: (1:3)-(1:22))
6+
β”‚ β”œβ”€β”€ tag_closing: "%>" (location: (1:22)-(1:24))
7+
β”‚ β”œβ”€β”€ parsed: true
8+
β”‚ └── valid: true
9+
β”‚
10+
└── @ HTMLTextNode (location: (1:24)-(2:0))
11+
└── content: "\n"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@ DocumentNode (location: (1:0)-(2:0))
2+
└── children: (2 items)
3+
β”œβ”€β”€ @ ERBContentNode (location: (1:0)-(1:24))
4+
β”‚ β”œβ”€β”€ tag_opening: "<%=" (location: (1:0)-(1:3))
5+
β”‚ β”œβ”€β”€ content: " link_to 'Β»', url " (location: (1:3)-(1:22))
6+
β”‚ β”œβ”€β”€ tag_closing: "%>" (location: (1:22)-(1:24))
7+
β”‚ β”œβ”€β”€ parsed: true
8+
β”‚ └── valid: true
9+
β”‚
10+
└── @ HTMLTextNode (location: (1:24)-(2:0))
11+
└── content: "\n"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@ DocumentNode (location: (1:0)-(2:0))
2+
└── children: (2 items)
3+
β”œβ”€β”€ @ ERBContentNode (location: (1:0)-(1:25))
4+
β”‚ β”œβ”€β”€ tag_opening: "<%=" (location: (1:0)-(1:3))
5+
β”‚ β”œβ”€β”€ content: " link_to 'β€Ή', url " (location: (1:3)-(1:23))
6+
β”‚ β”œβ”€β”€ tag_closing: "%>" (location: (1:23)-(1:25))
7+
β”‚ β”œβ”€β”€ parsed: true
8+
β”‚ └── valid: true
9+
β”‚
10+
└── @ HTMLTextNode (location: (1:25)-(2:0))
11+
└── content: "\n"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@ DocumentNode (location: (1:0)-(2:0))
2+
└── children: (2 items)
3+
β”œβ”€β”€ @ ERBContentNode (location: (1:0)-(1:25))
4+
β”‚ β”œβ”€β”€ tag_opening: "<%=" (location: (1:0)-(1:3))
5+
β”‚ β”œβ”€β”€ content: " link_to 'β€Ί', url " (location: (1:3)-(1:23))
6+
β”‚ β”œβ”€β”€ tag_closing: "%>" (location: (1:23)-(1:25))
7+
β”‚ β”œβ”€β”€ parsed: true
8+
β”‚ └── valid: true
9+
β”‚
10+
└── @ HTMLTextNode (location: (1:25)-(2:0))
11+
└── content: "\n"

0 commit comments

Comments
Β (0)