Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 8a45064

Browse files
chirayukpetebacondarwin
authored andcommitted
fix(ngMessageFormat): minified symbol and nested required expression
Add an E2E test that works against the minified module to test that the minified build works correctly. Fix a bug where mustHaveExpression was passed through to submessages unchanged. Use of the messageFormat syntax automatically means that you are using an expression. Therefore, submessages should not be required to also have messages. Closes #11414 Closes #11592
1 parent 40319a4 commit 8a45064

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

src/ngMessageFormat/messageFormatCommon.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
66
// constructs incompatible with that mode.
77

8-
var $interpolateMinErr = angular['$interpolateMinErr'];
8+
var $interpolateMinErr = window['angular']['$interpolateMinErr'];
99

10-
var noop = angular['noop'],
11-
isFunction = angular['isFunction'],
12-
toJson = angular['toJson'];
10+
var noop = window['angular']['noop'],
11+
isFunction = window['angular']['isFunction'],
12+
toJson = window['angular']['toJson'];
1313

1414
function stringify(value) {
1515
if (value == null /* null/undefined */) { return ''; }

src/ngMessageFormat/messageFormatParser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ MessageFormatParser.prototype.ruleInInterpolationOrMessageText = function ruleIn
334334
this.ruleStack.push(this.ruleEndMustacheInInterpolationOrMessage);
335335
this.rule = this.ruleEnteredMustache;
336336
} else if (token == "}") {
337-
this.choices[this.choiceKey] = this.interpolationParts.toParsedFn(this.mustHaveExpression, this.text);
337+
this.choices[this.choiceKey] = this.interpolationParts.toParsedFn(/*mustHaveExpression=*/false, this.text);
338338
this.rule = this.ruleChoiceKeyword;
339339
} else if (token == "#") {
340340
this.interpolationParts.addExpressionFn(this.expressionMinusOffsetFn);

src/ngMessageFormat/messageFormatService.js

+53-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,58 @@
1717
* Angular internal service to recognize MessageFormat extensions in interpolation expressions.
1818
* For more information, see:
1919
* https://docs.google.com/a/google.com/document/d/1pbtW2yvtmFBikfRrJd8VAsabiFkKezmYZ_PbgdjQOVU/edit
20+
*
21+
* ## Example
22+
*
23+
* <example name="ngMessageFormat-example" module="msgFmtExample" deps="angular-messageFormat.min.js">
24+
* <file name="index.html">
25+
* <div ng-controller="AppController">
26+
* <button ng-click="decreaseRecipients()" id="decreaseRecipients">decreaseRecipients</button><br>
27+
* <span id="message">{{recipients.length, plural, offset:1
28+
* =0 {{{sender.name}} gave no gifts (\#=#)}
29+
* =1 {{{sender.name}} gave one gift to {{recipients[0].name}} (\#=#)}
30+
* one {{{sender.name}} gave {{recipients[0].name}} and one other person a gift (\#=#)}
31+
* other {{{sender.name}} gave {{recipients[0].name}} and # other people a gift (\#=#)}
32+
* }}</span>
33+
* </div>
34+
* </file>
35+
*
36+
* <file name="script.js">
37+
* function Person(name, gender) {
38+
* this.name = name;
39+
* this.gender = gender;
40+
* }
41+
*
42+
* var alice = new Person("Alice", "female"),
43+
* bob = new Person("Bob", "male"),
44+
* charlie = new Person("Charlie", "male"),
45+
* harry = new Person("Harry Potter", "male");
46+
*
47+
* angular.module('msgFmtExample', ['ngMessageFormat'])
48+
* .controller('AppController', ['$scope', function($scope) {
49+
* $scope.recipients = [alice, bob, charlie];
50+
* $scope.sender = harry;
51+
* $scope.decreaseRecipients = function() {
52+
* --$scope.recipients.length;
53+
* };
54+
* }]);
55+
* </file>
56+
*
57+
* <file name="protractor.js" type="protractor">
58+
* describe('MessageFormat plural', function() {
59+
* it('should pluralize initial values', function() {
60+
* var messageElem = element(by.id('message')), decreaseRecipientsBtn = element(by.id('decreaseRecipients'));
61+
* expect(messageElem.getText()).toEqual('Harry Potter gave Alice and 2 other people a gift (#=2)');
62+
* decreaseRecipientsBtn.click();
63+
* expect(messageElem.getText()).toEqual('Harry Potter gave Alice and one other person a gift (#=1)');
64+
* decreaseRecipientsBtn.click();
65+
* expect(messageElem.getText()).toEqual('Harry Potter gave one gift to Alice (#=0)');
66+
* decreaseRecipientsBtn.click();
67+
* expect(messageElem.getText()).toEqual('Harry Potter gave no gifts (#=-1)');
68+
* });
69+
* });
70+
* </file>
71+
* </example>
2072
*/
2173
var $$MessageFormatFactory = ['$parse', '$locale', '$sce', '$exceptionHandler', function $$messageFormat(
2274
$parse, $locale, $sce, $exceptionHandler) {
@@ -61,7 +113,7 @@ var $$interpolateDecorator = ['$$messageFormat', '$delegate', function $$interpo
61113
* @name ngMessageFormat
62114
* @description
63115
*/
64-
var module = angular['module']('ngMessageFormat', ['ng']);
116+
var module = window['angular']['module']('ngMessageFormat', ['ng']);
65117
module['factory']('$$messageFormat', $$MessageFormatFactory);
66118
module['config'](['$provide', function($provide) {
67119
$provide['decorator']('$interpolate', $$interpolateDecorator);

test/ngMessageFormat/messageFormatSpec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe('$$ngMessageFormat', function() {
125125
" one {YOU SHOULD NEVER SEE THIS MESSAGE}\n" +
126126
" other {You gave some people gifts}\n" +
127127
"}}";
128-
var parsedFn = $interpolate(text);
128+
var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);
129129

130130
$rootScope.recipients.length=2;
131131
expect(parsedFn($rootScope)).toEqual("You gave some people gifts");
@@ -146,7 +146,7 @@ describe('$$ngMessageFormat', function() {
146146
" one {YOU SHOULD NEVER SEE THIS MESSAGE}\n" +
147147
" other {{{sender.name}} gave them a gift}\n" +
148148
"}}";
149-
var parsedFn = $interpolate(text);
149+
var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);
150150

151151
$rootScope.recipients.length=2;
152152
expect(parsedFn($rootScope)).toEqual("Harry Potter gave them a gift");
@@ -167,7 +167,7 @@ describe('$$ngMessageFormat', function() {
167167
" one {{{sender.name}} gave {{recipients[0].name}} and one other person a gift (\\#=#)}\n" +
168168
" other {{{sender.name}} gave {{recipients[0].name}} and # other people a gift (\\#=#)}\n" +
169169
"}}";
170-
var parsedFn = $interpolate(text);
170+
var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);
171171

172172
$rootScope.recipients.length=3;
173173
// "#" should get replaced with the value of "recipients.length - offset"
@@ -196,7 +196,7 @@ describe('$$ngMessageFormat', function() {
196196
" }\n" +
197197
" other {You gave {{recipients.length}} people gifts. -{{sender.name}}}\n" +
198198
"}}";
199-
var parsedFn = $interpolate(text);
199+
var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);
200200
var result = parsedFn($rootScope);
201201
expect(result).toEqual("You gave 3 people gifts. -Harry Potter");
202202
});

0 commit comments

Comments
 (0)