@@ -30,6 +30,8 @@ class ShadowDomComponentFactory implements ComponentFactory {
30
30
31
31
ShadowDomComponentFactory (this ._expando);
32
32
33
+ final Map <String , async.Future <dom.StyleElement >> _styleElementCache = {};
34
+
33
35
FactoryFn call (dom.Node node, DirectiveRef ref) {
34
36
return (Injector injector) {
35
37
var component = ref.annotation as Component ;
@@ -41,7 +43,7 @@ class ShadowDomComponentFactory implements ComponentFactory {
41
43
NgBaseCss baseCss = injector.get (NgBaseCss );
42
44
// This is a bit of a hack since we are returning different type then we are.
43
45
var componentFactory = new _ComponentFactory (node, ref.type, component,
44
- injector.get (dom.NodeTreeSanitizer ), _expando, baseCss);
46
+ injector.get (dom.NodeTreeSanitizer ), _expando, baseCss, _styleElementCache );
45
47
var controller = componentFactory.call (injector, scope, viewCache, http, templateCache,
46
48
directives);
47
49
@@ -65,14 +67,15 @@ class _ComponentFactory implements Function {
65
67
final dom.NodeTreeSanitizer treeSanitizer;
66
68
final Expando _expando;
67
69
final NgBaseCss _baseCss;
70
+ final Map <String , async.Future <dom.StyleElement >> _styleElementCache;
68
71
69
72
dom.ShadowRoot shadowDom;
70
73
Scope shadowScope;
71
74
Injector shadowInjector;
72
75
var controller;
73
76
74
77
_ComponentFactory (this .element, this .type, this .component, this .treeSanitizer,
75
- this ._expando, this ._baseCss);
78
+ this ._expando, this ._baseCss, this ._styleElementCache );
76
79
77
80
dynamic call (Injector injector, Scope scope,
78
81
ViewCache viewCache, Http http, TemplateCache templateCache,
@@ -87,28 +90,24 @@ class _ComponentFactory implements Function {
87
90
// styles all over the page. We shouldn't be doing browsers work,
88
91
// so change back to using @import once Chrome bug is fixed or a
89
92
// better work around is found.
90
- List <async.Future <String >> cssFutures = new List () ;
93
+ Iterable <async.Future <dom. StyleElement >> cssFutures;
91
94
var cssUrls = []..addAll (_baseCss.urls)..addAll (component.cssUrls);
92
95
if (cssUrls.isNotEmpty) {
93
- cssUrls.forEach ((css) => cssFutures.add (http
94
- .get (css, cache: templateCache).then (
95
- (resp) => resp.responseText,
96
- onError: (e) => '/*\n $e \n */\n ' )
97
- ));
96
+ cssFutures = cssUrls.map ((cssUrl) => _styleElementCache.putIfAbsent (cssUrl, () =>
97
+ http.get (cssUrl, cache: templateCache)
98
+ .then ((resp) => resp.responseText,
99
+ onError: (e) => '/*\n $e \n */\n ' )
100
+ .then ((styleContent) => new dom.StyleElement ()..appendText (styleContent))
101
+ )).toList ();
98
102
} else {
99
- cssFutures. add ( new async .Future .value (null )) ;
103
+ cssFutures = [ new async .Future .value (null )] ;
100
104
}
101
105
var viewFuture = ComponentFactory ._viewFuture (component, viewCache, directives);
102
106
TemplateLoader templateLoader = new TemplateLoader (
103
107
async .Future .wait (cssFutures).then ((Iterable <String > cssList) {
104
- if (cssList != null ) {
105
- shadowDom.setInnerHtml (
106
- cssList
107
- .where ((css) => css != null )
108
- .map ((css) => '<style>$css </style>' )
109
- .join ('' ),
110
- treeSanitizer: treeSanitizer);
111
- }
108
+ cssList
109
+ .where ((styleElement) => styleElement != null )
110
+ .forEach ((styleElement) => shadowDom.append (styleElement.clone (true )));
112
111
if (viewFuture != null ) {
113
112
return viewFuture.then ((ViewFactory viewFactory) {
114
113
return (! shadowScope.isAttached) ?
0 commit comments