From bccd5133a35f3487e8b0b95cf066fdf886cd21bc Mon Sep 17 00:00:00 2001 From: vakrilov Date: Fri, 7 Aug 2015 11:44:16 +0300 Subject: [PATCH] FIX: Nested templates are not parsed --- .../xml-declaration/xml-declaration-tests.ts | 45 +++++++++++++++++++ ui/builder/builder.ts | 6 +-- ui/builder/template-builder.d.ts | 1 + ui/builder/template-builder.ts | 18 ++++++-- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index a10d08e38..cdc6bb1c8 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -476,3 +476,48 @@ export function test_parse_ShouldParseNestedListViewInListViewTemplate() { helper.goBack(); } } + +export function test_parse_NestedRepeaters() { + var pageXML = + "" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + ""; + var p = builder.parse(pageXML); + + function testAction(views: Array) { + p.bindingContext = [["0", "1"], ["2", "3"]]; + TKUnit.wait(0.2); + + var lbls = new Array(); + view.eachDescendant(p, (v) => { + if (v instanceof labelModule.Label) { + lbls.push(v); + } + return true; + }); + + TKUnit.assertEqual(lbls.length, 4, "labels count"); + lbls.forEach((lbl, index, arr) => { + TKUnit.assertEqual(lbl.text.toString(), index.toString(), "label text"); + }); + }; + + helper.navigate(function () { return p; }); + + try { + testAction([p.content, p]); + } + finally { + helper.goBack(); + } +} diff --git a/ui/builder/builder.ts b/ui/builder/builder.ts index 343f143f4..133e198a8 100644 --- a/ui/builder/builder.ts +++ b/ui/builder/builder.ts @@ -79,13 +79,13 @@ function parseInternal(value: string, context: any): componentBuilder.ComponentM if (args.eventType === xml.ParserEventType.StartElement) { templateBuilder.addStartElement(args.prefix, args.namespace, args.elementName, args.attributes); } else if (args.eventType === xml.ParserEventType.EndElement) { - if (templateBuilder.elementName !== args.elementName) { - templateBuilder.addEndElement(args.prefix, args.elementName); - } else { + templateBuilder.addEndElement(args.prefix, args.elementName); + if (templateBuilder.hasFinished()) { templateBuilder.build(); templateBuilder = undefined; } } + return; } // Get the current parent. diff --git a/ui/builder/template-builder.d.ts b/ui/builder/template-builder.d.ts index 49aa369dc..4a8e6439e 100644 --- a/ui/builder/template-builder.d.ts +++ b/ui/builder/template-builder.d.ts @@ -9,6 +9,7 @@ declare module "ui/builder/template-builder" { addStartElement(prefix: string, namespace: string, elementName: string, attributes: Object); addEndElement(prefix: string, elementName: string); build(); + hasFinished(); } export function isKnownTemplate(name: string, exports: any): boolean; diff --git a/ui/builder/template-builder.ts b/ui/builder/template-builder.ts index 4510ba2fd..b5dcfbeb2 100644 --- a/ui/builder/template-builder.ts +++ b/ui/builder/template-builder.ts @@ -5,10 +5,12 @@ var KNOWNTEMPLATES = "knownTemplates"; export class TemplateBuilder { private _items: Array; private _templateProperty: definition.TemplateProperty; + private _nestingLevel: number; constructor(templateProperty: definition.TemplateProperty) { this._items = new Array(); this._templateProperty = templateProperty; + this._nestingLevel = 0; } public get elementName(): string { @@ -16,15 +18,23 @@ export class TemplateBuilder { } public addStartElement(prefix: string, namespace: string, elementName: string, attributes: Object) { + this._nestingLevel++; this._items.push("<" + - getElementNameWithPrefix(prefix, elementName) + - (namespace ? " " + getNamespace(prefix, namespace) : "") + - (attributes ? " " + getAttributesAsString(attributes) : "") + + getElementNameWithPrefix(prefix, elementName) + + (namespace ? " " + getNamespace(prefix, namespace) : "") + + (attributes ? " " + getAttributesAsString(attributes) : "") + ">"); } public addEndElement(prefix: string, elementName: string) { - this._items.push(""); + this._nestingLevel--; + if (!this.hasFinished()) { + this._items.push(""); + } + } + + public hasFinished() { + return this._nestingLevel < 0; } public build() {