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

Commit b307c82

Browse files
committed
feat(block): Chain ElementProbe parents; add to shadowRoot
This allows one to retrieve ElementProbe from any element including a shadowRoot. The ElementProbe contains element, injector, and scope. The added benefit is that by waling the ElementProbe parent one can get the parent element of a shadow DOM. Closes #625
1 parent 5b21520 commit b307c82

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

lib/core_dom/block_factory.dart

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ class BlockFactory {
158158
TemplateCache templateCache = injector.get(TemplateCache);
159159
DirectiveMap directives = injector.get(DirectiveMap);
160160
// This is a bit of a hack since we are returning different type then we are.
161-
var componentFactory = new _ComponentFactory(node, ref.type, ref.annotation as NgComponent, injector.get(dom.NodeTreeSanitizer));
161+
var componentFactory = new _ComponentFactory(node, ref.type,
162+
ref.annotation as NgComponent,
163+
injector.get(dom.NodeTreeSanitizer), _expando);
162164
if (fctrs == null) fctrs = new Map<Type, _ComponentFactory>();
163165
fctrs[ref.type] = componentFactory;
164166
return componentFactory.call(injector, compiler, scope, blockCache, http, templateCache, directives);
@@ -177,11 +179,14 @@ class BlockFactory {
177179
boundBlockFactory = (Injector injector) => ref.blockFactory.bind(injector);
178180
}
179181
});
180-
nodeModule.factory(BlockHole, blockHoleFactory);
181-
nodeModule.factory(BlockFactory, blockFactory);
182-
nodeModule.factory(BoundBlockFactory, boundBlockFactory);
182+
nodeModule
183+
..factory(BlockHole, blockHoleFactory)
184+
..factory(BlockFactory, blockFactory)
185+
..factory(BoundBlockFactory, boundBlockFactory)
186+
..factory(ElementProbe, (_) => probe);
183187
nodeInjector = parentInjector.createChild([nodeModule]);
184-
probe = _expando[node] = new ElementProbe(node, nodeInjector, scope);
188+
probe = _expando[node] = new ElementProbe(
189+
parentInjector.get(ElementProbe), node, nodeInjector, scope);
185190
} finally {
186191
assert(_perf.stopTimer(timerId) != false);
187192
}
@@ -314,14 +319,16 @@ class _ComponentFactory {
314319
final Type type;
315320
final NgComponent component;
316321
final dom.NodeTreeSanitizer treeSanitizer;
322+
final Expando _expando;
317323

318324
dom.ShadowRoot shadowDom;
319325
Scope shadowScope;
320326
Injector shadowInjector;
321327
Compiler compiler;
322328
var controller;
323329

324-
_ComponentFactory(this.element, this.type, this.component, this.treeSanitizer);
330+
_ComponentFactory(this.element, this.type, this.component, this.treeSanitizer,
331+
this._expando);
325332

326333
dynamic call(Injector injector, Compiler compiler, Scope scope, BlockCache $blockCache, Http $http, TemplateCache $templateCache, DirectiveMap directives) {
327334
this.compiler = compiler;
@@ -380,12 +387,16 @@ class _ComponentFactory {
380387
}
381388

382389
createShadowInjector(injector, TemplateLoader templateLoader) {
390+
var probe;
383391
var shadowModule = new Module()
384392
..type(type)
385393
..value(Scope, shadowScope)
386394
..value(TemplateLoader, templateLoader)
387-
..value(dom.ShadowRoot, shadowDom);
395+
..value(dom.ShadowRoot, shadowDom)
396+
..factory(ElementProbe, (_) => probe);
388397
shadowInjector = injector.createChild([shadowModule], name: _SHADOW);
398+
probe = _expando[shadowDom] = new ElementProbe(
399+
injector.get(ElementProbe), shadowDom, shadowInjector, shadowScope);
389400
return shadowInjector;
390401
}
391402
}
@@ -429,10 +440,11 @@ String _html(obj) {
429440
* SEE: [ngInjector], [ngScope], [ngDirectives]
430441
*/
431442
class ElementProbe {
443+
final ElementProbe parent;
432444
final dom.Node element;
433445
final Injector injector;
434446
final Scope scope;
435447
final directives = [];
436448

437-
ElementProbe(this.element, this.injector, this.scope);
449+
ElementProbe(this.parent, this.element, this.injector, this.scope);
438450
}

lib/core_dom/module.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ part 'tree_sanitizer.dart';
2828
class NgCoreDomModule extends Module {
2929
NgCoreDomModule() {
3030
value(dom.Window, dom.window);
31+
value(ElementProbe, null);
3132

3233
factory(TemplateCache, (_) => new TemplateCache(capacity: 0));
3334
type(dom.NodeTreeSanitizer, implementedBy: NullTreeSanitizer);

test/core_dom/compiler_spec.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ main() => describe('dte.compiler', () {
196196
expect(element.textWithShadow()).toEqual('INNER()');
197197
})));
198198

199+
it('should store ElementProbe with Elements', async(inject((TestBed tb) {
200+
tb.compile('<div><simple>innerText</simple></div>');
201+
microLeap();
202+
var simpleElement = tb.rootElement.querySelector('simple');
203+
expect(simpleElement.text).toEqual('innerText');
204+
var simpleProbe = ngProbe(simpleElement);
205+
var simpleComponent = simpleProbe.injector.get(SimpleComponent);
206+
expect(simpleComponent.scope.context['name']).toEqual('INNER');
207+
var shadowRoot = simpleElement.shadowRoot;
208+
var shadowProbe = ngProbe(shadowRoot);
209+
expect(shadowProbe).toBeNotNull();
210+
expect(shadowProbe.element).toEqual(shadowRoot);
211+
expect(shadowProbe.parent.element).toEqual(simpleElement);
212+
})));
213+
199214
it('should create a simple component', async(inject((NgZone zone) {
200215
rootScope.context['name'] = 'OUTTER';
201216
var element = $(r'<div>{{name}}:<simple>{{name}}</simple></div>');
@@ -674,7 +689,8 @@ class PublishTypesAttrDirective implements PublishTypesDirectiveSuperType {
674689
template: r'{{name}}(<content>SHADOW-CONTENT</content>)'
675690
)
676691
class SimpleComponent {
677-
SimpleComponent(Scope scope) {
692+
Scope scope;
693+
SimpleComponent(Scope this.scope) {
678694
scope.context['name'] = 'INNER';
679695
}
680696
}

0 commit comments

Comments
 (0)