forked from googlearchive/polymer-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinspect.html
235 lines (208 loc) · 6.19 KB
/
sinspect.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<script>
(function(scope) {
scope = scope || (window.Inspector = {});
var inspector;
window.sinspect = function(inNode, inProxy) {
if (!inspector) {
inspector = window.open('', 'ShadowDOM Inspector', null, true);
inspector.document.write(inspectorHTML);
//inspector.document.close();
inspector.api = {
shadowize: shadowize
};
}
inspect(inNode || wrap(document.body), inProxy);
};
var inspectorHTML = [
'<!DOCTYPE html>',
'<html>',
' <head>',
' <title>ShadowDOM Inspector</title>',
' <style>',
' body {',
' }',
' pre {',
' font: 9pt "Courier New", monospace;',
' line-height: 1.5em;',
' }',
' tag {',
' color: purple;',
' }',
' ul {',
' margin: 0;',
' padding: 0;',
' list-style: none;',
' }',
' li {',
' display: inline-block;',
' background-color: #f1f1f1;',
' padding: 4px 6px;',
' border-radius: 4px;',
' margin-right: 4px;',
' }',
' button {',
' display: inline-block;',
' color: purple;',
' font-weight: bold;',
' background: none;',
' border: none;',
' outline: none;',
' padding: 0;',
' margin: 0;',
' }',
' </style>',
' </head>',
' <body>',
' <ul id="crumbs">',
' </ul>',
' <div id="tree"></div>',
' </body>',
'</html>'
].join('\n');
var crumbs = [];
var displayCrumbs = function() {
// alias our document
var d = inspector.document;
// get crumbbar
var cb = d.querySelector('#crumbs');
// clear crumbs
cb.textContent = '';
// build new crumbs
for (var i=0, c; c=crumbs[i]; i++) {
var a = d.createElement('a');
a.href = '#';
a.textContent = c.localName;
a.idx = i;
a.onclick = function(event) {
var c;
while (crumbs.length > this.idx) {
c = crumbs.pop();
}
inspect(c.shadow || c, c);
event.preventDefault();
};
cb.appendChild(d.createElement('li')).appendChild(a);
}
};
var inspect = function(inNode, inProxy) {
// alias our document
var d = inspector.document;
// reset list of drillable nodes
drillable = [];
// memoize our crumb proxy
var proxy = inProxy || inNode;
crumbs.push(proxy);
// update crumbs
displayCrumbs();
// reflect local tree
d.body.querySelector('#tree').innerHTML =
'<pre>' + output(inNode, getLocalNodes(inNode)) + '</pre>';
};
var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach);
var blacklisted = {STYLE:1, SCRIPT:1, "#comment": 1, TEMPLATE: 1};
var blacklist = function(inNode) {
return blacklisted[inNode.nodeName];
};
var output = function(inNode, inChildNodes, inIndent) {
if (blacklist(inNode)) {
return '';
}
var indent = inIndent || '';
if (inNode.localName || inNode.domRoot) {
var name = inNode.localName || ROOT_NAME;
//inChildNodes = ShadowDOM.localNodes(inNode);
var info = indent + describe(inNode);
// if only textNodes
// TODO(sjmiles): make correct for ShadowDOM
/*if (!inNode.children.length && inNode.localName !== 'content' && inNode.localName !== 'shadow') {
info += catTextContent(inChildNodes);
} else*/ {
// TODO(sjmiles): native <shadow> has no reference to its projection
if (name == 'content' /*|| name == 'shadow'*/) {
inChildNodes = getDistributedNodes(inNode);
}
info += '<br/>';
var ind = indent + ' ';
//console.group('output ' + inNode.localName);
//console.log(inChildNodes);
forEach(inChildNodes, function(n) {
info += output(n, getLightNodes(n), ind);
});
//console.groupEnd('output ' + inNode.localName);
info += indent;
}
if (!({br:1}[name])) {
info += '<tag></' + name + '></tag>';
info += '<br/>';
}
} else {
var text = inNode.textContent.trim();
info = text ? indent + '"' + text + '"' + '<br/>' : '';
}
return info;
};
var catTextContent = function(inChildNodes) {
var info = '';
forEach(inChildNodes, function(n) {
info += n.textContent.trim();
});
return info;
};
var drillable = [];
var describe = function(inNode) {
var tag = '<tag>' + '<';
var name = inNode.localName || ROOT_NAME;
if (hasRoot(inNode)) {
tag += '<button idx="' + drillable.length +
'" onclick="api.shadowize.call(this)">' + name + '</button>';
drillable.push(inNode);
} else {
tag += name || ROOT_NAME;
}
if (inNode.attributes) {
forEach(inNode.attributes, function(a) {
tag += ' ' + a.name + (a.value ? '="' + a.value + '"' : '');
});
}
tag += '>'+ '</tag>';
return tag;
};
// remote api
shadowize = function() {
var idx = Number(this.attributes.idx.value);
//alert(idx);
var node = drillable[idx];
if (node) {
inspect(node, node)
} else {
console.log("bad shadowize node");
console.dir(this);
}
};
// util
var ROOT_NAME = 'local-root';
function hasRoot(node) {
return (node.shadyRoot || node.shadowRoot);
}
function getLocalNodes(n) {
return Polymer.dom.childNodes(n.root);
}
function getLightNodes(n) {
return Polymer.dom.childNodes(n);
}
function getDistributedNodes(node) {
Polymer.dom.distributedNodes(node);
}
// export
scope.output = output;
})(window.Inspector);
</script>