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

Commit 236b690

Browse files
committed
feat(directives): Do not crash on missing map parameters.
1 parent bad34f0 commit 236b690

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/block.dart

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ class ComponentFactory {
314314
createAttributeMapping(Scope parentScope, Scope shadowScope, Parser parser) {
315315
directive.$map.forEach((attrName, mapping) {
316316
var attrValue = element.attributes[attrName];
317+
if (attrValue == null) attrValue = '';
317318
if (mapping == '@') {
318319
shadowScope[attrName] = attrValue;
319320
} else if (mapping == '=') {

test/compiler_spec.dart

+39
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ main() {
392392
beforeEach(module((AngularModule module) {
393393
module.directive(SimpleComponent);
394394
module.directive(IoComponent);
395+
module.directive(ParentExpressionComponent);
395396
module.directive(PublishMeComponent);
396397
}));
397398

@@ -408,6 +409,35 @@ main() {
408409
}));
409410
}));
410411

412+
it('should create a component that can access parent scope', inject(() {
413+
$rootScope.fromParent = "should not be used";
414+
$rootScope.val = "poof";
415+
var element = $('<parent-expression fromParent=val></parent-expression>');
416+
417+
$compile(element)(injector, element);
418+
419+
ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
420+
expect(renderedText(element)).toEqual('inside poof');
421+
}));
422+
}));
423+
424+
it('should behave nicely if a mapped attribute is missing', inject(() {
425+
var element = $('<parent-expression></parent-expression>');
426+
$compile(element)(injector, element);
427+
ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
428+
expect(renderedText(element)).toEqual('inside ');
429+
}));
430+
}));
431+
432+
it('should behave nicely if a mapped attribute evals to null', inject(() {
433+
$rootScope.val = null;
434+
var element = $('<parent-expression fromParent=val></parent-expression>');
435+
$compile(element)(injector, element);
436+
ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
437+
expect(renderedText(element)).toEqual('inside ');
438+
}));
439+
}));
440+
411441
it('should create a component with IO', inject(() {
412442
var element = $(r'<div><io attr="A" expr="name" ondone="done=true"></io></div>');
413443
$compile(element)(injector, element);
@@ -480,6 +510,15 @@ class IoComponent {
480510
}
481511
}
482512

513+
class ParentExpressionComponent {
514+
static String $template = '<div>inside {{fromParent()}}</div>';
515+
static Map $map = {"fromParent": "&"};
516+
static TemplateLoader lastTemplateLoader;
517+
ParentExpressionComponent(Scope shadowScope, TemplateLoader templateLoader) {
518+
lastTemplateLoader = templateLoader;
519+
}
520+
}
521+
483522
class PublishMeComponent {
484523
static String $template = r'<content>{{ctrlName.value}}</content>';
485524
static String $publishAs = 'ctrlName';

0 commit comments

Comments
 (0)