Skip to content

Commit 58b2d73

Browse files
committed
In ERB::Util.tokenize, return :PLAIN when line has no ERB
Previous behavior was to raise. Allows caller to decide what to do with lines that are plain. Error highlighter assumes PLAIN lines must be code otherwise they wouldn't be in the backtrace.
1 parent 1f9295e commit 58b2d73

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

actionview/lib/action_view/template/handlers/erb.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ def find_offset(compiled, source_tokens, error_column)
152152

153153
def offset_source_tokens(source_tokens)
154154
source_offset = 0
155-
with_offset = source_tokens.filter_map do |(name, str)|
156-
result = [name, str, source_offset] if name == :CODE || name == :TEXT
155+
with_offset = source_tokens.filter_map do |name, str|
156+
result = [:CODE, str, source_offset] if name == :CODE || name == :PLAIN
157+
result = [:TEXT, str, source_offset] if name == :TEXT
157158
source_offset += str.bytesize
158159
result
159160
end

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Alter `ERB::Util.tokenize` to return :PLAIN token with full input string when string doesn't contain ERB tags.
2+
3+
*Martin Emde*
4+
15
* Fix a bug in `ERB::Util.tokenize` that causes incorrect tokenization when ERB tags are preceeded by multibyte characters.
26

37
*Martin Emde*

activesupport/lib/active_support/core_ext/erb/util.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def self.tokenize(source) # :nodoc:
169169
while !source.eos?
170170
pos = source.pos
171171
source.scan_until(/(?:#{start_re}|#{finish_re})/)
172-
raise NotImplementedError if source.matched.nil?
172+
return [[:PLAIN, source.string]] unless source.matched?
173173
len = source.pos - source.matched.bytesize - pos
174174

175175
case source.matched

activesupport/test/core_ext/erb_util_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ def test_text_end
135135
], actual_tokens
136136
end
137137

138+
# This happens when a template is multiline and no
139+
# ERB tags are used on the current line.
140+
def test_plain_without_tags
141+
source = " @post.title\n"
142+
actual_tokens = tokenize source
143+
assert_equal [[:PLAIN, " @post.title"]], actual_tokens
144+
end
145+
138146
def test_multibyte_characters_start
139147
source = "こんにちは<%= name %>"
140148
actual_tokens = tokenize source

0 commit comments

Comments
 (0)