This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): implement css encapsulation for transcluding compon…
…ents * Extract ComponentCssLoader so it can be shared by ShadowDomComponentFactory and TranscludingComponentFactory * Provide a built-in implementation of WebPlatformShim using css_shim * Implement a CSS transformer similar to the one provided by Platform.JS * Rename EventHandler into ShadowBoundary
- Loading branch information
Showing
27 changed files
with
1,369 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
part of angular.core.dom_internal; | ||
|
||
class ComponentCssLoader { | ||
final Http http; | ||
final TemplateCache templateCache; | ||
final WebPlatformShim platformShim; | ||
final ComponentCssRewriter componentCssRewriter; | ||
final dom.NodeTreeSanitizer treeSanitizer; | ||
final Map<_ComponentAssetKey, async.Future<dom.StyleElement>> styleElementCache; | ||
|
||
ComponentCssLoader(this.http, this.templateCache, this.platformShim, | ||
this.componentCssRewriter, this.treeSanitizer, | ||
this.styleElementCache); | ||
|
||
async.Future<List<dom.StyleElement>> call(String tag, List<String> cssUrls) => | ||
async.Future.wait(cssUrls.map((url) => _styleElement(tag, url))); | ||
|
||
async.Future<dom.StyleElement> _styleElement(String tag, String cssUrl) { | ||
final element = styleElementCache.putIfAbsent( | ||
new _ComponentAssetKey(tag, cssUrl), | ||
() => _loadNewCss(tag, cssUrl)); | ||
return element; | ||
} | ||
|
||
async.Future _loadNewCss(String tag, String cssUrl) { | ||
return _fetch(cssUrl) | ||
.then((css) => _shim(css, tag, cssUrl)) | ||
.then(_buildStyleElement); | ||
} | ||
|
||
async.Future<String> _fetch(String cssUrl) { | ||
return http.get(cssUrl, cache: templateCache) | ||
.then((resp) => resp.responseText, onError: (e) => '/* $e */'); | ||
} | ||
|
||
String _shim(String css, String tag, String cssUrl) { | ||
final shimmed = platformShim.shimCss(css, selector: tag, cssUrl: cssUrl); | ||
return componentCssRewriter(shimmed, selector: tag, cssUrl: cssUrl); | ||
} | ||
|
||
dom.StyleElement _buildStyleElement(String css) { | ||
var styleElement = new dom.StyleElement()..appendText(css); | ||
treeSanitizer.sanitizeTree(styleElement); | ||
return styleElement; | ||
} | ||
} | ||
|
||
class _ComponentAssetKey { | ||
final String tag; | ||
final String assetUrl; | ||
|
||
final String _key; | ||
|
||
_ComponentAssetKey(String tag, String assetUrl) | ||
: _key = "$tag|$assetUrl", | ||
this.tag = tag, | ||
this.assetUrl = assetUrl; | ||
|
||
@override | ||
String toString() => _key; | ||
|
||
@override | ||
int get hashCode => _key.hashCode; | ||
|
||
bool operator ==(key) => | ||
key is _ComponentAssetKey | ||
&& tag == key.tag | ||
&& assetUrl == key.assetUrl; | ||
} |
Oops, something went wrong.