diff --git a/lib/message_format/parser.rb b/lib/message_format/parser.rb index 0414832..764d195 100644 --- a/lib/message_format/parser.rb +++ b/lib/message_format/parser.rb @@ -94,6 +94,7 @@ def parse_text ( parent_type ) (is_arg_style and is_whitespace(char)) ) text << char + found_closing_quote = false while @index + 1 < @length @index += 1 char = @pattern[@index] @@ -102,11 +103,17 @@ def parse_text ( parent_type ) @index += 1 elsif char == '\'' # end of quoted @index += 1 + found_closing_quote = true break else text << char end end + # If no closing quote was found, increment past the last character + # to avoid reprocessing it in the outer loop + if !found_closing_quote + @index += 1 + end else # lone ' is just a ' text << '\'' # already incremented diff --git a/spec/message_format_spec.rb b/spec/message_format_spec.rb index ca559da..2207352 100644 --- a/spec/message_format_spec.rb +++ b/spec/message_format_spec.rb @@ -33,6 +33,13 @@ expect(message).to eql('This isn\'t a {\'simple\'} \'string\'') end + it 'handles escaped single apostrophe escapes' do + pattern = 'Hello \'{literal}!' + message = MessageFormat.new(pattern, 'en-US').format() + + expect(message).to eql('Hello {literal}!') + end + it 'accepts arguments' do pattern = 'x{ arg }z' message = MessageFormat.new(pattern, 'en-US').format({ :arg => 'y' })