MCPcopy
hub / github.com/formatjs/formatjs / tryParseQuote

Method tryParseQuote

packages/icu-messageformat-parser/parser.ts:453–505  ·  view source on GitHub ↗

* Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes * a character that requires quoting (that is, "only where needed"), and works the same in * nested messages as on the top level of the pattern. The new behavior is otherwise compatible.

(parentArgType: ArgType)

Source from the content-addressed store, hash-verified

451 * nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
452 */
453 private tryParseQuote(parentArgType: ArgType): string | null {
454 if (this.isEOF() || this.char() !== 39 /* `'` */) {
455 return null
456 }
457
458 // Parse escaped char following the apostrophe, or early return if there is no escaped char.
459 // Check if is valid escaped character
460 switch (this.peek()) {
461 case 39 /* `'` */:
462 // double quote, should return as a single quote.
463 this.bump()
464 this.bump()
465 return "'"
466 // '{', '<', '>', '}'
467 case 123:
468 case 60:
469 case 62:
470 case 125:
471 break
472 case 35: // '#'
473 if (parentArgType === 'plural' || parentArgType === 'selectordinal') {
474 break
475 }
476 return null
477 default:
478 return null
479 }
480
481 this.bump() // apostrophe
482 const codePoints = [this.char()] // escaped char
483 this.bump()
484
485 // read chars until the optional closing apostrophe is found
486 while (!this.isEOF()) {
487 const ch = this.char()
488 if (ch === 39 /* `'` */) {
489 if (this.peek() === 39 /* `'` */) {
490 codePoints.push(39)
491 // Bump one more time because we need to skip 2 characters.
492 this.bump()
493 } else {
494 // Optional closing apostrophe.
495 this.bump()
496 break
497 }
498 } else {
499 codePoints.push(ch)
500 }
501 this.bump()
502 }
503
504 return String.fromCodePoint(...codePoints)
505 }
506
507 private tryParseUnquoted(
508 nestingLevel: number,

Callers 1

parseLiteralMethod · 0.95

Calls 4

isEOFMethod · 0.95
charMethod · 0.95
peekMethod · 0.95
bumpMethod · 0.95

Tested by

no test coverage detected