Skip to content

Commit

Permalink
fix(intl-messageformat-parser): Escape double-' to a single ' (#103)
Browse files Browse the repository at this point in the history
* fix(intl-messageformat-parser): Escape double-apostrophes to a single apostrophe

* rm unnecessary rule
  • Loading branch information
longlho committed Apr 26, 2020
1 parent a1af97b commit f68ad8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ messageTextElement

argument
= number
/ $([^ \t\n\r,.+={}#]+)

/ chars:quoteEscapedChar* { return chars.join(''); }
argumentElement
= '{' _ id:argument _ format:(',' _ elementFormat)? _ '}' {
return {
Expand Down Expand Up @@ -145,8 +145,17 @@ number = digits:('0' / $([1-9] digit*)) {
return parseInt(digits, 10);
}

quoteEscapedChar =
!("'" / [ \t\n\r,.+={}#]) char:. { return char; }
/ "'" sequence:escape { return sequence; }

apostrophe = "'"
escape = [ \t\n\r,.+={}#] / apostrophe

char
= [^{}\\\0-\x1F\x7f \t\n\r]
=
"'" sequence:apostrophe { return sequence; }
/ [^{}\\\0-\x1F\x7f \t\n\r]
/ '\\\\' { return '\\'; }
/ '\\#' { return '\\#'; }
/ '\\{' { return '\u007B'; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,21 @@ describe('parse()', function() {
'\\'
);
});

/**
* @see http://userguide.icu-project.org/formatparse/messages#TOC-Quoting-Escaping
* @see https://github.com/formatjs/formatjs/issues/97
*/
it('should escape a pair of ASCII apostrophes to represent one ASCII apostrophe', function() {
expect(
(parse("This '{isn''t}' obvious").elements[1] as ArgumentElement).id
).to.equal("isn't");
expect(
(parse("''{name}''").elements[0] as MessageTextElement).value
).to.equal("'");
expect(
(parse("''{name}''").elements[2] as MessageTextElement).value
).to.equal("'");
});
});
});

0 comments on commit f68ad8f

Please sign in to comment.