mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
platform specific component declaration support for UI builder + tests
This commit is contained in:
@ -142,6 +142,16 @@ export function test_parse_ShouldParsePlatformSpecificProperties() {
|
||||
}
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParsePlatformSpecificComponents() {
|
||||
var p = <page.Page>builder.parse("<Page><ios><TextField /></ios><android><Label /></android></Page>");
|
||||
if (platform.device.os === platform.platformNames.ios) {
|
||||
TKUnit.assert(p.content instanceof textFieldModule.TextField, "Expected result: TextField; Actual result: " + p.content);
|
||||
}
|
||||
else {
|
||||
TKUnit.assert(p.content instanceof labelModule.Label, "Expected result: Label; Actual result: " + p.content);
|
||||
}
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindings() {
|
||||
var p = <page.Page>builder.parse("<Page><Switch checked='{{ myProp }}' /></Page>");
|
||||
p.bindingContext = { myProp: true };
|
||||
|
@ -5,9 +5,19 @@ import file_access_module = require("file-system/file-system-access");
|
||||
import types = require("utils/types");
|
||||
import componentBuilder = require("ui/builder/component-builder");
|
||||
import templateBuilderDef = require("ui/builder/template-builder");
|
||||
import platform = require("platform");
|
||||
|
||||
var KNOWNCOLLECTIONS = "knownCollections";
|
||||
|
||||
function isPlatform(value: string): boolean {
|
||||
return value && (value.toLowerCase() === platform.platformNames.android.toLowerCase()
|
||||
|| value.toLowerCase() === platform.platformNames.ios.toLowerCase());
|
||||
}
|
||||
|
||||
function isCurentPlatform(value: string): boolean {
|
||||
return value && value.toLowerCase() === platform.device.os.toLowerCase();
|
||||
}
|
||||
|
||||
export function parse(value: string, exports: any): view.View {
|
||||
var viewToReturn: view.View;
|
||||
|
||||
@ -32,9 +42,29 @@ function parseInternal(value: string, exports: any): componentBuilder.ComponentM
|
||||
|
||||
var templateBuilder: templateBuilderDef.TemplateBuilder;
|
||||
|
||||
var currentPlatformContext: string;
|
||||
|
||||
// Parse the XML.
|
||||
var xmlParser = new xml.XmlParser((args: xml.ParserEvent) => {
|
||||
|
||||
if (args.eventType === xml.ParserEventType.StartElement) {
|
||||
if (isPlatform(args.elementName)) {
|
||||
currentPlatformContext = args.elementName;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentPlatformContext && !isCurentPlatform(currentPlatformContext)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.eventType === xml.ParserEventType.EndElement) {
|
||||
if (isPlatform(args.elementName)) {
|
||||
currentPlatformContext = undefined;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (templateBuilder) {
|
||||
if (args.eventType === xml.ParserEventType.StartElement) {
|
||||
templateBuilder.addStartElement(args.prefix, args.namespace, args.elementName, args.attributes);
|
||||
|
Reference in New Issue
Block a user