Merge pull request #1795 from NativeScript/formatted-text-crash

Update TextBase/Button `text` property when `formattedText` changes
This commit is contained in:
Rossen Hristov
2016-03-18 17:08:38 +02:00
9 changed files with 107 additions and 12 deletions

View File

@ -5,6 +5,8 @@ import pagesModule = require("ui/page");
import buttonTestsNative = require("./button-tests-native");
import colorModule = require("color");
import enums = require("ui/enums");
import formattedStringModule = require("text/formatted-string");
import spanModule = require("text/span");
// <snippet module="ui/button" title="button">
// # Button
@ -291,3 +293,37 @@ export var testNativeTextAlignmentFromLocal = function () {
TKUnit.assert(actualResult === expectedTextAlignment, "Actual: " + actualResult + "; Expected: " + expectedTextAlignment);
});
}
export var test_WhenFormattedTextPropertyChanges_TextIsUpdated_Button = function () {
var firstSpan = new spanModule.Span();
firstSpan.fontSize = 10;
firstSpan.text = "First";
var secondSpan = new spanModule.Span();
secondSpan.fontSize = 15;
secondSpan.text = "Second";
var thirdSpan = new spanModule.Span();
thirdSpan.fontSize = 20;
thirdSpan.text = "Third";
var formattedString1 = new formattedStringModule.FormattedString();
formattedString1.spans.push(firstSpan);
var formattedString2 = new formattedStringModule.FormattedString();
formattedString2.spans.push(secondSpan);
formattedString2.spans.push(thirdSpan);
var view = new buttonModule.Button();
helper.buildUIAndRunTest(view, function (views: Array<viewModule.View>) {
TKUnit.assertEqual(view.text, "");
view.formattedText = formattedString1;
TKUnit.assertEqual(view.text, "First");
view.formattedText = formattedString2;
TKUnit.assertEqual(view.text, "SecondThird");
formattedString2.spans.getItem(0).text = "Mecond";
TKUnit.assertEqual(view.text, "MecondThird");
view.formattedText = null;
TKUnit.assertEqual(view.text, "");
});
}

View File

@ -6,6 +6,8 @@ import textFieldTestsNative = require("./text-field-tests-native");
import colorModule = require("color");
import enums = require("ui/enums");
import platform = require("platform");
import formattedStringModule = require("text/formatted-string");
import spanModule = require("text/span");
// <snippet module="ui/text-field" title="TextField">
// # TextField
@ -468,3 +470,37 @@ export var testMemoryLeak = function (done) {
textFieldTestsNative.typeTextNatively(textField, "Hello, world!");
}, done);
}
export var test_WhenFormattedTextPropertyChanges_TextIsUpdated_TextBase = function () {
var firstSpan = new spanModule.Span();
firstSpan.fontSize = 10;
firstSpan.text = "First";
var secondSpan = new spanModule.Span();
secondSpan.fontSize = 15;
secondSpan.text = "Second";
var thirdSpan = new spanModule.Span();
thirdSpan.fontSize = 20;
thirdSpan.text = "Third";
var formattedString1 = new formattedStringModule.FormattedString();
formattedString1.spans.push(firstSpan);
var formattedString2 = new formattedStringModule.FormattedString();
formattedString2.spans.push(secondSpan);
formattedString2.spans.push(thirdSpan);
var view = new textFieldModule.TextField();
helper.buildUIAndRunTest(view, function (views: Array<viewModule.View>) {
TKUnit.assertEqual(view.text, "");
view.formattedText = formattedString1;
TKUnit.assertEqual(view.text, "First");
view.formattedText = formattedString2;
TKUnit.assertEqual(view.text, "SecondThird");
formattedString2.spans.getItem(0).text = "Mecond";
TKUnit.assertEqual(view.text, "MecondThird");
view.formattedText = null;
TKUnit.assertEqual(view.text, "");
});
}

View File

@ -740,6 +740,7 @@ export function test_parseSpansDirectlyOnLabel() {
var page = <Page>views[0];
var testLabel = <Label>page.getViewById("testLabel");
TKUnit.assertEqual(testLabel.formattedText + "", "We areAwesome", "Formatted string should be set");
TKUnit.assertEqual(testLabel.text + "", "We areAwesome", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -757,6 +758,7 @@ export function test_parseSpansDirectlyOnButton() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "We areAwesome", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "We areAwesome", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -774,6 +776,7 @@ export function test_parseFormattedStringWithoutFormattedText() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "author num_comments", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "author num_comments", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -791,6 +794,7 @@ export function test_parseFormattedStringFullSyntax() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "author num_comments", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "author num_comments", "Formatted string should be set");
}
helper.navigate(function () { return p; });
@ -808,6 +812,7 @@ export function test_parseSpansDirectlyToFormattedString() {
var page = <Page>views[0];
var testButton = <Button>page.getViewById("testButton");
TKUnit.assertEqual(testButton.formattedText + "", "author num_comments", "Formatted string should be set");
TKUnit.assertEqual(testButton.text + "", "author num_comments", "Formatted string should be set");
}
helper.navigate(function () { return p; });

View File

@ -99,7 +99,10 @@ export class Button extends view.View implements definition.Button {
}
private onFormattedTextChanged(eventData: observable.PropertyChangeData) {
this._setFormattedTextPropertyToNative(eventData.value);
var value = <formattedString.FormattedString>eventData.value;
this._setFormattedTextPropertyToNative(value);
this._onPropertyChangedFromNative(Button.textProperty, value.toString());
}
public _onTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
@ -111,10 +114,14 @@ export class Button extends view.View implements definition.Button {
}
public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
if (data.newValue) {
(<formattedString.FormattedString>data.newValue).parent = this;
var newValue = <formattedString.FormattedString>data.newValue;
if (newValue) {
newValue.parent = this;
}
this._setFormattedTextPropertyToNative(data.newValue);
this._setFormattedTextPropertyToNative(newValue);
var newText = newValue ? newValue.toString() : "";
this._onPropertyChangedFromNative(Button.textProperty, newText);
}
public _addChildFromBuilder(name: string, value: any): void {

View File

@ -45,8 +45,9 @@ export class Button extends common.Button {
}
public _setFormattedTextPropertyToNative(value) {
var newText = value ? value._formattedText : null;
if (this.android) {
this.android.setText(value._formattedText);
this.android.setText(newText);
}
}
}

View File

@ -64,7 +64,8 @@ export class Button extends common.Button {
// the UIControlStateNormal value. If the value for UIControlStateNormal is not set,
// then the property defaults to a system value. Therefore, at a minimum, you should
// set the value for the normal state.
this.ios.setAttributedTitleForState(value._formattedText, UIControlState.UIControlStateNormal);
var newText = value ? value._formattedText : null;
this.ios.setAttributedTitleForState(newText, UIControlState.UIControlStateNormal);
this.style._updateTextDecoration();
}
}

View File

@ -96,7 +96,10 @@ export class TextBase extends view.View implements definition.TextBase, formatte
}
private onFormattedTextChanged(eventData: observable.PropertyChangeData) {
this._setFormattedTextPropertyToNative(eventData.value);
var value = (<formattedString.FormattedString>eventData.value);
this._setFormattedTextPropertyToNative(value);
this._onPropertyChangedFromNative(TextBase.textProperty, value.toString());
}
public _onTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
@ -108,10 +111,14 @@ export class TextBase extends view.View implements definition.TextBase, formatte
}
public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
if (data.newValue) {
(<formattedString.FormattedString>data.newValue).parent = this;
var newValue = (<formattedString.FormattedString>data.newValue);
if (newValue) {
newValue.parent = this;
}
this._setFormattedTextPropertyToNative(data.newValue);
this._setFormattedTextPropertyToNative(newValue);
var newText = newValue ? newValue.toString() : "";
this._onPropertyChangedFromNative(TextBase.textProperty, newText);
}
public _addChildFromBuilder(name: string, value: any): void {

View File

@ -10,8 +10,9 @@ export class TextBase extends common.TextBase {
}
}
public _setFormattedTextPropertyToNative(value) {
var newText = value ? value._formattedText : null;
if (this.android) {
this.android.setText(value._formattedText);
this.android.setText(newText);
}
}
}

View File

@ -11,7 +11,8 @@ export class TextBase extends common.TextBase {
}
public _setFormattedTextPropertyToNative(value) {
this.ios.attributedText = value._formattedText;
var newText = value ? value._formattedText : null;
this.ios.attributedText = newText;
this.style._updateTextDecoration();
this.style._updateTextTransform();
this.requestLayout();