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

Commit

Permalink
feat(ElementPanel): display scope properties for children of Document…
Browse files Browse the repository at this point in the history
…Fragments

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
  • Loading branch information
caitp authored and btford committed Jul 16, 2015
1 parent a471f76 commit ead15c2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
32 changes: 23 additions & 9 deletions devtoolsBackground.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
var panels = chrome.devtools.panels;
var panels = chrome && chrome.devtools && chrome.devtools.panels;

function getScope(node) {
var scope = window.angular.element(node).scope();
if (!scope) {
// Might be a child of a DocumentFragment...
while (node && node.nodeType === 1) node = node.parentNode;
if (node && node.nodeType === 11) node = (node.parentNode || node.host);
return getScope(node);
}
return scope;
}

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

var getPanelContents = function () {
if (window.angular && $0) {
//TODO: can we move this scope export into updateElementProperties
var scope = window.angular.element($0).scope();
var scope = getScope($0);

// Export $scope to the console
window.$scope = scope;
return (function (scope) {
Expand All @@ -29,16 +41,18 @@ var getPanelContents = function () {
}
};

panels.elements.createSidebarPane(
panels && panels.elements.createSidebarPane(
"$scope",
function (sidebar) {
panels.elements.onSelectionChanged.addListener(function updateElementProperties() {
sidebar.setExpression("(" + getPanelContents.toString() + ")()");
});
});

var angularPanel = panels.create(
"AngularJS",
"img/angular.png",
"panel/app.html"
);
// Angular panel
var angularPanel = panels.create(
"AngularJS",
"img/angular.png",
"panel.html"
);
});

4 changes: 3 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = function(config) {
'node_modules/angular-mocks/angular-mocks.js',
'panel/app.js',
'panel/**/*.js',
'panel/**/*.spec.js'
'panel/**/*.spec.js',
'devtoolsBackground.js',
'test/*.spec.js'
],
exclude: [],
preprocessors: {
Expand Down
38 changes: 38 additions & 0 deletions test/element-panel.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
describe('elements panel', function() {

afterEach(function() {
$0 = null;
});


describe('angular properties sidebar', function() {
describe('getPanelContents()', function() {
it('should return properties for scope of selected element', inject(function($rootScope) {
var element = angular.element('<div><p>Hello, world</p></div>');
element.data('$scope', $rootScope);
$rootScope.text = "Hello, world!";
$0 = element[0];
expect (getPanelContents().text).toBe("Hello, world!");
$0 = element.children().eq(0)[0];
expect (getPanelContents().text).toBe("Hello, world!");
}));


it('should cross shadow DOM barrier via DocumentFragment#host', inject(function($rootScope) {
var parent = document.createElement('div'),
fragment = document.createDocumentFragment(),
child = document.createElement('p');
fragment.host = parent;
fragment.appendChild(child);
parent.appendChild(fragment);

parent = angular.element(parent);
parent.data('$scope', $rootScope);
$rootScope.text = "Fragmented fun for everyone";

$0 = child;
expect(getPanelContents().text).toBe("Fragmented fun for everyone");
}));
});
});
});

0 comments on commit ead15c2

Please sign in to comment.