Skip to content

Fix runtime renderers rendering of certain sections #2674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/src/mustachio/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ class MustachioParser {
keySpanEndOffset - lastName.length, keySpanEndOffset);
var section = Section([lastName], children,
invert: invert, span: span, keySpan: lastNameSpan);
//for (var sectionKey in parsedKey.names.reversed.skip(1)) {
for (var i = parsedKey.names.length - 2; i >= 0; i--) {
var sectionKey = parsedKey.names[i];
// To find the start offset of the ith name, take the length of all of
Expand Down
16 changes: 14 additions & 2 deletions lib/src/mustachio/renderer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ abstract class RendererBase<T> {
Template _template;

/// The output buffer into which [context] is rendered, using a template.
final buffer = StringBuffer();
// TODO(srawlins): Pass around a single [StringBuffer], and make this field
// `final`.
StringBuffer buffer = StringBuffer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can we not pass buffers into the RendererBase and share them, rather than requiring them to be different for each object? Or, further, maybe this could be part of the idea @kevmoo suggested of just having a stream that we add to.

I'm OK with a big honking TODO/issue filed on cleaning this up, but I think the need for a withBuffer really points to a refactoring using streams being the way to handle this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any need for an asynchronous stream API. I've added a TODO to pass around a single StringBuffer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was thinking StreamSink accessed synchronously. Async definitely seems like overkill.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting.


final Set<String> _invisibleGetters;

Expand Down Expand Up @@ -234,7 +236,7 @@ abstract class RendererBase<T> {
"Failed to resolve '$key' as a property on any types in the "
'current context'));
} else {
return parent.section(node);
return parent.withBuffer(buffer, () => parent.section(node));
}
}

Expand Down Expand Up @@ -279,6 +281,16 @@ abstract class RendererBase<T> {
renderBlock(partialTemplate.ast);
_template = outerTemplate;
}

/// Executes [fn] after replacing [buffer] with [newBuffer].
///
/// Replaces the previous buffer as [buffer].
void withBuffer(StringBuffer newBuffer, void Function() fn) {
var previousBuffer = buffer;
buffer = newBuffer;
fn();
buffer = previousBuffer;
}
}

String renderSimple(Object context, List<MustachioNode> ast, Template template,
Expand Down
9 changes: 9 additions & 0 deletions test/mustachio/aot_compiler_render_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ void main() {
expect(output, equals('Text Foo: hello'));
});

test('Renderer renders a value section node keyed lower in the stack',
() async {
var output = await renderBar({
'foo|lib/templates/html/bar.html':
'Text {{#foo}}One {{#s2}}Two{{/s2}}{{/foo}}',
}, '_i1.Bar()..foo = _i1.Foo()..s2 = "hello"');
expect(output, equals('Text One Two'));
});

test('Renderer renders a null value section node as blank', () async {
var output = await renderFoo({
'foo|lib/templates/html/foo.html':
Expand Down
11 changes: 11 additions & 0 deletions test/mustachio/runtime_renderer_render_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ void main() {
expect(renderBar(bar, barTemplate), equals('Text Foo: hello'));
});

test('Renderer renders a value section node keyed lower in the stack',
() async {
var barTemplateFile = getFile('/project/bar.mustache')
..writeAsStringSync('Text {{#foo}}One {{#s2}}Two{{/s2}}{{/foo}}');
var barTemplate = await Template.parse(barTemplateFile);
var bar = Bar()
..foo = Foo()
..s2 = 'hello';
expect(renderBar(bar, barTemplate), equals('Text One Two'));
});

test('Renderer renders a null value section node as blank', () async {
var fooTemplateFile = getFile('/project/foo.mustache')
..writeAsStringSync('Text {{#s1}}"{{.}}" ({{length}}){{/s1}}');
Expand Down