This commit is contained in:
Tobe O 2017-10-02 12:29:13 -04:00
parent a8a45940ed
commit a5204161b9
3 changed files with 103 additions and 2 deletions

View file

@ -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) {

View file

@ -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 <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/jael/tree/master/jael

View file

@ -266,6 +266,60 @@ main() {
'''
.trim());
});
test('switch', () {
const template = '''
<switch value=account.isDisabled>
<case value=true>
BAN HAMMER LOLOL
</case>
<case value=false>
You are in good standing.
</case>
<default>
Weird...
</default>
</switch>
''';
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 = '''
<switch value=account.isDisabled>
<case value=true>
BAN HAMMER LOLOL
</case>
<case value=false>
You are in good standing.
</case>
<default>
Weird...
</default>
</switch>
''';
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});
}