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

Commit

Permalink
feat(NgComponent): Support multiple css files
Browse files Browse the repository at this point in the history
  • Loading branch information
mvuksano authored and mhevery committed Dec 13, 2013
1 parent 6efde83 commit 6c6151c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
15 changes: 14 additions & 1 deletion lib/core/directive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ class NgComponent extends NgAnnotation {
final String cssUrl;

/**
* A list of CSS URLs to load into the shadow DOM.
*/
final List<String> cssUrls;

List<String> get allCssUrls {
if (cssUrls == null && cssUrl == null) return null;
if (cssUrls == null && cssUrl != null) return [cssUrl];
if (cssUrls != null && cssUrl == null) return cssUrls;
if (cssUrls != null && cssUrl != null) return [cssUrl]..addAll(cssUrls);
}

/**
* Set the shadow root applyAuthorStyles property. See shadow-DOM
* documentation for further details.
*/
Expand All @@ -212,6 +224,7 @@ class NgComponent extends NgAnnotation {
this.template,
this.templateUrl,
this.cssUrl,
this.cssUrls,
this.applyAuthorStyles,
this.resetStyleInheritance,
publishAs,
Expand All @@ -234,7 +247,7 @@ class NgComponent extends NgAnnotation {
new NgComponent(
template: this.template,
templateUrl: this.templateUrl,
cssUrl: this.cssUrl,
cssUrls: this.cssUrls,
applyAuthorStyles: this.applyAuthorStyles,
resetStyleInheritance: this.resetStyleInheritance,
publishAs: this.publishAs,
Expand Down
16 changes: 9 additions & 7 deletions lib/core_dom/block_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,23 @@ class _ComponentFactory {
// styles all over the page. We shouldn't be doing browsers work,
// so change back to using @import once Chrome bug is fixed or a
// better work around is found.
async.Future<String> cssFuture;
if (component.cssUrl != null) {
cssFuture = $http.getString(component.cssUrl, cache: $templateCache);
List<async.Future<String>> cssFutures = new List();
var cssUrls = component.allCssUrls;
if (cssUrls != null) {
cssUrls.forEach((css) => cssFutures.add( $http.getString(css, cache: $templateCache) ) );
} else {
cssFuture = new async.Future.value(null);
cssFutures.add( new async.Future.value(null) );
}
var blockFuture;
if (component.template != null) {
blockFuture = new async.Future.value($blockCache.fromHtml(component.template));
} else if (component.templateUrl != null) {
blockFuture = $blockCache.fromUrl(component.templateUrl);
}
TemplateLoader templateLoader = new TemplateLoader(cssFuture.then((String css) {
if (css != null) {
shadowDom.setInnerHtml('<style>$css</style>', treeSanitizer: treeSanitizer);
TemplateLoader templateLoader = new TemplateLoader( async.Future.wait(cssFutures).then((Iterable<String> cssList) {
if (cssList != null) {
var filteredCssList = cssList.where((css) => css != null );
shadowDom.setInnerHtml('<style>${filteredCssList.join('')}</style>', treeSanitizer: treeSanitizer);
}
if (blockFuture != null) {
return blockFuture.then((BlockFactory blockFactory) => attachBlockToShadowDom(blockFactory));
Expand Down
36 changes: 36 additions & 0 deletions test/core/templateurl_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class SimpleUrlComponent {
class HtmlAndCssComponent {
}

@NgComponent(
selector: 'html-and-css',
templateUrl: 'simple.html',
cssUrls: const ['simple.css', 'another.css'])
class HtmlAndMultipleCssComponent {
}

@NgComponent(
selector: 'inline-with-css',
template: '<div>inline!</div>',
Expand Down Expand Up @@ -170,4 +177,33 @@ main() => describe('template url', () {
expect(renderedText(element)).toEqual('.hello{}Simple!');
})));
});

describe('multiple css loading', () {
beforeEach(module((Module module) {
module.type(LogAttrDirective);
module.type(HtmlAndMultipleCssComponent);
}));

it('should load multiple CSS files into a style', async(inject((Http $http,
Compiler $compile, Scope $rootScope, Logger log, Injector injector,
MockHttpBackend backend) {
backend.expectGET('simple.css').respond('.hello{}');
backend.expectGET('another.css').respond('.world{}');
backend.expectGET('simple.html').respond('<div log="SIMPLE">Simple!</div>');

var element = $('<div><html-and-css log>ignore</html-and-css><div>');
$compile(element)(injector, element);

backend.flush();
microLeap();

expect(renderedText(element)).toEqual('.hello{}.world{}Simple!');
expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(
'<style>.hello{}.world{}</style><div log="SIMPLE">Simple!</div>'
);
$rootScope.$digest();
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; SIMPLE');
})));
});
});

0 comments on commit 6c6151c

Please sign in to comment.