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

Commit

Permalink
feat(components): implement css encapsulation for transcluding compon…
Browse files Browse the repository at this point in the history
…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
vsavkin committed Sep 2, 2014
1 parent fb7a3aa commit 78175c4
Show file tree
Hide file tree
Showing 27 changed files with 1,369 additions and 400 deletions.
69 changes: 69 additions & 0 deletions lib/core_dom/component_css_loader.dart
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;
}
Loading

0 comments on commit 78175c4

Please sign in to comment.