Skip to content

Commit 184f887

Browse files
authored
fix(renderer): support namespaced attributes (#719)
Platform attributes (such as :ios and :android) are passed as namespaces to the renderer in Angular 4. Caused by: angular/angular#14874.
1 parent 8cf6e6c commit 184f887

File tree

4 files changed

+18
-35
lines changed

4 files changed

+18
-35
lines changed

nativescript-angular/renderer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ export class NativeScriptRenderer extends Renderer2 {
151151
// TODO: handle this when we resolve routing and navigation.
152152
}
153153

154-
setAttribute(view: NgView, name: string, value: string) {
155-
traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}`);
156-
return this.setProperty(view, name, value);
154+
setAttribute(view: NgView, name: string, value: string, namespace?: string) {
155+
traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}, namespace: ${namespace}`);
156+
return this.viewUtil.setProperty(view, name, value, namespace);
157157
}
158158

159159
removeAttribute(_el: NgView, _name: string): void {
@@ -162,7 +162,7 @@ export class NativeScriptRenderer extends Renderer2 {
162162

163163
setProperty(view: any, name: string, value: any) {
164164
traceLog(`NativeScriptRenderer.setProperty ${view} : ${name} = ${value}`);
165-
this.viewUtil.setProperty(view, name, value);
165+
return this.viewUtil.setProperty(view, name, value);
166166
}
167167

168168
addClass(view: NgView, name: string): void {

nativescript-angular/view-util.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import { ValueSource } from "ui/core/dependency-observable";
1616
import { platformNames, Device } from "platform";
1717
import { rendererLog as traceLog, styleError } from "./trace";
1818

19-
const IOS_PREFX: string = "ios-";
20-
const ANDROID_PREFX: string = "android-";
2119
const XML_ATTRIBUTES = Object.freeze(["style", "rows", "columns", "fontAttributes"]);
2220
const whiteSpaceSplitter = /\s+/;
2321

@@ -149,30 +147,8 @@ export class ViewUtil {
149147
return view;
150148
}
151149

152-
private platformFilter(attribute: string): string {
153-
let lowered = attribute.toLowerCase();
154-
if (lowered.indexOf(IOS_PREFX) === 0) {
155-
if (this.isIos) {
156-
return attribute.substr(IOS_PREFX.length);
157-
} else {
158-
return null;
159-
}
160-
}
161-
162-
if (lowered.indexOf(ANDROID_PREFX) === 0) {
163-
if (this.isAndroid) {
164-
return attribute.substr(ANDROID_PREFX.length);
165-
} else {
166-
return null;
167-
}
168-
}
169-
170-
return attribute;
171-
}
172-
173-
public setProperty(view: NgView, attributeName: string, value: any): void {
174-
attributeName = this.platformFilter(attributeName);
175-
if (!attributeName) {
150+
public setProperty(view: NgView, attributeName: string, value: any, namespace?: string): void {
151+
if (namespace && !this.runsIn(namespace)) {
176152
return;
177153
}
178154

@@ -223,6 +199,12 @@ export class ViewUtil {
223199
return found ? index : -1;
224200
}
225201

202+
private runsIn(platform: string): boolean {
203+
return (platform === "ios" && this.isIos) ||
204+
(platform === "android" && this.isAndroid);
205+
}
206+
207+
226208
private setPropertyInternal(view: NgView, attributeName: string, value: any): void {
227209
traceLog("Setting attribute: " + attributeName);
228210

@@ -354,3 +336,4 @@ export class ViewUtil {
354336
}
355337
}
356338
}
339+

tests/app/tests/platform-filter-components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class AndroidSpecificComponent {
2929
@Component({
3030
template: `
3131
<StackLayout>
32-
<Label android-text="ANDROID" ios-text="IOS"></Label>
32+
<Label android:text="ANDROID" ios:text="IOS"></Label>
3333
</StackLayout>`
3434
})
3535
export class PlatformSpecificAttributeComponent {

tests/app/tests/property-sets.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,28 @@ describe("setting View properties", () => {
8787
it("sets ios property in ios", () => {
8888
let view = new TestView();
8989
let testUtil = new ViewUtil(iosDevice);
90-
testUtil.setProperty(view, "ios-anyValue", "blah");
90+
testUtil.setProperty(view, "anyValue", "blah", "ios");
9191
assert.strictEqual("blah", view.anyValue);
9292
});
9393

9494
it("doesn\'t set android property in ios", () => {
9595
let view = new TestView();
9696
let testUtil = new ViewUtil(iosDevice);
97-
testUtil.setProperty(view, "android-anyValue", "blah");
97+
testUtil.setProperty(view, "anyValue", "blah", "android");
9898
assert.isUndefined(view.anyValue);
9999
});
100100

101101
it("sets android property in android", () => {
102102
let view = new TestView();
103103
let testUtil = new ViewUtil(androidDevice);
104-
testUtil.setProperty(view, "android-anyValue", "blah");
104+
testUtil.setProperty(view, "anyValue", "blah", "android");
105105
assert.strictEqual("blah", view.anyValue);
106106
});
107107

108108
it("doesn\'t set ios property in android", () => {
109109
let view = new TestView();
110110
let testUtil = new ViewUtil(androidDevice);
111-
testUtil.setProperty(view, "ios-anyValue", "blah");
111+
testUtil.setProperty(view, "anyValue", "blah", "ios");
112112
assert.isUndefined(view.anyValue);
113113
});
114114
});

0 commit comments

Comments
 (0)