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

Commit 7c23c4e

Browse files
Jare Fagbemijbdeboer
Jare Fagbemi
authored andcommitted
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. Closes #1346
1 parent e2350ca commit 7c23c4e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/mock/test_bed.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class TestBed {
6161
*/
6262
List<Element> toNodeList(html) {
6363
var div = new DivElement();
64-
div.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer());
64+
var sanitizedHtml = _handleWhitespace(html);
65+
div.setInnerHtml(sanitizedHtml, treeSanitizer: new NullTreeSanitizer());
6566
var nodes = [];
6667
for (var node in div.nodes) {
6768
nodes.add(node);
@@ -99,4 +100,12 @@ class TestBed {
99100
}
100101

101102
getScope(Node node) => getProbe(node).scope;
103+
104+
String _handleWhitespace(html) {
105+
return html.split('\n')
106+
.map((line) {
107+
var trimmed = line.trim();
108+
return trimmed + (trimmed.isEmpty || trimmed.endsWith('>') ? '' : ' ');})
109+
.join();
110+
}
102111
}

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).toBeAnInstanceOf(DivElement);
44+
expect(_.rootElements[1]).toBeAnInstanceOf(SpanElement);
45+
expect(_.rootElements.length).toBe(2);
46+
});
47+
3048
});
3149
}
3250

0 commit comments

Comments
 (0)