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

fix(ngMessageFormat): minified symbol and nested required expression #11592

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ngMessageFormat/messageFormatCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
// constructs incompatible with that mode.

var $interpolateMinErr = angular['$interpolateMinErr'];
var $interpolateMinErr = window['angular']['$interpolateMinErr'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary, since normally the code would be wrapped into an IIFE and passed window.angular as an argument:

(function (window, angular, undefined) { ... })(window, window.angular);

Any idea why it doesn't work ?


var noop = angular['noop'],
isFunction = angular['isFunction'],
toJson = angular['toJson'];
var noop = window['angular']['noop'],
isFunction = window['angular']['isFunction'],
toJson = window['angular']['toJson'];

function stringify(value) {
if (value == null /* null/undefined */) { return ''; }
Expand Down
2 changes: 1 addition & 1 deletion src/ngMessageFormat/messageFormatParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ MessageFormatParser.prototype.ruleInInterpolationOrMessageText = function ruleIn
this.ruleStack.push(this.ruleEndMustacheInInterpolationOrMessage);
this.rule = this.ruleEnteredMustache;
} else if (token == "}") {
this.choices[this.choiceKey] = this.interpolationParts.toParsedFn(this.mustHaveExpression, this.text);
this.choices[this.choiceKey] = this.interpolationParts.toParsedFn(/*mustHaveExpression=*/false, this.text);
this.rule = this.ruleChoiceKeyword;
} else if (token == "#") {
this.interpolationParts.addExpressionFn(this.expressionMinusOffsetFn);
Expand Down
54 changes: 53 additions & 1 deletion src/ngMessageFormat/messageFormatService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,58 @@
* Angular internal service to recognize MessageFormat extensions in interpolation expressions.
* For more information, see:
* https://docs.google.com/a/google.com/document/d/1pbtW2yvtmFBikfRrJd8VAsabiFkKezmYZ_PbgdjQOVU/edit
*
* ## Example
*
* <example name="ngMessageFormat-example" module="msgFmtExample" deps="angular-messageFormat.min.js">
* <file name="index.html">
* <div ng-controller="AppController">
* <button ng-click="decreaseRecipients()" id="decreaseRecipients">decreaseRecipients</button><br>
* <span id="message">{{recipients.length, plural, offset:1
* =0 {{{sender.name}} gave no gifts (\#=#)}
* =1 {{{sender.name}} gave one 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 (\#=#)}
* }}</span>
* </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"),
* charlie = new Person("Charlie", "male"),
* harry = new Person("Harry Potter", "male");
*
* angular.module('msgFmtExample', ['ngMessageFormat'])
* .controller('AppController', ['$scope', function($scope) {
* $scope.recipients = [alice, bob, charlie];
* $scope.sender = harry;
* $scope.decreaseRecipients = function() {
* --$scope.recipients.length;
* };
* }]);
* </file>
*
* <file name="protractor.js" type="protractor">
* describe('MessageFormat plural', function() {
* it('should pluralize initial values', function() {
* var messageElem = element(by.id('message')), 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 one gift to Alice (#=0)');
* decreaseRecipientsBtn.click();
* expect(messageElem.getText()).toEqual('Harry Potter gave no gifts (#=-1)');
* });
* });
* </file>
* </example>
*/
var $$MessageFormatFactory = ['$parse', '$locale', '$sce', '$exceptionHandler', function $$messageFormat(
$parse, $locale, $sce, $exceptionHandler) {
Expand Down Expand Up @@ -61,7 +113,7 @@ var $$interpolateDecorator = ['$$messageFormat', '$delegate', function $$interpo
* @name ngMessageFormat
* @description
*/
var module = angular['module']('ngMessageFormat', ['ng']);
var module = window['angular']['module']('ngMessageFormat', ['ng']);
module['factory']('$$messageFormat', $$MessageFormatFactory);
module['config'](['$provide', function($provide) {
$provide['decorator']('$interpolate', $$interpolateDecorator);
Expand Down
8 changes: 4 additions & 4 deletions test/ngMessageFormat/messageFormatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('$$ngMessageFormat', function() {
" one {YOU SHOULD NEVER SEE THIS MESSAGE}\n" +
" other {You gave some people gifts}\n" +
"}}";
var parsedFn = $interpolate(text);
var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);

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

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

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