Allow @ for passing values to elements

This commit is contained in:
Tobe O 2018-06-27 21:23:38 -04:00
parent dd205ea693
commit 2339bfa92b
5 changed files with 36 additions and 8 deletions

View file

@ -1,3 +1,6 @@
# 1.0.6+1
* Ensure `<element>` pass attributes.
# 1.0.6 # 1.0.6
* Add `index-as` to `for-each`. * Add `index-as` to `for-each`.
* Support registering + rendering custom elements. * Support registering + rendering custom elements.

View file

@ -353,8 +353,10 @@ class Renderer {
var attrs = element.attributes.where((a) => a.name != 'as'); var attrs = element.attributes.where((a) => a.name != 'as');
for (var attribute in attrs) { for (var attribute in attrs) {
scope.create(attribute.name, if (attribute.name.startsWith('@')) {
value: attribute.value?.compute(scope), constant: true); scope.create(attribute.name.substring(1),
value: attribute.value?.compute(scope), constant: true);
}
} }
if (renderAs == false) { if (renderAs == false) {
@ -369,7 +371,8 @@ class Renderer {
var syntheticElement = new RegularElement( var syntheticElement = new RegularElement(
template.lt, template.lt,
new SyntheticIdentifier(tagName), new SyntheticIdentifier(tagName),
[], element.attributes
.where((a) => a.name != 'as' && !a.name.startsWith('@')),
template.gt, template.gt,
template.children, template.children,
template.lt2, template.lt2,

View file

@ -6,7 +6,7 @@ import '../ast/ast.dart';
final RegExp _whitespace = new RegExp(r'[ \n\r\t]+'); final RegExp _whitespace = new RegExp(r'[ \n\r\t]+');
final RegExp _id = final RegExp _id =
new RegExp(r'(([A-Za-z][A-Za-z0-9_]*-)*([A-Za-z][A-Za-z0-9_]*))'); new RegExp(r'@?(([A-Za-z][A-Za-z0-9_]*-)*([A-Za-z][A-Za-z0-9_]*))');
final RegExp _string1 = new RegExp( final RegExp _string1 = new RegExp(
r"'((\\(['\\/bfnrt]|(u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])))|([^'\\]))*'"); r"'((\\(['\\/bfnrt]|(u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])))|([^'\\]))*'");
final RegExp _string2 = new RegExp( final RegExp _string2 = new RegExp(

View file

@ -1,5 +1,5 @@
name: jael name: jael
version: 1.0.6 version: 1.0.6+1
description: A simple server-side HTML templating engine for Dart. description: A simple server-side HTML templating engine for Dart.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/jael/tree/master/jael homepage: https://github.com/angel-dart/jael/tree/master/jael

View file

@ -11,7 +11,7 @@ void main() {
<element name="square-root"> <element name="square-root">
The square root of {{ n }} is {{ sqrt(n).toInt() }}. The square root of {{ n }} is {{ sqrt(n).toInt() }}.
</element> </element>
<square-root n=16 /> <square-root @n=16 />
</div> </div>
'''; ''';
@ -33,7 +33,7 @@ void main() {
<element name="square-root"> <element name="square-root">
The square root of {{ n }} is {{ sqrt(n).toInt() }}. The square root of {{ n }} is {{ sqrt(n).toInt() }}.
</element> </element>
<square-root as="span" n=16 /> <square-root as="span" @n=16 />
</div> </div>
'''; ''';
@ -49,13 +49,35 @@ void main() {
'''.trim()); '''.trim());
}); });
test('pass attributes', () {
var template = '''
<div>
<element name="square-root">
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
</element>
<square-root foo="bar" baz="quux" @n=16 />
</div>
''';
var html = render(template, {'sqrt': sqrt});
print(html);
expect(html, '''
<div>
<div foo="bar" baz="quux">
The square root of 16 is 4.
</div>
</div>
'''.trim());
});
test('render without tag name', () { test('render without tag name', () {
var template = ''' var template = '''
<div> <div>
<element name="square-root"> <element name="square-root">
The square root of {{ n }} is {{ sqrt(n).toInt() }}. The square root of {{ n }} is {{ sqrt(n).toInt() }}.
</element> </element>
<square-root as=false n=16 /> <square-root as=false @n=16 />
</div> </div>
'''; ''';