Set contentStart/End on SectionNode
diff --git a/lib/src/lambda_context.dart b/lib/src/lambda_context.dart
index 13f8f6a..465dc65 100644
--- a/lib/src/lambda_context.dart
+++ b/lib/src/lambda_context.dart
@@ -60,17 +60,15 @@
if (_node is! SectionNode) return '';
- var nodes = (_node as SectionNode).children;
+ SectionNode node = _node;
+
+ var nodes = node.children;
if (nodes.isEmpty) return '';
- if (nodes.length == 1 && nodes.first is TextNode)
- return nodes.first.text;
+ if (nodes.length == 1 && nodes.first is TextNode) return nodes.first.text;
- var source = _context.source.substring(
- _node.contentStart, _node.contentEnd);
-
- return source;
+ return _context.source.substring(node.contentStart, node.contentEnd);
}
/// Evaluate the string as a mustache template using the current context.
diff --git a/lib/src/node.dart b/lib/src/node.dart
index 5228486..670f453 100644
--- a/lib/src/node.dart
+++ b/lib/src/node.dart
@@ -33,11 +33,7 @@
// or inverse section, then this stores the start of the content of the
// section.
final int start;
- final int end;
-
- int contentStart;
- int contentEnd;
-
+ final int end;
}
@@ -157,16 +153,17 @@
SectionNode(this.name, int start, int end, this.delimiters,
{this.inverse: false})
- : super(start, end);
+ : contentStart = end,
+ super(start, end);
final String name;
final String delimiters;
final bool inverse;
- int contentStart;
- int contentEnd;
+ final int contentStart;
+ int contentEnd; // Set in parser when close tag is parsed.
final List<Node> children = <Node>[];
- toString() => '(SectionNode $name inverse: $inverse)';
+ toString() => '(SectionNode $name inverse: $inverse $start $end)';
// TODO Only used for testing.
//FIXME use deepequals in test for comparing children.
@@ -175,7 +172,8 @@
&& name == o.name
&& delimiters == o.delimiters
&& inverse == o.inverse
- && contentStart == o.contentEnd;
+ && start == o.start
+ && end == o.end;
// TODO hashcode. import quiver.
diff --git a/lib/src/parser.dart b/lib/src/parser.dart
index cd55bce..68c330c 100644
--- a/lib/src/parser.dart
+++ b/lib/src/parser.dart
@@ -96,9 +96,10 @@
// Close section tag
case '/':
if (tag.name != _stack.last.name) throw 'boom!'; //TODO error message.
- _stack.removeLast();
- break;
-
+ var node = _stack.removeLast();
+ node.contentEnd = tag.start;
+ break;
+
default:
if (node != null) _stack.last.children.add(node);
}
@@ -113,7 +114,10 @@
_currentDelimiters = _delimiters;
_stack.add(new SectionNode('root', 0, 0, _delimiters));
-
+
+ // Handle standalone tag on first line.
+ _parseLine();
+
for (var token = _peek(); token != null; token = _peek()) {
switch(token.type) {
@@ -134,8 +138,6 @@
_currentDelimiters = token.value;
break;
- //TODO think about this. It looks like this loop will usually just call
- // into parseLine(). May be able to simplify the logic.
case TokenType.lineEnd:
//TODO the first line can be a standalone line too, and there is
// no lineEnd. Perhaps _parseLine(firstLine: true)?
@@ -168,8 +170,11 @@
//TODO handle EOFs. i.e. check for null return from peek.
//TODO make this EOF handling clearer.
- assert(_peek().type == TokenType.lineEnd); //TODO expect.
- var precedingLineEnd = _read();
+ assert(_peek().type == TokenType.lineEnd || _i == 0); //TODO expect.
+
+ //TODO _readIf(TokenType) helper.
+ var precedingLineEnd = _peek() != null && _peek().type == TokenType.lineEnd
+ ? _read() : null;
// The scanner guarantees that there will only be a single whitespace token,
// there are never consecutive whitespace tokens.
@@ -197,7 +202,7 @@
// This is a standalone line, so do not create text nodes for whitespace,
// or the following newline.
-
+ _read();
_appendTag(tag, tagNode);
} else {
diff --git a/test/parser_test.dart b/test/parser_test.dart
index 4914886..d51ea6d 100644
--- a/test/parser_test.dart
+++ b/test/parser_test.dart
@@ -152,7 +152,7 @@
var nodes = parser.parse();
expect(nodes, orderedEquals([
new TextNode('abc\n', 0, 4),
- new SectionNode('foo', 4, 25, '{{ }}'),
+ new SectionNode('foo', 4, 12, '{{ }}'),
new TextNode('ghi', 26, 29)
]));
expect(nodes[1].children, orderedEquals([new TextNode('def\n', 13, 17)]));