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

Remove usage of (deprecated) probe #1247

Closed
wants to merge 3 commits 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
44 changes: 29 additions & 15 deletions lib/introspection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,38 @@ List<ElementProbe> _findAllProbesInTree(dom.Node node) {
*
* The node parameter could be:
* * a [dom.Node],
* * a CSS selector for this node.
* * a CSS selector to look for a matching node inside the [root] or the dom.document.
*
* Specifying a [root] element allows querying a node that is not attached to the DOM. It is an
* error to pass a [root] element that is already attached to the DOM.
*
* **NOTE:** This global method is here to make it easier to debug Angular
* application from the browser's REPL, unit or end-to-end tests. The
* function is not intended to be called from Angular application.
*/
ElementProbe ngProbe(nodeOrSelector) {
if (nodeOrSelector == null) throw "ngProbe called without node";
ElementProbe ngProbe(nodeOrSelector, [dom.Node root]) {
if (nodeOrSelector == null) throw "ngProbe called without node/selector";
var node = nodeOrSelector;
if (nodeOrSelector is String) {
var nodes = ngQuery(dom.document, nodeOrSelector);
node = (nodes.isNotEmpty) ? nodes.first : null;
if (root == null) {
root = dom.document;
} else {
var attached = false;
for (var parent = root.parentNode; parent != null; parent = parent.parentNode) {
if (parent == dom.document) {
attached = true;
break;
}
}
if (attached) throw "The root element must not be attached to the DOM";
root = new dom.DivElement()..append(root);
}
var nodes = ngQuery(root, nodeOrSelector);
if (nodes.isEmpty) throw "The '$nodeOrSelector' selector does not match any node";
node = nodes.first;
}
var probe = _findProbeWalkingUp(node);
if (probe != null) {
return probe;
}
if (probe != null) return probe;
var forWhat = (nodeOrSelector is String) ? "selector" : "node";
throw "Could not find a probe for the $forWhat '$nodeOrSelector' nor its parents";
}
Expand All @@ -93,7 +108,8 @@ ElementProbe ngProbe(nodeOrSelector) {
* application from the browser's REPL, unit or end-to-end tests. The function
* is not intended to be called from Angular application.
*/
DirectiveInjector ngInjector(nodeOrSelector) => ngProbe(nodeOrSelector).injector;
DirectiveInjector ngInjector(nodeOrSelector, [dom.Node root]) =>
ngProbe(nodeOrSelector, root).injector;


/**
Expand All @@ -103,17 +119,16 @@ DirectiveInjector ngInjector(nodeOrSelector) => ngProbe(nodeOrSelector).injector
* application from the browser's REPL, unit or end-to-end tests. The function
* is not intended to be called from Angular application.
*/
Scope ngScope(nodeOrSelector) => ngProbe(nodeOrSelector).scope;
Scope ngScope(nodeOrSelector, [dom.Node root]) => ngProbe(nodeOrSelector, root).scope;


List<dom.Element> ngQuery(dom.Node element, String selector,
[String containsText]) {
List<dom.Element> ngQuery(dom.Node element, String selector, [String containsText]) {
var list = [];
var children = [element];
if ((element is dom.Element) && element.shadowRoot != null) {
children.add(element.shadowRoot);
}
while (!children.isEmpty) {
while (children.isNotEmpty) {
var child = children.removeAt(0);
child.querySelectorAll(selector).forEach((e) {
if (containsText == null || e.text.contains(containsText)) list.add(e);
Expand All @@ -136,7 +151,6 @@ List<dom.Element> ngQuery(dom.Node element, String selector,
List<Object> ngDirectives(nodeOrSelector) => ngProbe(nodeOrSelector).directives;



js.JsObject _jsProbe(ElementProbe probe) {
return _jsify({
"element": probe.element,
Expand Down Expand Up @@ -217,7 +231,7 @@ _jsify(var obj) {
return _jsFunction(obj);
}
if ((obj is Map) || (obj is Iterable)) {
var mappedObj = (obj is Map) ?
var mappedObj = (obj is Map) ?
new Map.fromIterables(obj.keys, obj.values.map(_jsify)) : obj.map(_jsify);
if (obj is List) {
return new js.JsArray.from(mappedObj);
Expand Down
4 changes: 2 additions & 2 deletions lib/mock/test_bed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TestBed {
* - [List<Node>] then treat it as a collection of nods
*
* After the compilation the [rootElements] contains an array of compiled root nodes,
* and [rootElement] contains the first element from the [rootElemets].
* and [rootElement] contains the first element from the [rootElements].
*
* An option [scope] parameter can be supplied to link it with non root scope.
*/
Expand All @@ -46,7 +46,7 @@ class TestBed {
} else {
throw 'Expecting: String, Node, or List<Node> got $html.';
}
rootElement = rootElements.length > 0 && rootElements[0] is Element ? rootElements[0] : null;
rootElement = rootElements.isNotEmpty && rootElements[0] is Element ? rootElements[0] : null;
if (directives == null) {
directives = injector.getByKey(DIRECTIVE_MAP_KEY);
}
Expand Down
20 changes: 13 additions & 7 deletions test/core_dom/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import '../_specs.dart';
import 'package:angular/core_dom/directive_injector.dart';


forBothCompilers(fn) {
var probeEnabled;

forCompilerSetups(fn) {
describe('walking compiler', () {
probeEnabled = true;
beforeEachModule((Module m) {
m.bind(Compiler, toImplementation: WalkingCompiler);
return m;
Expand All @@ -14,6 +17,7 @@ forBothCompilers(fn) {
});

describe('tagging compiler', () {
probeEnabled = true;
beforeEachModule((Module m) {
m.bind(Compiler, toImplementation: TaggingCompiler);
return m;
Expand All @@ -22,6 +26,7 @@ forBothCompilers(fn) {
});

describe('tagging compiler with ElementProbe disabled', () {
probeEnabled = false;
beforeEachModule((Module m) {
m.bind(Compiler, toImplementation: TaggingCompiler);
m.bind(CompilerConfig, toValue: new CompilerConfig.withOptions(elementProbeEnabled: false));
Expand All @@ -32,7 +37,9 @@ forBothCompilers(fn) {
}

forAllCompilersAndComponentFactories(fn) {
forBothCompilers(fn);
describe('shadow dom components', () {
forCompilerSetups(fn);
});

describe('transcluding components', () {
beforeEachModule((Module m) {
Expand All @@ -46,7 +53,7 @@ forAllCompilersAndComponentFactories(fn) {
}

void main() {
forBothCompilers((compilerType) =>
forCompilerSetups((compilerType) =>
describe('TranscludingComponentFactory', () {
TestBed _;

Expand Down Expand Up @@ -370,7 +377,7 @@ void main() {
}));

it('should store ElementProbe with Elements', async(() {
if (compilerType == 'tagging-no-elementProbe') return;
if (!probeEnabled) return;

_.compile('<div><simple>innerText</simple></div>');
microLeap();
Expand Down Expand Up @@ -632,9 +639,8 @@ void main() {
});

it('should expose PublishModuleDirectiveSuperType as PublishModuleDirectiveSuperType', () {
_.compile(r'<div publish-types probe="publishModuleProbe"></div>');
Probe probe = _.rootScope.context['publishModuleProbe'];
var directive = probe.injector.get(PublishModuleDirectiveSuperType);
_.compile(r'<div publish-types></div>');
var directive = PublishModuleAttrDirective._injector.get(PublishModuleDirectiveSuperType);
expect(directive is PublishModuleAttrDirective).toBeTruthy();
});

Expand Down
5 changes: 2 additions & 3 deletions test/core_dom/selector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ library angular.dom.selector_spec;

import '../_specs.dart';

const _aBElement = const Decorator(selector:'b');
const _aBElement = const Decorator(selector:'b');
const _aBClass = const Decorator(selector:'.b');
const _aDirectiveAttr = const Decorator(selector:'[directive]');
const _aWildcardDirectiveAttr = const Decorator(selector:'[wildcard-*]');
Expand Down Expand Up @@ -181,10 +181,9 @@ main() {

it('should match ng-model + required on the same element', () {
expect(
selector(element = e('<input type="text" ng-model="val" probe="i" required="true" />')),
selector(element = e('<input type="text" ng-model="val" required="true" />')),
toEqualsDirectiveInfos([
{ "selector": '[ng-model]', "value": 'val', "element": element},
{ "selector": '[probe]', "value": 'i', "element": element},
{ "selector": '[ng-model][required]', "value": 'true', "element": element},
{ "selector": 'input[type=text][ng-model]', "value": 'val', "element": element}
]));
Expand Down
10 changes: 6 additions & 4 deletions test/core_dom/view_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ main() {

Compiler compiler = rootInjector.get(Compiler);
DirectiveMap directives = rootInjector.get(DirectiveMap);
compiler(es('<dir-a>{{\'a\' | formatterA}}</dir-a><dir-b></dir-b>'), directives)(rootScope, rootInjector.get(DirectiveInjector));
var els = es('<dir-a>{{\'a\' | formatterA}}</dir-a><dir-b></dir-b>');
compiler(els, directives)(rootScope, rootInjector.get(DirectiveInjector));
rootScope.apply();

expect(log.log, equals(['AFormatter', 'ADirective']));
Expand All @@ -232,11 +233,12 @@ main() {

DirectiveMap newDirectives = childInjector.get(DirectiveMap);
var scope = childInjector.get(Scope);
compiler(es('<dir-a probe="dirA"></dir-a>{{\'a\' | formatterA}}'
'<dir-b probe="dirB"></dir-b>{{\'b\' | formatterB}}'), newDirectives)(scope, childInjector.get(DirectiveInjector));
els = es('<dir-a></dir-a>{{\'a\' | formatterA}}<dir-b></dir-b>{{\'b\' | formatterB}}');
compiler(els, newDirectives)(scope, childInjector.get(DirectiveInjector));
rootScope.apply();

expect(log.log, equals(['AFormatter', 'ADirective', 'BFormatter', 'ADirective', 'BDirective']));
expect(log.log)
.toEqual(['AFormatter', 'ADirective', 'BFormatter', 'ADirective', 'BDirective']);
});

});
Expand Down
Loading