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

feat(directives): $cssUrl #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}');
});
});
}
52 changes: 50 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,35 @@ 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"');
}));
});
}
Expand Down