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

Commit 0873ba8

Browse files
committed
Added support for resetStyleInheritance and applyAuthorStyles on shadow root
http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/#toc- style-inheriting
1 parent 8cb01fc commit 0873ba8

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

lib/block.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ class ComponentWrapper {
407407
ComponentWrapper(this.directiveRef, this.controller, this.elementRoot, this.parser, $compiler, $http) {
408408
var directive = directiveRef.directive;
409409
var shadowRoot = elementRoot.createShadowRoot();
410+
shadowRoot.applyAuthorStyles =
411+
directive.$shadowRootOptions.$applyAuthorStyles;
412+
shadowRoot.resetStyleInheritance =
413+
directive.$shadowRootOptions.$resetStyleInheritance;
410414

411415
var styleData = '';
412416
if (directive.$cssUrl != null) {

lib/directive.dart

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Directive {
1616
String $cssUrl;
1717
Map<String, String> $map;
1818
String $visibility;
19+
ShadowRootOptions $shadowRootOptions;
1920

2021
bool isComponent = false;
2122
bool isStructural = false;
@@ -46,15 +47,10 @@ class Directive {
4647
$template = reflectStaticField(type, '\$template');
4748
$templateUrl = reflectStaticField(type, '\$templateUrl');
4849
$cssUrl = reflectStaticField(type, '\$cssUrl');
49-
$priority = reflectStaticField(type, '\$priority');
50-
$visibility = reflectStaticField(type, '\$visibility');
51-
if ($visibility == null) {
52-
$visibility = DirectiveVisibility.LOCAL;
53-
}
50+
$priority = _defaultIfNull(reflectStaticField(type, '\$priority'), 0);
51+
$visibility = _defaultIfNull(
52+
reflectStaticField(type, '\$visibility'), DirectiveVisibility.LOCAL);
5453
$map = reflectStaticField(type, '\$map');
55-
if ($priority == null) {
56-
$priority = 0;
57-
}
5854
isStructural = $transclude != null;
5955
var $selector = reflectStaticField(type, r'$selector');
6056
if ($selector != null) {
@@ -63,9 +59,17 @@ class Directive {
6359
if (isComponent && $map == null) {
6460
$map = new Map<String, String>();
6561
}
62+
if (isComponent) {
63+
$shadowRootOptions = _defaultIfNull(
64+
reflectStaticField(type, '\$shadowRootOptions'),
65+
new ShadowRootOptions());
66+
}
6667
}
6768
}
6869

70+
dynamic _defaultIfNull(dynamic value, dynamic defaultValue) =>
71+
value == null ? defaultValue : value;
72+
6973
class DirectiveRef {
7074
dom.Node element;
7175
String selector;
@@ -119,6 +123,17 @@ abstract class DirectiveVisibility {
119123
static const String DIRECT_CHILDREN = 'direct_children';
120124
}
121125

126+
/**
127+
* See:
128+
* http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/#toc-style-inheriting
129+
*/
130+
class ShadowRootOptions {
131+
bool $applyAuthorStyles = false;
132+
bool $resetStyleInheritance = false;
133+
ShadowRootOptions([this.$applyAuthorStyles = false,
134+
this.$resetStyleInheritance = false]);
135+
}
136+
122137
class Controller {
123138

124139
}

test/directive_spec.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class ExplicitNullTranscludeDirective {
1212
static var $transclude = null;
1313
}
1414

15+
class WithDefaultShadowRootOptionsComponent {
16+
}
17+
18+
class WithCustomShadowRootOptionsComponent {
19+
static var $shadowRootOptions = new ShadowRootOptions(true, true);
20+
}
21+
1522
main() {
1623
describe('directive factory', () {
1724
it('should guess the directive name correctly', () {
@@ -38,5 +45,17 @@ main() {
3845
Directive factory = new Directive(ExplicitNullTranscludeDirective);
3946
expect(factory.$transclude).toEqual(null);
4047
});
48+
49+
it('should default \$shadowRootOptions to false/false', () {
50+
Directive factory = new Directive(WithDefaultShadowRootOptionsComponent);
51+
expect(factory.$shadowRootOptions.$applyAuthorStyles, isFalse);
52+
expect(factory.$shadowRootOptions.$resetStyleInheritance, isFalse);
53+
});
54+
55+
it('should override \$shadowRootOptions with values provided by component type', () {
56+
Directive factory = new Directive(WithCustomShadowRootOptionsComponent);
57+
expect(factory.$shadowRootOptions.$applyAuthorStyles, isTrue);
58+
expect(factory.$shadowRootOptions.$resetStyleInheritance, isTrue);
59+
});
4160
});
4261
}

0 commit comments

Comments
 (0)