Skip to content

Commit 16ca175

Browse files
committed
Don't call add_text with nil
Calling add_text with nil breaks the Rails' subclass of Erubi. So this reverts many of the changes in 4dc81c2.
1 parent c78f722 commit 16ca175

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/erubi.rb

+15-14
Original file line numberDiff line numberDiff line change
@@ -134,31 +134,31 @@ def initialize(input, properties={})
134134
end
135135

136136
is_bol = rspace
137-
add_text(text)
137+
add_text(text) if text
138138
case ch
139139
when '='
140140
rspace = nil if tailch && !tailch.empty?
141-
add_text(lspace)
141+
add_text(lspace) if lspace
142142
add_expression(indicator, code)
143-
add_text(rspace)
143+
add_text(rspace) if rspace
144144
when '#'
145145
n = code.count("\n") + (rspace ? 1 : 0)
146146
if trim && lspace && rspace
147147
add_code("\n" * n)
148148
else
149-
add_text(lspace)
149+
add_text(lspace) if lspace
150150
add_code("\n" * n)
151-
add_text(rspace)
151+
add_text(rspace) if rspace
152152
end
153153
when '%'
154154
add_text("#{lspace}#{literal_prefix}#{code}#{tailch}#{literal_postfix}#{rspace}")
155155
when nil, '-'
156156
if trim && lspace && rspace
157157
add_code("#{lspace}#{code}#{rspace}")
158158
else
159-
add_text(lspace)
159+
add_text(lspace) if lspace
160160
add_code(code)
161-
add_text(rspace)
161+
add_text(rspace) if rspace
162162
end
163163
else
164164
handle(indicator, code, tailch, rspace, lspace)
@@ -177,15 +177,16 @@ def initialize(input, properties={})
177177
private
178178

179179
# Add raw text to the template. Modifies argument if argument is mutable as a memory optimization.
180+
# Must be called with a string, cannot be called with nil (Rails's subclass depends on it).
180181
def add_text(text)
181-
if text && !text.empty?
182-
if text.frozen?
183-
text = text.gsub(/['\\]/, '\\\\\&')
184-
else
185-
text.gsub!(/['\\]/, '\\\\\&')
186-
end
187-
@src << " " << @bufvar << " << '" << text << TEXT_END
182+
return if text.empty?
183+
184+
if text.frozen?
185+
text = text.gsub(/['\\]/, '\\\\\&')
186+
else
187+
text.gsub!(/['\\]/, '\\\\\&')
188188
end
189+
@src << " " << @bufvar << " << '" << text << TEXT_END
189190
end
190191

191192
# Add ruby code to the template

0 commit comments

Comments
 (0)