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

Commit ead15c2

Browse files
caitpbtford
authored andcommittedJul 16, 2015
feat(ElementPanel): display scope properties for children of DocumentFragments
Sort of related to angular/angular.js#6637 --- Currently, jqLite#inheritedData() will not find data if it needs to cross through document fragment barriers, such as the Shadow DOM barrier. This patch allows batarang to figure this out. In addition, it provides some tests for the angularjs properties sidebar of the element panel, which were missing previously. Closes #104
1 parent a471f76 commit ead15c2

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed
 

‎devtoolsBackground.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
var panels = chrome.devtools.panels;
1+
var panels = chrome && chrome.devtools && chrome.devtools.panels;
2+
3+
function getScope(node) {
4+
var scope = window.angular.element(node).scope();
5+
if (!scope) {
6+
// Might be a child of a DocumentFragment...
7+
while (node && node.nodeType === 1) node = node.parentNode;
8+
if (node && node.nodeType === 11) node = (node.parentNode || node.host);
9+
return getScope(node);
10+
}
11+
return scope;
12+
}
213

314
// The function below is executed in the context of the inspected page.
415

516
var getPanelContents = function () {
617
if (window.angular && $0) {
718
//TODO: can we move this scope export into updateElementProperties
8-
var scope = window.angular.element($0).scope();
19+
var scope = getScope($0);
20+
921
// Export $scope to the console
1022
window.$scope = scope;
1123
return (function (scope) {
@@ -29,16 +41,18 @@ var getPanelContents = function () {
2941
}
3042
};
3143

32-
panels.elements.createSidebarPane(
44+
panels && panels.elements.createSidebarPane(
3345
"$scope",
3446
function (sidebar) {
3547
panels.elements.onSelectionChanged.addListener(function updateElementProperties() {
3648
sidebar.setExpression("(" + getPanelContents.toString() + ")()");
3749
});
38-
});
3950

40-
var angularPanel = panels.create(
41-
"AngularJS",
42-
"img/angular.png",
43-
"panel/app.html"
44-
);
51+
// Angular panel
52+
var angularPanel = panels.create(
53+
"AngularJS",
54+
"img/angular.png",
55+
"panel.html"
56+
);
57+
});
58+

‎karma.conf.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ module.exports = function(config) {
1313
'node_modules/angular-mocks/angular-mocks.js',
1414
'panel/app.js',
1515
'panel/**/*.js',
16-
'panel/**/*.spec.js'
16+
'panel/**/*.spec.js',
17+
'devtoolsBackground.js',
18+
'test/*.spec.js'
1719
],
1820
exclude: [],
1921
preprocessors: {

‎test/element-panel.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
describe('elements panel', function() {
2+
3+
afterEach(function() {
4+
$0 = null;
5+
});
6+
7+
8+
describe('angular properties sidebar', function() {
9+
describe('getPanelContents()', function() {
10+
it('should return properties for scope of selected element', inject(function($rootScope) {
11+
var element = angular.element('<div><p>Hello, world</p></div>');
12+
element.data('$scope', $rootScope);
13+
$rootScope.text = "Hello, world!";
14+
$0 = element[0];
15+
expect (getPanelContents().text).toBe("Hello, world!");
16+
$0 = element.children().eq(0)[0];
17+
expect (getPanelContents().text).toBe("Hello, world!");
18+
}));
19+
20+
21+
it('should cross shadow DOM barrier via DocumentFragment#host', inject(function($rootScope) {
22+
var parent = document.createElement('div'),
23+
fragment = document.createDocumentFragment(),
24+
child = document.createElement('p');
25+
fragment.host = parent;
26+
fragment.appendChild(child);
27+
parent.appendChild(fragment);
28+
29+
parent = angular.element(parent);
30+
parent.data('$scope', $rootScope);
31+
$rootScope.text = "Fragmented fun for everyone";
32+
33+
$0 = child;
34+
expect(getPanelContents().text).toBe("Fragmented fun for everyone");
35+
}));
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)
This repository has been archived.