diff --git a/jael/lib/src/renderer.dart b/jael/lib/src/renderer.dart index 81084bdc..9ee98303 100644 --- a/jael/lib/src/renderer.dart +++ b/jael/lib/src/renderer.dart @@ -46,6 +46,9 @@ class Renderer { } else if (element.tagName.name == 'declare') { renderDeclare(element, buffer, childScope, html5); return; + } else if (element.tagName.name == 'switch') { + renderSwitch(element, buffer, childScope, html5); + return; } buffer..write('<')..write(element.tagName.name); @@ -161,7 +164,8 @@ class Renderer { renderElement(strippedElement, buffer, scope, html5); } - void renderDeclare(Element element, CodeBuffer buffer, SymbolTable scope, bool html5) { + void renderDeclare( + Element element, CodeBuffer buffer, SymbolTable scope, bool html5) { for (var attribute in element.attributes) { scope.add(attribute.name, value: attribute.value?.compute(scope), constant: true); @@ -174,6 +178,43 @@ class Renderer { } } + void renderSwitch( + Element element, CodeBuffer buffer, SymbolTable scope, bool html5) { + var value = element.attributes + .firstWhere((a) => a.name == 'value', orElse: () => null) + ?.value + ?.compute(scope); + + var cases = + element.children.where((c) => c is Element && c.tagName.name == 'case'); + + for (Element child in cases) { + var comparison = child.attributes + .firstWhere((a) => a.name == 'value', orElse: () => null) + ?.value + ?.compute(scope); + if (comparison == value) { + for (int i = 0; i < child.children.length; i++) { + var c = child.children.elementAt(i); + renderElementChild(c, buffer, scope, html5, i, child.children.length); + } + + return; + } + } + + Element defaultCase = element.children.firstWhere( + (c) => c is Element && c.tagName.name == 'default', + orElse: () => null); + if (defaultCase != null) { + for (int i = 0; i < defaultCase.children.length; i++) { + var child = defaultCase.children.elementAt(i); + renderElementChild( + child, buffer, scope, html5, i, defaultCase.children.length); + } + } + } + void renderElementChild(ElementChild child, CodeBuffer buffer, SymbolTable scope, bool html5, int index, int total) { if (child is Text) { diff --git a/jael/pubspec.yaml b/jael/pubspec.yaml index 78d43094..eb95bba2 100644 --- a/jael/pubspec.yaml +++ b/jael/pubspec.yaml @@ -1,5 +1,5 @@ name: jael -version: 1.0.0-alpha+4 +version: 1.0.0-alpha+5 description: A simple server-side HTML templating engine for Dart. author: Tobe O homepage: https://github.com/angel-dart/jael/tree/master/jael diff --git a/jael/test/render/render_test.dart b/jael/test/render/render_test.dart index 67532822..83e6dd5c 100644 --- a/jael/test/render/render_test.dart +++ b/jael/test/render/render_test.dart @@ -266,6 +266,60 @@ main() { ''' .trim()); }); + + test('switch', () { + const template = ''' + + + BAN HAMMER LOLOL + + + You are in good standing. + + + Weird... + + +'''; + + var buf = new CodeBuffer(); + var document = jael.parseDocument(template, sourceUrl: 'test.jl'); + var scope = new SymbolTable(values: { + 'account': new _Account(isDisabled: true), + }); + + const jael.Renderer().render(document, buf, scope); + print(buf); + + expect(buf.toString().trim(), 'BAN HAMMER LOLOL'); + }); + + test('default', () { + const template = ''' + + + BAN HAMMER LOLOL + + + You are in good standing. + + + Weird... + + +'''; + + var buf = new CodeBuffer(); + var document = jael.parseDocument(template, sourceUrl: 'test.jl'); + var scope = new SymbolTable(values: { + 'account': new _Account(isDisabled: null), + }); + + const jael.Renderer().render(document, buf, scope); + print(buf); + + expect(buf.toString().trim(), 'Weird...'); + }); } const List<_Pokemon> starters = const [ @@ -279,3 +333,9 @@ class _Pokemon { const _Pokemon(this.name, this.type); } + +class _Account { + final bool isDisabled; + + _Account({this.isDisabled}); +}