@@ -16,21 +16,41 @@ abstract class ShadowBoundary {
16
16
if (elements.isEmpty) return ;
17
17
18
18
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
+ }
30
27
31
28
_addInsertedStyles (newStyles);
32
29
}
33
30
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
+
34
54
Iterable <dom.StyleElement > _newStyles (Iterable <dom.StyleElement > elements) {
35
55
if (_insertedStyles == null ) return elements;
36
56
return elements.where ((el) => ! _insertedStyles.contains (el));
0 commit comments