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

fix(ngMessageFormat): ensure bindings are valid for Protractor #11649

Merged
merged 1 commit into from
Apr 19, 2015
Merged
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
4 changes: 2 additions & 2 deletions src/ngMessageFormat/messageFormatCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function parseTextLiteral(text) {
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.
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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/ngMessageFormat/messageFormatInterpolationParts.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ InterpolationParts.prototype.toParsedFn = function toParsedFn(mustHaveExpression
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.
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;
parsedFn['expressions'][i] = this.expressionFns[i]['exp'];
}

return parsedFn;
Expand Down
13 changes: 8 additions & 5 deletions src/ngMessageFormat/messageFormatParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ MessageFormatParser.prototype.ruleEndMustache = function ruleEndMustache() {
// 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.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;
};
Expand Down Expand Up @@ -461,7 +461,8 @@ MessageFormatParser.prototype.ruleInAngularExpression = function ruleInAngularEx
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['exp'] = this.text.substring(this.expressionStartIndex, this.index);
this.expressionFn['expressions'] = this.expressionFn['expressions'];
this.rule = null;
return;
}
Expand All @@ -488,7 +489,8 @@ MessageFormatParser.prototype.ruleInAngularExpression = function ruleInAngularEx
// 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['exp'] = this.text.substring(this.expressionStartIndex, match.index);
this.expressionFn['expressions'] = this.expressionFn['expressions'];
this.rule = null;
this.rule = this.rulePluralOrSelect;
}
Expand Down Expand Up @@ -516,6 +518,7 @@ MessageFormatParser.prototype.ruleInAngularExpression = function ruleInAngularEx
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['exp'] = this.text.substring(this.expressionStartIndex, this.index);
this.expressionFn['expressions'] = this.expressionFn['expressions'];
this.rule = null;
};
2 changes: 2 additions & 0 deletions src/ngMessageFormat/messageFormatSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function MessageSelectorBase(expressionFn, choices) {
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) {
Expand Down
14 changes: 7 additions & 7 deletions src/ngMessageFormat/messageFormatService.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
* <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>
* <span>{{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>
*
Expand Down Expand Up @@ -57,7 +57,7 @@
* <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'));
* 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)');
Expand Down
8 changes: 8 additions & 0 deletions test/ngMessageFormat/messageFormatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ describe('$$ngMessageFormat', function() {
" other {You gave some people gifts}\n" +
"}}";
var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);
expect(parsedFn.expressions.length).toBe(1);
expect(parsedFn.expressions[0]).toEqual("recipients.length");

$rootScope.recipients.length=2;
expect(parsedFn($rootScope)).toEqual("You gave some people gifts");
Expand All @@ -147,6 +149,8 @@ describe('$$ngMessageFormat', function() {
" other {{{sender.name}} gave them a gift}\n" +
"}}";
var parsedFn = $interpolate(text, /*mustHaveExpression=*/true);
expect(parsedFn.expressions.length).toBe(1);
expect(parsedFn.expressions[0]).toEqual("recipients.length");

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

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