From b38d181b67a37d3c1ee10529b57229f660a6e66a Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Wed, 17 Jun 2015 10:51:32 +0300 Subject: [PATCH] platform specific component declaration support for UI builder + tests --- .../xml-declaration/xml-declaration-tests.ts | 10 +++++++ ui/builder/builder.ts | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 1c0c60f0a..a932e97b5 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -142,6 +142,16 @@ export function test_parse_ShouldParsePlatformSpecificProperties() { } }; +export function test_parse_ShouldParsePlatformSpecificComponents() { + var p = builder.parse(""); + 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 = builder.parse(""); p.bindingContext = { myProp: true }; diff --git a/ui/builder/builder.ts b/ui/builder/builder.ts index b68cad7ac..76c1887a3 100644 --- a/ui/builder/builder.ts +++ b/ui/builder/builder.ts @@ -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);