Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 1f8d0bd

Browse files
author
Jare Fagbemi
committed
feat(TestBed): add whitespace handling to compile
Add whitespace handling to the TestBed’s compile function. Closes #1262. Previously, strings passed with leading or trailing whitespace would be compiled incorrectly. Now, valid HTML with arbitrary whitespace in and surrounding it can be compiled properly.
1 parent c2b4314 commit 1f8d0bd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/mock/test_bed.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,22 @@ class TestBed {
5454
return rootElement;
5555
}
5656

57+
String _handleWhitespace(html) {
58+
return html
59+
.split('\n')
60+
.map((line) {
61+
var trimmed = line.trim();
62+
return trimmed +
63+
(trimmed.isEmpty || trimmed.endsWith('>') ? '' : ' ');
64+
})
65+
.join();
66+
}
5767
/**
5868
* Convert an [html] String to a [List] of [Element]s.
5969
*/
6070
List<Element> toNodeList(html) {
6171
var div = new DivElement();
62-
var sanitizedHtml = html.split('\n').map((line) => line.trim()).join(' ');
72+
var sanitizedHtml = _handleWhitespace(html);
6373
div.setInnerHtml(sanitizedHtml, treeSanitizer: new NullTreeSanitizer());
6474
var nodes = [];
6575
for (var node in div.nodes) {

test/mock/test_bed_spec.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ void main() {
2727
expect(directiveInst.destroyed).toBe(true);
2828
});
2929

30+
it('should handle whitespace cleanly', () {
31+
var root = _.compile('''
32+
<div>
33+
<h1
34+
attr="Hi">
35+
</h1>
36+
</div>
37+
<span
38+
attr2="Bye"
39+
40+
></span>
41+
''');
42+
43+
expect(root.runtimeType).toBe(DivElement);
44+
expect(_.rootElements[1].runtimeType).toBe(SpanElement);
45+
expect(_.rootElements.length).toBe(2);
46+
});
47+
3048
});
3149
}
3250

0 commit comments

Comments
 (0)