Skip to content

Commit b234afb

Browse files
authored
Fix empty parse control value (#22)
* Fix empty parse control value * Add tests for failing control matching
1 parent b2c2138 commit b234afb

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

lib/ruby-rtf/parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def parse_control(src, current_pos = 0)
104104

105105
contents = src[start, current_pos - start]
106106
m = contents.match(/([\*a-z]+)(\-?\d+)?\*?/)
107-
ctrl = m[1].to_sym
108-
val = m[2].to_i unless m[2].nil?
107+
ctrl = m[1].to_sym unless m.nil? || m[1].nil?
108+
val = m[2].to_i unless m.nil? || m[2].nil?
109109

110110
# we advance past the optional space if present
111111
current_pos += 1 if src[current_pos] == ' '

spec/parser_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@
172172
section[1][:modifiers].has_key?(:underline).should == false
173173
section[1][:text].should == 'World'
174174
end
175+
176+
it 'parses text when control matching fails' do
177+
src = '{\rtf1 Hello\~{World}}'
178+
section = parser.parse(src).sections
179+
section[0][:text].should == 'Hello'
180+
section[1][:text].should == 'World'
181+
end
175182
end
176183

177184
context '#parse_control' do
@@ -232,6 +239,10 @@
232239
it 'advances the current positon past the optional space' do
233240
parser.parse_control('Test ansi test', 5).last.should == 10
234241
end
242+
243+
it 'does not fail when control matching fails' do
244+
parser.parse_control('~}')[0, 2].should == ['', nil]
245+
end
235246
end
236247

237248
context 'character set' do

0 commit comments

Comments
 (0)