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

Commit 81bab3e

Browse files
vsavkinrkirov
authored andcommitted
fix(shadow_boundary): change ShadowBoundary not to reorder styles when prepending them
1 parent 013f18d commit 81bab3e

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

lib/core_dom/shadow_boundary.dart

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,41 @@ abstract class ShadowBoundary {
1616
if (elements.isEmpty) return;
1717

1818
final newStyles = _newStyles(elements);
19-
final cloned = newStyles.map((el) => el.clone(true));
20-
21-
cloned.forEach((style) {
22-
if (_lastStyleElement != null && !prepend) {
23-
_lastStyleElement = root.insertBefore(style, _lastStyleElement.nextNode);
24-
} else if (root.hasChildNodes()) {
25-
_lastStyleElement = root.insertBefore(style, root.firstChild);
26-
} else {
27-
_lastStyleElement = root.append(style);
28-
}
29-
});
19+
if (newStyles.isEmpty) return;
20+
21+
final cloned = newStyles.map((el) => el.clone(true)).toList();
22+
if (_lastStyleElement == null) {
23+
_insertFirstStyles(cloned);
24+
} else {
25+
_insertStyles(cloned, prepend);
26+
}
3027

3128
_addInsertedStyles(newStyles);
3229
}
3330

31+
_insertFirstStyles(List<dom.StyleElement> elements) {
32+
elements.reversed.forEach(_insertFrontNode);
33+
_lastStyleElement = elements.last;
34+
}
35+
36+
_insertStyles(List<dom.StyleElement> elements, bool prepend) {
37+
if (prepend) {
38+
elements.reversed.forEach(_insertFrontNode);
39+
} else {
40+
final next = _lastStyleElement.nextNode;
41+
root.insertAllBefore(elements, next);
42+
_lastStyleElement = elements.last;
43+
}
44+
}
45+
46+
_insertFrontNode(dom.StyleElement style) {
47+
if (root.hasChildNodes()) {
48+
return root.insertBefore(style, root.firstChild);
49+
} else {
50+
return root.append(style);
51+
}
52+
}
53+
3454
Iterable<dom.StyleElement> _newStyles(Iterable<dom.StyleElement> elements) {
3555
if (_insertedStyles == null) return elements;
3656
return elements.where((el) => !_insertedStyles.contains(el));

test/core_dom/shadow_boundary_spec.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ main() {
2626
expect(root).toHaveText(".style1{}.style2{}.style3{}");
2727
});
2828

29-
it("should prepend style elements before other style elements", () {
29+
it("should prepend style elements before other style elements (prepend, then append)", () {
30+
final s1 = new dom.StyleElement()..text = ".style1{}";
31+
final s2 = new dom.StyleElement()..text = ".style2{}";
32+
final s3 = new dom.StyleElement()..text = ".style3{}";
33+
34+
boundary.insertStyleElements([s1, s2], prepend: true);
35+
boundary.insertStyleElements([s3]);
36+
37+
expect(root).toHaveText(".style1{}.style2{}.style3{}");
38+
});
39+
40+
it("should prepend style elements before other style elements (append, then prepend)", () {
3041
final s1 = new dom.StyleElement()..text = ".style1{}";
3142
final s2 = new dom.StyleElement()..text = ".style2{}";
3243
final s3 = new dom.StyleElement()..text = ".style3{}";

0 commit comments

Comments
 (0)