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

Commit

Permalink
feat(directives): $cssUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer committed Jun 20, 2013
1 parent 472b6c6 commit ea1f7a2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
1 change: 1 addition & 0 deletions demo/web/book.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h2 { color: red }
1 change: 1 addition & 0 deletions demo/web/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class BookComponent {
BookComponent(BookController this.controller);

static String $templateUrl = 'book.html';
static String $cssUrl = 'book.css';

attach(Scope scope) {
controller.attach(scope);
Expand Down
12 changes: 7 additions & 5 deletions lib/block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -344,21 +344,23 @@ class ComponentWrapper {
Parser parser;
Block shadowBlock;


ComponentWrapper(this.directiveRef, this.controller, this.elementRoot, this.parser, $compiler, $http) {
var directive = directiveRef.directive;
var shadowRoot = elementRoot.createShadowRoot();

var styleData = '';
if (directive.$cssUrl != null) {
styleData = '<style>@import "${directive.$cssUrl}"</style>';
}

_appendAndCompileTemplate(data) {
shadowRoot.innerHtml = data;
shadowRoot.innerHtml = styleData + data;
shadowBlock = $compiler(shadowRoot.nodes)(shadowRoot.nodes);
}
// There is support here for directives having both $template and
// $templateUrl. This could be a clever way to add a 'LOADING'
// message.
if (directive.$template != null) {
_appendAndCompileTemplate(directive.$template);
}
_appendAndCompileTemplate(directive.$template != null ? directive.$template : '');

if (directive.$templateUrl != null) {
$http.getString(directive.$templateUrl).then((data) {
Expand Down
2 changes: 2 additions & 0 deletions lib/directive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Directive {
Type $controllerType;
String $template;
String $templateUrl;
String $cssUrl;
Map<String, String> $map;

bool isComponent = false;
Expand Down Expand Up @@ -43,6 +44,7 @@ class Directive {
$transclude = reflectStaticField(type, '\$transclude');
$template = reflectStaticField(type, '\$template');
$templateUrl = reflectStaticField(type, '\$templateUrl');
$cssUrl = reflectStaticField(type, '\$cssUrl');
$priority = reflectStaticField(type, '\$priority');
$map = reflectStaticField(type, '\$map');
if ($priority == null) {
Expand Down
3 changes: 2 additions & 1 deletion test/_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class MockHttp extends Http {
gets[url] = content;
}

flush() => Future.wait(futures);
flush() => gets.length == 0 ? Future.wait(futures) :
throw "Expected GETs not called $gets";

Future<String> getString(String url, {bool withCredentials, void onProgress(ProgressEvent e)}) {
if (!gets.containsKey(url)) throw "Unexpected URL $url";
Expand Down
7 changes: 7 additions & 0 deletions test/_http_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ main() {
http.getString('unknown');
}).toThrow('Unexpected URL unknown');
});

it('should barf on hanging requests', () {
http.expectGET('request', 'response');
expect(() {
http.flush();
}).toThrow('Expected GETs not called {request: response}');
});
});
}
66 changes: 64 additions & 2 deletions test/templateurl_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,37 @@ class SimpleUrlComponent {
attach(Scope scope) {}
}

class HtmlAndCssComponent {
static String $templateUrl = 'simple.html';
static String $cssUrl = 'simple.css';
attach(Scope scope) {}
}

class InlineWithCssComponent {
static String $template = '<div>inline!</div>';
static String $cssUrl = 'simple.css';
attach(Scope scope) {}
}

class OnlyCssComponent {
static String $cssUrl = 'simple.css';
attach(Scope scope) {}
}

main() {
describe('templateUrl', () {
describe('async template loading', () {
beforeEach(module((module) {
var mockHttp = new MockHttp();
module.value(MockHttp, mockHttp);
module.value(Http, mockHttp);
module.directive(LogAttrDirective);
module.directive(SimpleUrlComponent);
module.directive(HtmlAndCssComponent);
module.directive(OnlyCssComponent);
module.directive(InlineWithCssComponent);
}));

it('should replace element with template from url', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log) {
it('should replace element with template from url', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log) {
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>');

var element = $('<div><simple-url log>ignore</replace><div>');
Expand All @@ -37,7 +57,49 @@ main() {
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; SIMPLE');
}));
}));

it('should load a CSS file into a style', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log) {
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>');

var element = $('<div><html-and-css log>ignore</html-and-css><div>');
$compile(element)(element)..attach($rootScope);

$http.flush().then(expectAsync1((data) {
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(
'<style>@import "simple.css"</style><div log="SIMPLE">Simple!</div>'
);
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; SIMPLE');
}));
}));

it('should load a CSS file with a \$template', inject((Compiler $compile, Scope $rootScope) {
var element = $('<div><inline-with-css log>ignore</inline-with-css><div>');
$compile(element)(element)..attach($rootScope);
expect(renderedText(element)).toEqual('@import "simple.css"inline!');
}));

it('should load a CSS with no template', inject((Compiler $compile, Scope $rootScope) {
var element = $('<div><only-css log>ignore</only-css><div>');
$compile(element)(element)..attach($rootScope);

expect(renderedText(element)).toEqual('@import "simple.css"');
}));

it('should load the CSS before the template is loaded', inject((MockHttp $http, Compiler $compile, Scope $rootScope) {
$http.expectGET('simple.html', '<div>Simple!</div>');

var element = $('<html-and-css>ignore</html-and-css>');
$compile(element)(element)..attach($rootScope);

// The HTML is not loaded yet, but the CSS @import should be in the DOM.
expect(renderedText(element)).toEqual('@import "simple.css"');

$http.flush().then(expectAsync1((data) {
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
}));
}));
});
}
Expand Down

0 comments on commit ea1f7a2

Please sign in to comment.