/usr/share/javascript/angular.js/angular-message-format.js is in libjs-angularjs 1.5.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 | /**
* @license AngularJS v1.5.10
* (c) 2010-2016 Google, Inc. http://angularjs.org
* License: MIT
*/
(function(window, angular) {
'use strict';
// NOTE: ADVANCED_OPTIMIZATIONS mode.
//
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
// constructs incompatible with that mode.
/* global isFunction: false */
/* global noop: false */
/* global toJson: false */
function stringify(value) {
if (value == null /* null/undefined */) { return ''; }
switch (typeof value) {
case 'string': return value;
case 'number': return '' + value;
default: return toJson(value);
}
}
// Convert an index into the string into line/column for use in error messages
// As such, this doesn't have to be efficient.
function indexToLineAndColumn(text, index) {
var lines = text.split(/\n/g);
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (index >= line.length) {
index -= line.length;
} else {
return { line: i + 1, column: index + 1 };
}
}
}
var PARSE_CACHE_FOR_TEXT_LITERALS = Object.create(null);
function parseTextLiteral(text) {
var cachedFn = PARSE_CACHE_FOR_TEXT_LITERALS[text];
if (cachedFn != null) {
return cachedFn;
}
function parsedFn(context) { return text; }
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
var unwatch = scope['$watch'](noop,
function textLiteralWatcher() {
if (isFunction(listener)) { listener(text, text, scope); }
unwatch();
},
objectEquality);
return unwatch;
};
PARSE_CACHE_FOR_TEXT_LITERALS[text] = parsedFn;
parsedFn['exp'] = text; // Needed to pretend to be $interpolate for tests copied from interpolateSpec.js
parsedFn['expressions'] = []; // Require this to call $compile.$$addBindingInfo() which allows Protractor to find elements by binding.
return parsedFn;
}
function subtractOffset(expressionFn, offset) {
if (offset === 0) {
return expressionFn;
}
function minusOffset(value) {
return (value == null) ? value : value - offset;
}
function parsedFn(context) { return minusOffset(expressionFn(context)); }
var unwatch;
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
unwatch = scope['$watch'](expressionFn,
function pluralExpressionWatchListener(newValue, oldValue) {
if (isFunction(listener)) { listener(minusOffset(newValue), minusOffset(oldValue), scope); }
},
objectEquality);
return unwatch;
};
return parsedFn;
}
// NOTE: ADVANCED_OPTIMIZATIONS mode.
//
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
// constructs incompatible with that mode.
/* global $interpolateMinErr: false */
/* global isFunction: false */
/* global noop: false */
/**
* @constructor
* @private
*/
function MessageSelectorBase(expressionFn, choices) {
var self = this;
this.expressionFn = expressionFn;
this.choices = choices;
if (choices['other'] === undefined) {
throw $interpolateMinErr('reqother', '“other” is a required option.');
}
this.parsedFn = function(context) { return self.getResult(context); };
this.parsedFn['$$watchDelegate'] = function $$watchDelegate(scope, listener, objectEquality) {
return self.watchDelegate(scope, listener, objectEquality);
};
this.parsedFn['exp'] = expressionFn['exp'];
this.parsedFn['expressions'] = expressionFn['expressions'];
}
MessageSelectorBase.prototype.getMessageFn = function getMessageFn(value) {
return this.choices[this.categorizeValue(value)];
};
MessageSelectorBase.prototype.getResult = function getResult(context) {
return this.getMessageFn(this.expressionFn(context))(context);
};
MessageSelectorBase.prototype.watchDelegate = function watchDelegate(scope, listener, objectEquality) {
var watchers = new MessageSelectorWatchers(this, scope, listener, objectEquality);
return function() { watchers.cancelWatch(); };
};
/**
* @constructor
* @private
*/
function MessageSelectorWatchers(msgSelector, scope, listener, objectEquality) {
var self = this;
this.scope = scope;
this.msgSelector = msgSelector;
this.listener = listener;
this.objectEquality = objectEquality;
this.lastMessage = undefined;
this.messageFnWatcher = noop;
var expressionFnListener = function(newValue, oldValue) { return self.expressionFnListener(newValue, oldValue); };
this.expressionFnWatcher = scope['$watch'](msgSelector.expressionFn, expressionFnListener, objectEquality);
}
MessageSelectorWatchers.prototype.expressionFnListener = function expressionFnListener(newValue, oldValue) {
var self = this;
this.messageFnWatcher();
var messageFnListener = function(newMessage, oldMessage) { return self.messageFnListener(newMessage, oldMessage); };
var messageFn = this.msgSelector.getMessageFn(newValue);
this.messageFnWatcher = this.scope['$watch'](messageFn, messageFnListener, this.objectEquality);
};
MessageSelectorWatchers.prototype.messageFnListener = function messageFnListener(newMessage, oldMessage) {
if (isFunction(this.listener)) {
this.listener.call(null, newMessage, newMessage === oldMessage ? newMessage : this.lastMessage, this.scope);
}
this.lastMessage = newMessage;
};
MessageSelectorWatchers.prototype.cancelWatch = function cancelWatch() {
this.expressionFnWatcher();
this.messageFnWatcher();
};
/**
* @constructor
* @extends MessageSelectorBase
* @private
*/
function SelectMessage(expressionFn, choices) {
MessageSelectorBase.call(this, expressionFn, choices);
}
function SelectMessageProto() {}
SelectMessageProto.prototype = MessageSelectorBase.prototype;
SelectMessage.prototype = new SelectMessageProto();
SelectMessage.prototype.categorizeValue = function categorizeSelectValue(value) {
return (this.choices[value] !== undefined) ? value : 'other';
};
/**
* @constructor
* @extends MessageSelectorBase
* @private
*/
function PluralMessage(expressionFn, choices, offset, pluralCat) {
MessageSelectorBase.call(this, expressionFn, choices);
this.offset = offset;
this.pluralCat = pluralCat;
}
function PluralMessageProto() {}
PluralMessageProto.prototype = MessageSelectorBase.prototype;
PluralMessage.prototype = new PluralMessageProto();
PluralMessage.prototype.categorizeValue = function categorizePluralValue(value) {
if (isNaN(value)) {
return 'other';
} else if (this.choices[value] !== undefined) {
return value;
} else {
var category = this.pluralCat(value - this.offset);
return (this.choices[category] !== undefined) ? category : 'other';
}
};
// NOTE: ADVANCED_OPTIMIZATIONS mode.
//
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
// constructs incompatible with that mode.
/* global $interpolateMinErr: false */
/* global isFunction: false */
/* global parseTextLiteral: false */
/**
* @constructor
* @private
*/
function InterpolationParts(trustedContext, allOrNothing) {
this.trustedContext = trustedContext;
this.allOrNothing = allOrNothing;
this.textParts = [];
this.expressionFns = [];
this.expressionIndices = [];
this.partialText = '';
this.concatParts = null;
}
InterpolationParts.prototype.flushPartialText = function flushPartialText() {
if (this.partialText) {
if (this.concatParts == null) {
this.textParts.push(this.partialText);
} else {
this.textParts.push(this.concatParts.join(''));
this.concatParts = null;
}
this.partialText = '';
}
};
InterpolationParts.prototype.addText = function addText(text) {
if (text.length) {
if (!this.partialText) {
this.partialText = text;
} else if (this.concatParts) {
this.concatParts.push(text);
} else {
this.concatParts = [this.partialText, text];
}
}
};
InterpolationParts.prototype.addExpressionFn = function addExpressionFn(expressionFn) {
this.flushPartialText();
this.expressionIndices.push(this.textParts.length);
this.expressionFns.push(expressionFn);
this.textParts.push('');
};
InterpolationParts.prototype.getExpressionValues = function getExpressionValues(context) {
var expressionValues = new Array(this.expressionFns.length);
for (var i = 0; i < this.expressionFns.length; i++) {
expressionValues[i] = this.expressionFns[i](context);
}
return expressionValues;
};
InterpolationParts.prototype.getResult = function getResult(expressionValues) {
for (var i = 0; i < this.expressionIndices.length; i++) {
var expressionValue = expressionValues[i];
if (this.allOrNothing && expressionValue === undefined) return;
this.textParts[this.expressionIndices[i]] = expressionValue;
}
return this.textParts.join('');
};
InterpolationParts.prototype.toParsedFn = function toParsedFn(mustHaveExpression, originalText) {
var self = this;
this.flushPartialText();
if (mustHaveExpression && this.expressionFns.length === 0) {
return undefined;
}
if (this.textParts.length === 0) {
return parseTextLiteral('');
}
if (this.trustedContext && this.textParts.length > 1) {
$interpolateMinErr['throwNoconcat'](originalText);
}
if (this.expressionFns.length === 0) {
if (this.textParts.length !== 1) { this.errorInParseLogic(); }
return parseTextLiteral(this.textParts[0]);
}
var parsedFn = function(context) {
return self.getResult(self.getExpressionValues(context));
};
parsedFn['$$watchDelegate'] = function $$watchDelegate(scope, listener, objectEquality) {
return self.watchDelegate(scope, listener, objectEquality);
};
parsedFn['exp'] = originalText; // Needed to pretend to be $interpolate for tests copied from interpolateSpec.js
parsedFn['expressions'] = new Array(this.expressionFns.length); // Require this to call $compile.$$addBindingInfo() which allows Protractor to find elements by binding.
for (var i = 0; i < this.expressionFns.length; i++) {
parsedFn['expressions'][i] = this.expressionFns[i]['exp'];
}
return parsedFn;
};
InterpolationParts.prototype.watchDelegate = function watchDelegate(scope, listener, objectEquality) {
var watcher = new InterpolationPartsWatcher(this, scope, listener, objectEquality);
return function() { watcher.cancelWatch(); };
};
function InterpolationPartsWatcher(interpolationParts, scope, listener, objectEquality) {
this.interpolationParts = interpolationParts;
this.scope = scope;
this.previousResult = (undefined);
this.listener = listener;
var self = this;
this.expressionFnsWatcher = scope['$watchGroup'](interpolationParts.expressionFns, function(newExpressionValues, oldExpressionValues) {
self.watchListener(newExpressionValues, oldExpressionValues);
});
}
InterpolationPartsWatcher.prototype.watchListener = function watchListener(newExpressionValues, oldExpressionValues) {
var result = this.interpolationParts.getResult(newExpressionValues);
if (isFunction(this.listener)) {
this.listener.call(null, result, newExpressionValues === oldExpressionValues ? result : this.previousResult, this.scope);
}
this.previousResult = result;
};
InterpolationPartsWatcher.prototype.cancelWatch = function cancelWatch() {
this.expressionFnsWatcher();
};
// NOTE: ADVANCED_OPTIMIZATIONS mode.
//
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
// constructs incompatible with that mode.
/* global $interpolateMinErr: false */
/* global indexToLineAndColumn: false */
/* global InterpolationParts: false */
/* global PluralMessage: false */
/* global SelectMessage: false */
/* global subtractOffset: false */
// The params src and dst are exactly one of two types: NestedParserState or MessageFormatParser.
// This function is fully optimized by V8. (inspect via IRHydra or --trace-deopt.)
// The idea behind writing it this way is to avoid repeating oneself. This is the ONE place where
// the parser state that is saved/restored when parsing nested mustaches is specified.
function copyNestedParserState(src, dst) {
dst.expressionFn = src.expressionFn;
dst.expressionMinusOffsetFn = src.expressionMinusOffsetFn;
dst.pluralOffset = src.pluralOffset;
dst.choices = src.choices;
dst.choiceKey = src.choiceKey;
dst.interpolationParts = src.interpolationParts;
dst.ruleChoiceKeyword = src.ruleChoiceKeyword;
dst.msgStartIndex = src.msgStartIndex;
dst.expressionStartIndex = src.expressionStartIndex;
}
function NestedParserState(parser) {
copyNestedParserState(parser, this);
}
/**
* @constructor
* @private
*/
function MessageFormatParser(text, startIndex, $parse, pluralCat, stringifier,
mustHaveExpression, trustedContext, allOrNothing) {
this.text = text;
this.index = startIndex || 0;
this.$parse = $parse;
this.pluralCat = pluralCat;
this.stringifier = stringifier;
this.mustHaveExpression = !!mustHaveExpression;
this.trustedContext = trustedContext;
this.allOrNothing = !!allOrNothing;
this.expressionFn = null;
this.expressionMinusOffsetFn = null;
this.pluralOffset = null;
this.choices = null;
this.choiceKey = null;
this.interpolationParts = null;
this.msgStartIndex = null;
this.nestedStateStack = [];
this.parsedFn = null;
this.rule = null;
this.ruleStack = null;
this.ruleChoiceKeyword = null;
this.interpNestLevel = null;
this.expressionStartIndex = null;
this.stringStartIndex = null;
this.stringQuote = null;
this.stringInterestsRe = null;
this.angularOperatorStack = null;
this.textPart = null;
}
// preserve v8 optimization.
var EMPTY_STATE = new NestedParserState(new MessageFormatParser(
/* text= */ '', /* startIndex= */ 0, /* $parse= */ null, /* pluralCat= */ null, /* stringifier= */ null,
/* mustHaveExpression= */ false, /* trustedContext= */ null, /* allOrNothing */ false));
MessageFormatParser.prototype.pushState = function pushState() {
this.nestedStateStack.push(new NestedParserState(this));
copyNestedParserState(EMPTY_STATE, this);
};
MessageFormatParser.prototype.popState = function popState() {
if (this.nestedStateStack.length === 0) {
this.errorInParseLogic();
}
var previousState = this.nestedStateStack.pop();
copyNestedParserState(previousState, this);
};
// Oh my JavaScript! Who knew you couldn't match a regex at a specific
// location in a string but will always search forward?!
// Apparently you'll be growing this ability via the sticky flag (y) in
// ES6. I'll just to work around you for now.
MessageFormatParser.prototype.matchRe = function matchRe(re, search) {
re.lastIndex = this.index;
var match = re.exec(this.text);
if (match != null && (search === true || (match.index === this.index))) {
this.index = re.lastIndex;
return match;
}
return null;
};
MessageFormatParser.prototype.searchRe = function searchRe(re) {
return this.matchRe(re, true);
};
MessageFormatParser.prototype.consumeRe = function consumeRe(re) {
// Without the sticky flag, we can't use the .test() method to consume a
// match at the current index. Instead, we'll use the slower .exec() method
// and verify match.index.
return !!this.matchRe(re);
};
// Run through our grammar avoiding deeply nested function call chains.
MessageFormatParser.prototype.run = function run(initialRule) {
this.ruleStack = [initialRule];
do {
this.rule = this.ruleStack.pop();
while (this.rule) {
this.rule();
}
this.assertRuleOrNull(this.rule);
} while (this.ruleStack.length > 0);
};
MessageFormatParser.prototype.errorInParseLogic = function errorInParseLogic() {
throw $interpolateMinErr('logicbug',
'The messageformat parser has encountered an internal error. Please file a github issue against the AngularJS project and provide this message text that triggers the bug. Text: “{0}”',
this.text);
};
MessageFormatParser.prototype.assertRuleOrNull = function assertRuleOrNull(rule) {
if (rule === undefined) {
this.errorInParseLogic();
}
};
var NEXT_WORD_RE = /\s*(\w+)\s*/g;
MessageFormatParser.prototype.errorExpecting = function errorExpecting() {
// What was wrong with the syntax? Unsupported type, missing comma, or something else?
var match = this.matchRe(NEXT_WORD_RE), position;
if (match == null) {
position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('reqarg',
'Expected one of “plural” or “select” at line {0}, column {1} of text “{2}”',
position.line, position.column, this.text);
}
var word = match[1];
if (word === 'select' || word === 'plural') {
position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('reqcomma',
'Expected a comma after the keyword “{0}” at line {1}, column {2} of text “{3}”',
word, position.line, position.column, this.text);
} else {
position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('unknarg',
'Unsupported keyword “{0}” at line {0}, column {1}. Only “plural” and “select” are currently supported. Text: “{3}”',
word, position.line, position.column, this.text);
}
};
var STRING_START_RE = /['"]/g;
MessageFormatParser.prototype.ruleString = function ruleString() {
var match = this.matchRe(STRING_START_RE);
if (match == null) {
var position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('wantstring',
'Expected the beginning of a string at line {0}, column {1} in text “{2}”',
position.line, position.column, this.text);
}
this.startStringAtMatch(match);
};
MessageFormatParser.prototype.startStringAtMatch = function startStringAtMatch(match) {
this.stringStartIndex = match.index;
this.stringQuote = match[0];
this.stringInterestsRe = this.stringQuote === '\'' ? SQUOTED_STRING_INTEREST_RE : DQUOTED_STRING_INTEREST_RE;
this.rule = this.ruleInsideString;
};
var SQUOTED_STRING_INTEREST_RE = /\\(?:\\|'|u[0-9A-Fa-f]{4}|x[0-9A-Fa-f]{2}|[0-7]{3}|\r\n|\n|[\s\S])|'/g;
var DQUOTED_STRING_INTEREST_RE = /\\(?:\\|"|u[0-9A-Fa-f]{4}|x[0-9A-Fa-f]{2}|[0-7]{3}|\r\n|\n|[\s\S])|"/g;
MessageFormatParser.prototype.ruleInsideString = function ruleInsideString() {
var match = this.searchRe(this.stringInterestsRe);
if (match == null) {
var position = indexToLineAndColumn(this.text, this.stringStartIndex);
throw $interpolateMinErr('untermstr',
'The string beginning at line {0}, column {1} is unterminated in text “{2}”',
position.line, position.column, this.text);
}
if (match[0] === this.stringQuote) {
this.rule = null;
}
};
var PLURAL_OR_SELECT_ARG_TYPE_RE = /\s*(plural|select)\s*,\s*/g;
MessageFormatParser.prototype.rulePluralOrSelect = function rulePluralOrSelect() {
var match = this.searchRe(PLURAL_OR_SELECT_ARG_TYPE_RE);
if (match == null) {
this.errorExpecting();
}
var argType = match[1];
switch (argType) {
case 'plural': this.rule = this.rulePluralStyle; break;
case 'select': this.rule = this.ruleSelectStyle; break;
default: this.errorInParseLogic();
}
};
MessageFormatParser.prototype.rulePluralStyle = function rulePluralStyle() {
this.choices = Object.create(null);
this.ruleChoiceKeyword = this.rulePluralValueOrKeyword;
this.rule = this.rulePluralOffset;
};
MessageFormatParser.prototype.ruleSelectStyle = function ruleSelectStyle() {
this.choices = Object.create(null);
this.ruleChoiceKeyword = this.ruleSelectKeyword;
this.rule = this.ruleSelectKeyword;
};
var NUMBER_RE = /[0]|(?:[1-9][0-9]*)/g;
var PLURAL_OFFSET_RE = new RegExp('\\s*offset\\s*:\\s*(' + NUMBER_RE.source + ')', 'g');
MessageFormatParser.prototype.rulePluralOffset = function rulePluralOffset() {
var match = this.matchRe(PLURAL_OFFSET_RE);
this.pluralOffset = (match == null) ? 0 : parseInt(match[1], 10);
this.expressionMinusOffsetFn = subtractOffset(this.expressionFn, this.pluralOffset);
this.rule = this.rulePluralValueOrKeyword;
};
MessageFormatParser.prototype.assertChoiceKeyIsNew = function assertChoiceKeyIsNew(choiceKey, index) {
if (this.choices[choiceKey] !== undefined) {
var position = indexToLineAndColumn(this.text, index);
throw $interpolateMinErr('dupvalue',
'The choice “{0}” is specified more than once. Duplicate key is at line {1}, column {2} in text “{3}”',
choiceKey, position.line, position.column, this.text);
}
};
var SELECT_KEYWORD = /\s*(\w+)/g;
MessageFormatParser.prototype.ruleSelectKeyword = function ruleSelectKeyword() {
var match = this.matchRe(SELECT_KEYWORD);
if (match == null) {
this.parsedFn = new SelectMessage(this.expressionFn, this.choices).parsedFn;
this.rule = null;
return;
}
this.choiceKey = match[1];
this.assertChoiceKeyIsNew(this.choiceKey, match.index);
this.rule = this.ruleMessageText;
};
var EXPLICIT_VALUE_OR_KEYWORD_RE = new RegExp('\\s*(?:(?:=(' + NUMBER_RE.source + '))|(\\w+))', 'g');
MessageFormatParser.prototype.rulePluralValueOrKeyword = function rulePluralValueOrKeyword() {
var match = this.matchRe(EXPLICIT_VALUE_OR_KEYWORD_RE);
if (match == null) {
this.parsedFn = new PluralMessage(this.expressionFn, this.choices, this.pluralOffset, this.pluralCat).parsedFn;
this.rule = null;
return;
}
if (match[1] != null) {
this.choiceKey = parseInt(match[1], 10);
} else {
this.choiceKey = match[2];
}
this.assertChoiceKeyIsNew(this.choiceKey, match.index);
this.rule = this.ruleMessageText;
};
var BRACE_OPEN_RE = /\s*\{/g;
var BRACE_CLOSE_RE = /}/g;
MessageFormatParser.prototype.ruleMessageText = function ruleMessageText() {
if (!this.consumeRe(BRACE_OPEN_RE)) {
var position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('reqopenbrace',
'The plural choice “{0}” must be followed by a message in braces at line {1}, column {2} in text “{3}”',
this.choiceKey, position.line, position.column, this.text);
}
this.msgStartIndex = this.index;
this.interpolationParts = new InterpolationParts(this.trustedContext, this.allOrNothing);
this.rule = this.ruleInInterpolationOrMessageText;
};
// Note: Since "\" is used as an escape character, don't allow it to be part of the
// startSymbol/endSymbol when I add the feature to allow them to be redefined.
var INTERP_OR_END_MESSAGE_RE = /\\.|{{|}/g;
var INTERP_OR_PLURALVALUE_OR_END_MESSAGE_RE = /\\.|{{|#|}/g;
var ESCAPE_OR_MUSTACHE_BEGIN_RE = /\\.|{{/g;
MessageFormatParser.prototype.advanceInInterpolationOrMessageText = function advanceInInterpolationOrMessageText() {
var currentIndex = this.index, match;
if (this.ruleChoiceKeyword == null) { // interpolation
match = this.searchRe(ESCAPE_OR_MUSTACHE_BEGIN_RE);
if (match == null) { // End of interpolation text. Nothing more to process.
this.textPart = this.text.substring(currentIndex);
this.index = this.text.length;
return null;
}
} else {
match = this.searchRe(this.ruleChoiceKeyword === this.rulePluralValueOrKeyword ?
INTERP_OR_PLURALVALUE_OR_END_MESSAGE_RE : INTERP_OR_END_MESSAGE_RE);
if (match == null) {
var position = indexToLineAndColumn(this.text, this.msgStartIndex);
throw $interpolateMinErr('reqendbrace',
'The plural/select choice “{0}” message starting at line {1}, column {2} does not have an ending closing brace. Text “{3}”',
this.choiceKey, position.line, position.column, this.text);
}
}
// match is non-null.
var token = match[0];
this.textPart = this.text.substring(currentIndex, match.index);
return token;
};
MessageFormatParser.prototype.ruleInInterpolationOrMessageText = function ruleInInterpolationOrMessageText() {
var currentIndex = this.index;
var token = this.advanceInInterpolationOrMessageText();
if (token == null) {
// End of interpolation text. Nothing more to process.
this.index = this.text.length;
this.interpolationParts.addText(this.text.substring(currentIndex));
this.rule = null;
return;
}
if (token[0] === '\\') {
// unescape next character and continue
this.interpolationParts.addText(this.textPart + token[1]);
return;
}
this.interpolationParts.addText(this.textPart);
if (token === '{{') {
this.pushState();
this.ruleStack.push(this.ruleEndMustacheInInterpolationOrMessage);
this.rule = this.ruleEnteredMustache;
} else if (token === '}') {
this.choices[this.choiceKey] = this.interpolationParts.toParsedFn(/*mustHaveExpression=*/false, this.text);
this.rule = this.ruleChoiceKeyword;
} else if (token === '#') {
this.interpolationParts.addExpressionFn(this.expressionMinusOffsetFn);
} else {
this.errorInParseLogic();
}
};
MessageFormatParser.prototype.ruleInterpolate = function ruleInterpolate() {
this.interpolationParts = new InterpolationParts(this.trustedContext, this.allOrNothing);
this.rule = this.ruleInInterpolation;
};
MessageFormatParser.prototype.ruleInInterpolation = function ruleInInterpolation() {
var currentIndex = this.index;
var match = this.searchRe(ESCAPE_OR_MUSTACHE_BEGIN_RE);
if (match == null) {
// End of interpolation text. Nothing more to process.
this.index = this.text.length;
this.interpolationParts.addText(this.text.substring(currentIndex));
this.parsedFn = this.interpolationParts.toParsedFn(this.mustHaveExpression, this.text);
this.rule = null;
return;
}
var token = match[0];
if (token[0] === '\\') {
// unescape next character and continue
this.interpolationParts.addText(this.text.substring(currentIndex, match.index) + token[1]);
return;
}
this.interpolationParts.addText(this.text.substring(currentIndex, match.index));
this.pushState();
this.ruleStack.push(this.ruleInterpolationEndMustache);
this.rule = this.ruleEnteredMustache;
};
MessageFormatParser.prototype.ruleInterpolationEndMustache = function ruleInterpolationEndMustache() {
var expressionFn = this.parsedFn;
this.popState();
this.interpolationParts.addExpressionFn(expressionFn);
this.rule = this.ruleInInterpolation;
};
MessageFormatParser.prototype.ruleEnteredMustache = function ruleEnteredMustache() {
this.parsedFn = null;
this.ruleStack.push(this.ruleEndMustache);
this.rule = this.ruleAngularExpression;
};
MessageFormatParser.prototype.ruleEndMustacheInInterpolationOrMessage = function ruleEndMustacheInInterpolationOrMessage() {
var expressionFn = this.parsedFn;
this.popState();
this.interpolationParts.addExpressionFn(expressionFn);
this.rule = this.ruleInInterpolationOrMessageText;
};
var INTERP_END_RE = /\s*}}/g;
MessageFormatParser.prototype.ruleEndMustache = function ruleEndMustache() {
var match = this.matchRe(INTERP_END_RE);
if (match == null) {
var position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('reqendinterp',
'Expecting end of interpolation symbol, “{0}”, at line {1}, column {2} in text “{3}”',
'}}', position.line, position.column, this.text);
}
if (this.parsedFn == null) {
// If we parsed a MessageFormat extension, (e.g. select/plural today, maybe more some other
// day), then the result *has* to be a string and those rules would have already set
// this.parsedFn. If there was no MessageFormat extension, then there is no requirement to
// stringify the result and parsedFn isn't set. We set it here. While we could have set it
// unconditionally when exiting the Angular expression, I intend for us to not just replace
// $interpolate, but also to replace $parse in a future version (so ng-bind can work), and in
// such a case we do not want to unnecessarily stringify something if it's not going to be used
// in a string context.
this.parsedFn = this.$parse(this.expressionFn, this.stringifier);
this.parsedFn['exp'] = this.expressionFn['exp']; // Needed to pretend to be $interpolate for tests copied from interpolateSpec.js
this.parsedFn['expressions'] = this.expressionFn['expressions']; // Require this to call $compile.$$addBindingInfo() which allows Protractor to find elements by binding.
}
this.rule = null;
};
MessageFormatParser.prototype.ruleAngularExpression = function ruleAngularExpression() {
this.angularOperatorStack = [];
this.expressionStartIndex = this.index;
this.rule = this.ruleInAngularExpression;
};
function getEndOperator(opBegin) {
switch (opBegin) {
case '{': return '}';
case '[': return ']';
case '(': return ')';
default: return null;
}
}
function getBeginOperator(opEnd) {
switch (opEnd) {
case '}': return '{';
case ']': return '[';
case ')': return '(';
default: return null;
}
}
// TODO(chirayu): The interpolation endSymbol must also be accounted for. It
// just so happens that "}" is an operator so it's in the list below. But we
// should support any other type of start/end interpolation symbol.
var INTERESTING_OPERATORS_RE = /[[\]{}()'",]/g;
MessageFormatParser.prototype.ruleInAngularExpression = function ruleInAngularExpression() {
var match = this.searchRe(INTERESTING_OPERATORS_RE);
var position;
if (match == null) {
if (this.angularOperatorStack.length === 0) {
// This is the end of the Angular expression so this is actually a
// success. Note that when inside an interpolation, this means we even
// consumed the closing interpolation symbols if they were curlies. This
// is NOT an error at this point but will become an error further up the
// stack when the part that saw the opening curlies is unable to find the
// closing ones.
this.index = this.text.length;
this.expressionFn = this.$parse(this.text.substring(this.expressionStartIndex, this.index));
// Needed to pretend to be $interpolate for tests copied from interpolateSpec.js
this.expressionFn['exp'] = this.text.substring(this.expressionStartIndex, this.index);
this.expressionFn['expressions'] = this.expressionFn['expressions'];
this.rule = null;
return;
}
var innermostOperator = this.angularOperatorStack[0];
throw $interpolateMinErr('badexpr',
'Unexpected end of Angular expression. Expecting operator “{0}” at the end of the text “{1}”',
this.getEndOperator(innermostOperator), this.text);
}
var operator = match[0];
if (operator === '\'' || operator === '"') {
this.ruleStack.push(this.ruleInAngularExpression);
this.startStringAtMatch(match);
return;
}
if (operator === ',') {
if (this.trustedContext) {
position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('unsafe',
'Use of select/plural MessageFormat syntax is currently disallowed in a secure context ({0}). At line {1}, column {2} of text “{3}”',
this.trustedContext, position.line, position.column, this.text);
}
// only the top level comma has relevance.
if (this.angularOperatorStack.length === 0) {
// todo: does this need to be trimmed?
this.expressionFn = this.$parse(this.text.substring(this.expressionStartIndex, match.index));
// Needed to pretend to be $interpolate for tests copied from interpolateSpec.js
this.expressionFn['exp'] = this.text.substring(this.expressionStartIndex, match.index);
this.expressionFn['expressions'] = this.expressionFn['expressions'];
this.rule = null;
this.rule = this.rulePluralOrSelect;
}
return;
}
if (getEndOperator(operator) != null) {
this.angularOperatorStack.unshift(operator);
return;
}
var beginOperator = getBeginOperator(operator);
if (beginOperator == null) {
this.errorInParseLogic();
}
if (this.angularOperatorStack.length > 0) {
if (beginOperator === this.angularOperatorStack[0]) {
this.angularOperatorStack.shift();
return;
}
position = indexToLineAndColumn(this.text, this.index);
throw $interpolateMinErr('badexpr',
'Unexpected operator “{0}” at line {1}, column {2} in text. Was expecting “{3}”. Text: “{4}”',
operator, position.line, position.column, getEndOperator(this.angularOperatorStack[0]), this.text);
}
// We are trying to pop off the operator stack but there really isn't anything to pop off.
this.index = match.index;
this.expressionFn = this.$parse(this.text.substring(this.expressionStartIndex, this.index));
// Needed to pretend to be $interpolate for tests copied from interpolateSpec.js
this.expressionFn['exp'] = this.text.substring(this.expressionStartIndex, this.index);
this.expressionFn['expressions'] = this.expressionFn['expressions'];
this.rule = null;
};
// NOTE: ADVANCED_OPTIMIZATIONS mode.
//
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
// constructs incompatible with that mode.
/* global $interpolateMinErr: true */
/* global isFunction: true */
/* global noop: true */
/* global toJson: true */
/* global MessageFormatParser: false */
/* global stringify: false */
/**
* @ngdoc module
* @name ngMessageFormat
* @packageName angular-message-format
*
* @description
*
* ## What is ngMessageFormat?
*
* The ngMessageFormat module extends the Angular {@link ng.$interpolate `$interpolate`} service
* with a syntax for handling pluralization and gender specific messages, which is based on the
* [ICU MessageFormat syntax][ICU].
*
* See [the design doc][ngMessageFormat doc] for more information.
*
* [ICU]: http://userguide.icu-project.org/formatparse/messages#TOC-MessageFormat
* [ngMessageFormat doc]: https://docs.google.com/a/google.com/document/d/1pbtW2yvtmFBikfRrJd8VAsabiFkKezmYZ_PbgdjQOVU/edit
*
* ## Examples
*
* ### Gender
*
* This example uses the "select" keyword to specify the message based on gender.
*
* <example name="ngMessageFormat-example-gender" module="msgFmtExample" deps="angular-message-format.js">
* <file name="index.html">
* <div ng-controller="AppController">
* Select Recipient:<br>
<select ng-model="recipient" ng-options="person as person.name for person in recipients">
</select>
<p>{{recipient.gender, select,
male {{{recipient.name}} unwrapped his gift. }
female {{{recipient.name}} unwrapped her gift. }
other {{{recipient.name}} unwrapped their gift. }
}}</p>
* </div>
* </file>
* <file name="script.js">
* function Person(name, gender) {
* this.name = name;
* this.gender = gender;
* }
*
* var alice = new Person('Alice', 'female'),
* bob = new Person('Bob', 'male'),
* ashley = new Person('Ashley', '');
*
* angular.module('msgFmtExample', ['ngMessageFormat'])
* .controller('AppController', ['$scope', function($scope) {
* $scope.recipients = [alice, bob, ashley];
* $scope.recipient = $scope.recipients[0];
* }]);
* </file>
* </example>
*
* ### Plural
*
* This example shows how the "plural" keyword is used to account for a variable number of entities.
* The "#" variable holds the current number and can be embedded in the message.
*
* Note that "=1" takes precedence over "one".
*
* The example also shows the "offset" keyword, which allows you to offset the value of the "#" variable.
*
* <example name="ngMessageFormat-example-plural" module="msgFmtExample" deps="angular-message-format.js">
* <file name="index.html">
* <div ng-controller="AppController">
* <button ng-click="recipients.pop()" id="decreaseRecipients">decreaseRecipients</button><br>
* Select recipients:<br>
* <select multiple size=5 ng-model="recipients" ng-options="person as person.name for person in people">
* </select><br>
* <p>{{recipients.length, plural, offset:1
* =0 {{{sender.name}} gave no gifts (\#=#)}
* =1 {{{sender.name}} gave a gift to {{recipients[0].name}} (\#=#)}
* one {{{sender.name}} gave {{recipients[0].name}} and one other person a gift (\#=#)}
* other {{{sender.name}} gave {{recipients[0].name}} and # other people a gift (\#=#)}
* }}</p>
* </div>
* </file>
*
* <file name="script.js">
* function Person(name, gender) {
* this.name = name;
* this.gender = gender;
* }
*
* var alice = new Person('Alice', 'female'),
* bob = new Person('Bob', 'male'),
* sarah = new Person('Sarah', 'female'),
* harry = new Person('Harry Potter', 'male'),
* ashley = new Person('Ashley', '');
*
* angular.module('msgFmtExample', ['ngMessageFormat'])
* .controller('AppController', ['$scope', function($scope) {
* $scope.people = [alice, bob, sarah, ashley];
* $scope.recipients = [alice, bob, sarah];
* $scope.sender = harry;
* }]);
* </file>
*
* <file name="protractor.js" type="protractor">
* describe('MessageFormat plural', function() {
*
* it('should pluralize initial values', function() {
* var messageElem = element(by.binding('recipients.length')),
* decreaseRecipientsBtn = element(by.id('decreaseRecipients'));
*
* expect(messageElem.getText()).toEqual('Harry Potter gave Alice and 2 other people a gift (#=2)');
* decreaseRecipientsBtn.click();
* expect(messageElem.getText()).toEqual('Harry Potter gave Alice and one other person a gift (#=1)');
* decreaseRecipientsBtn.click();
* expect(messageElem.getText()).toEqual('Harry Potter gave a gift to Alice (#=0)');
* decreaseRecipientsBtn.click();
* expect(messageElem.getText()).toEqual('Harry Potter gave no gifts (#=-1)');
* });
* });
* </file>
* </example>
*
* ### Plural and Gender together
*
* This example shows how you can specify gender rules for specific plural matches - in this case,
* =1 is special cased for gender.
* <example name="ngMessageFormat-example-plural-gender" module="msgFmtExample" deps="angular-message-format.js">
* <file name="index.html">
* <div ng-controller="AppController">
Select recipients:<br>
<select multiple size=5 ng-model="recipients" ng-options="person as person.name for person in people">
</select><br>
<p>{{recipients.length, plural,
=0 {{{sender.name}} has not given any gifts to anyone.}
=1 { {{recipients[0].gender, select,
female { {{sender.name}} gave {{recipients[0].name}} her gift.}
male { {{sender.name}} gave {{recipients[0].name}} his gift.}
other { {{sender.name}} gave {{recipients[0].name}} their gift.}
}}
}
other {{{sender.name}} gave {{recipients.length}} people gifts.}
}}</p>
</file>
* <file name="script.js">
* function Person(name, gender) {
* this.name = name;
* this.gender = gender;
* }
*
* var alice = new Person('Alice', 'female'),
* bob = new Person('Bob', 'male'),
* harry = new Person('Harry Potter', 'male'),
* ashley = new Person('Ashley', '');
*
* angular.module('msgFmtExample', ['ngMessageFormat'])
* .controller('AppController', ['$scope', function($scope) {
* $scope.people = [alice, bob, ashley];
* $scope.recipients = [alice];
* $scope.sender = harry;
* }]);
* </file>
</example>
*/
var $$MessageFormatFactory = ['$parse', '$locale', '$sce', '$exceptionHandler', function $$messageFormat(
$parse, $locale, $sce, $exceptionHandler) {
function getStringifier(trustedContext, allOrNothing, text) {
return function stringifier(value) {
try {
value = trustedContext ? $sce['getTrusted'](trustedContext, value) : $sce['valueOf'](value);
return allOrNothing && (value === undefined) ? value : stringify(value);
} catch (err) {
$exceptionHandler($interpolateMinErr['interr'](text, err));
}
};
}
function interpolate(text, mustHaveExpression, trustedContext, allOrNothing) {
var stringifier = getStringifier(trustedContext, allOrNothing, text);
var parser = new MessageFormatParser(text, 0, $parse, $locale['pluralCat'], stringifier,
mustHaveExpression, trustedContext, allOrNothing);
parser.run(parser.ruleInterpolate);
return parser.parsedFn;
}
return {
'interpolate': interpolate
};
}];
var $$interpolateDecorator = ['$$messageFormat', '$delegate', function $$interpolateDecorator($$messageFormat, $interpolate) {
if ($interpolate['startSymbol']() !== '{{' || $interpolate['endSymbol']() !== '}}') {
throw $interpolateMinErr('nochgmustache', 'angular-message-format.js currently does not allow you to use custom start and end symbols for interpolation.');
}
var interpolate = $$messageFormat['interpolate'];
interpolate['startSymbol'] = $interpolate['startSymbol'];
interpolate['endSymbol'] = $interpolate['endSymbol'];
return interpolate;
}];
var $interpolateMinErr;
var isFunction;
var noop;
var toJson;
var module = window['angular']['module']('ngMessageFormat', ['ng']);
module['factory']('$$messageFormat', $$MessageFormatFactory);
module['config'](['$provide', function($provide) {
$interpolateMinErr = window['angular']['$interpolateMinErr'];
isFunction = window['angular']['isFunction'];
noop = window['angular']['noop'];
toJson = window['angular']['toJson'];
$provide['decorator']('$interpolate', $$interpolateDecorator);
}]);
})(window, window.angular);
|