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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Javascript that is being injected into the inspectable page
* while debugging.
*/
goog.require('goog.json');
goog.provide('devtools.Injected');
/**
* Main injected object.
* @constructor.
*/
devtools.Injected = function() {
/**
* Unique style id generator.
* @type {number}
* @private
*/
this.lastStyleId_ = 1;
/**
* This array is not unused as it may seem. It stores references to the
* styles so that they could be found for future editing.
* @type {Array<CSSStyleDeclaration>}
* @private
*/
this.styles_ = [];
};
/**
* Returns array of properties for a given node on a given path.
* @param {Node} node Node to get property value for.
* @param {Array.<string>} path Path to the nested object.
* @param {number} protoDepth Depth of the actual proto to inspect.
* @return {Array.<Object>} Array where each property is represented
* by the tree entries [{string} type, {string} name, {Object} value].
*/
devtools.Injected.prototype.getProperties = function(node, path, protoDepth) {
var result = [];
var obj = node;
// Follow the path.
for (var i = 0; obj && i < path.length; ++i) {
obj = obj[path[i]];
}
if (!obj) {
return [];
}
// Get to the necessary proto layer.
for (var i = 0; obj && i < protoDepth; ++i) {
obj = obj.__proto__;
}
if (!obj) {
return [];
}
// Go over properties, prepare results.
for (var name in obj) {
if (protoDepth != -1 && 'hasOwnProperty' in obj &&
!obj.hasOwnProperty(name)) {
continue;
}
var type = typeof obj[name];
result.push(type);
result.push(name);
if (type == 'string') {
var str = obj[name];
result.push(str.length > 99 ? str.substr(0, 99) + '...' : str);
} else if (type != 'object' && type != 'array' &&
type != 'function') {
result.push(obj[name]);
} else {
result.push(undefined);
}
}
return result;
};
/**
* Returns array of prototypes for a given node.
* @param {Node} node Node to get prorotypes for.
* @return {Array<string>} Array of proto names.
*/
devtools.Injected.prototype.getPrototypes = function(node) {
var result = [];
for (var prototype = node; prototype; prototype = prototype.__proto__) {
var description = Object.prototype.toString.call(prototype);
result.push(description.replace(/^\[object (.*)\]$/i, '$1'));
}
return result;
};
/**
* Returns style information that is used in devtools.js.
* @param {Node} node Node to get prorotypes for.
* @param {boolean} authorOnly Determines whether only author styles need to
* be added.
* @return {string} Style collection descriptor.
*/
devtools.Injected.prototype.getStyles = function(node, authorOnly) {
if (!node.nodeType == Node.ELEMENT_NODE) {
return {};
}
var matchedRules = window.getMatchedCSSRules(node, '', authorOnly);
var matchedCSSRulesObj = [];
for (var i = 0; matchedRules && i < matchedRules.length; ++i) {
var rule = matchedRules[i];
var style = this.serializeStyle_(rule.style);
var ruleValue = {
'selector' : rule.selectorText,
'style' : style
};
if (rule.parentStyleSheet) {
ruleValue['parentStyleSheet'] = {
'href' : rule.parentStyleSheet.href,
'ownerNodeName' : rule.parentStyleSheet.ownerNode ?
rule.parentStyleSheet.ownerNode.name : null
};
}
var parentStyleSheetHref = (rule.parentStyleSheet ?
rule.parentStyleSheet.href : undefined);
var parentStyleSheetOwnerNodeName;
if (rule.parentStyleSheet && rule.parentStyleSheet.ownerNode) {
parentStyleSheetOwnerNodeName = rule.parentStyleSheet.ownerNode.name;
}
matchedCSSRulesObj.push(ruleValue);
}
var attributeStyles = {};
var attributes = node.attributes;
for (var i = 0; attributes && i < attributes.length; ++i) {
if (attributes[i].style) {
attributeStyles[attributes[i].name] =
this.serializeStyle_(attributes[i].style);
}
}
var result = {
'inlineStyle' : this.serializeStyle_(node.style),
'computedStyle' : this.serializeStyle_(
window.getComputedStyle(node, '')),
'matchedCSSRules' : matchedCSSRulesObj,
'styleAttributes' : attributeStyles
};
return result;
};
/**
* Returns style decoration object for given id.
* @param {Node} node Node to get prorotypes for.
* @param {number} id Style id.
* @return {Object} Style object.
* @private
*/
devtools.Injected.prototype.getStyleForId_ = function(node, id) {
var matchedRules = window.getMatchedCSSRules(node, '', false);
for (var i = 0; matchedRules && i < matchedRules.length; ++i) {
var rule = matchedRules[i];
if (rule.style.__id == id) {
return rule.style;
}
}
var attributes = node.attributes;
for (var i = 0; attributes && i < attributes.length; ++i) {
if (attributes[i].style && attributes[i].style.__id == id) {
return attributes[i].style;
}
}
if (node.style.__id == id) {
return node.style;
}
return null;
};
/**
* Converts given style into serializable object.
* @param {CSSStyleDeclaration} style Style to serialize.
* @return {Array<Object>} Serializable object.
* @private
*/
devtools.Injected.prototype.serializeStyle_ = function(style) {
if (!style) {
return [];
}
if (!style.__id) {
style.__id = this.lastStyleId_++;
this.styles_.push(style);
}
var result = [
style.__id,
style.__disabledProperties,
style.__disabledPropertyValues,
style.__disabledPropertyPriorities
];
for (var i = 0; i < style.length; ++i) {
var name = style[i];
result.push([
name,
style.getPropertyPriority(name),
style.isPropertyImplicit(name),
style.getPropertyShorthand(name),
style.getPropertyValue(name)
]);
}
return result;
};
/**
* Toggles style with given id on/off.
* @param {Node} node Node to get prorotypes for.
* @param {number} styleId Id of style to toggle.
* @param {boolean} enabled Determines value to toggle to,
* @param {string} name Name of the property.
*/
devtools.Injected.prototype.toggleNodeStyle = function(node, styleId, enabled,
name) {
var style = this.getStyleForId_(node, styleId);
if (!style) {
return false;
}
if (!enabled) {
if (!style.__disabledPropertyValues ||
!style.__disabledPropertyPriorities) {
style.__disabledProperties = {};
style.__disabledPropertyValues = {};
style.__disabledPropertyPriorities = {};
}
style.__disabledPropertyValues[name] = style.getPropertyValue(name);
style.__disabledPropertyPriorities[name] = style.getPropertyPriority(name);
if (style.getPropertyShorthand(name)) {
var longhandProperties = this.getLonghandProperties_(style, name);
for (var i = 0; i < longhandProperties.length; ++i) {
style.__disabledProperties[longhandProperties[i]] = true;
style.removeProperty(longhandProperties[i]);
}
} else {
style.__disabledProperties[name] = true;
style.removeProperty(name);
}
} else if (style.__disabledProperties &&
style.__disabledProperties[name]) {
var value = style.__disabledPropertyValues[name];
var priority = style.__disabledPropertyPriorities[name];
style.setProperty(name, value, priority);
delete style.__disabledProperties[name];
delete style.__disabledPropertyValues[name];
delete style.__disabledPropertyPriorities[name];
}
return true;
};
/**
* Returns longhand proeprties for a given shorthand one.
* @param {CSSStyleDeclaration} style Style declaration to use for lookup.
* @param {string} shorthandProperty Shorthand property to get longhands for.
* @return {Array.<string>} Array with longhand properties.
* @private
*/
devtools.Injected.prototype.getLonghandProperties_ = function(style,
shorthandProperty) {
var properties = [];
var foundProperties = {};
for (var i = 0; i < style.length; ++i) {
var individualProperty = style[i];
if (individualProperty in foundProperties ||
style.getPropertyShorthand(individualProperty) != shorthandProperty) {
continue;
}
foundProperties[individualProperty] = true;
properties.push(individualProperty);
}
return properties;
};
|