Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(828)

Side by Side Diff: Source/devtools/front_end/sdk/CSSStyleModel.js

Issue 1196193016: DevTools: [CSS] promisify CSS domain (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: address comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 27 matching lines...) Expand all
38 WebInspector.SDKModel.call(this, WebInspector.CSSStyleModel, target); 38 WebInspector.SDKModel.call(this, WebInspector.CSSStyleModel, target);
39 this._domModel = WebInspector.DOMModel.fromTarget(target); 39 this._domModel = WebInspector.DOMModel.fromTarget(target);
40 this._agent = target.cssAgent(); 40 this._agent = target.cssAgent();
41 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event s.SuspendStateChanged, this._suspendStateChanged, this); 41 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event s.SuspendStateChanged, this._suspendStateChanged, this);
42 this._pendingCommandsMajorState = []; 42 this._pendingCommandsMajorState = [];
43 this._styleLoader = new WebInspector.CSSStyleModel.ComputedStyleLoader(this) ; 43 this._styleLoader = new WebInspector.CSSStyleModel.ComputedStyleLoader(this) ;
44 this._domModel.addEventListener(WebInspector.DOMModel.Events.UndoRedoRequest ed, this._undoRedoRequested, this); 44 this._domModel.addEventListener(WebInspector.DOMModel.Events.UndoRedoRequest ed, this._undoRedoRequested, this);
45 this._domModel.addEventListener(WebInspector.DOMModel.Events.UndoRedoComplet ed, this._undoRedoCompleted, this); 45 this._domModel.addEventListener(WebInspector.DOMModel.Events.UndoRedoComplet ed, this._undoRedoCompleted, this);
46 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.Eve ntTypes.MainFrameNavigated, this._mainFrameNavigated, this); 46 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.Eve ntTypes.MainFrameNavigated, this._mainFrameNavigated, this);
47 target.registerCSSDispatcher(new WebInspector.CSSDispatcher(this)); 47 target.registerCSSDispatcher(new WebInspector.CSSDispatcher(this));
48 this._agent.enable(this._wasEnabled.bind(this)); 48 this._agent.enable().then(this._wasEnabled.bind(this));
49 /** @type {!Map.<string, !WebInspector.CSSStyleSheetHeader>} */ 49 /** @type {!Map.<string, !WebInspector.CSSStyleSheetHeader>} */
50 this._styleSheetIdToHeader = new Map(); 50 this._styleSheetIdToHeader = new Map();
51 /** @type {!Map.<string, !Object.<!PageAgent.FrameId, !Array.<!CSSAgent.Styl eSheetId>>>} */ 51 /** @type {!Map.<string, !Object.<!PageAgent.FrameId, !Array.<!CSSAgent.Styl eSheetId>>>} */
52 this._styleSheetIdsForURL = new Map(); 52 this._styleSheetIdsForURL = new Map();
53 } 53 }
54 54
55 WebInspector.CSSStyleModel.PseudoStatePropertyName = "pseudoState"; 55 WebInspector.CSSStyleModel.PseudoStatePropertyName = "pseudoState";
56 56
57 /** 57 /**
58 * @param {!WebInspector.CSSStyleModel} cssModel 58 * @param {!WebInspector.CSSStyleModel} cssModel
(...skipping 24 matching lines...) Expand all
83 83
84 WebInspector.CSSStyleModel.prototype = { 84 WebInspector.CSSStyleModel.prototype = {
85 /** 85 /**
86 * @param {function(!Array.<!WebInspector.CSSMedia>)} userCallback 86 * @param {function(!Array.<!WebInspector.CSSMedia>)} userCallback
87 */ 87 */
88 getMediaQueries: function(userCallback) 88 getMediaQueries: function(userCallback)
89 { 89 {
90 /** 90 /**
91 * @param {?Protocol.Error} error 91 * @param {?Protocol.Error} error
92 * @param {?Array.<!CSSAgent.CSSMedia>} payload 92 * @param {?Array.<!CSSAgent.CSSMedia>} payload
93 * @return {!Array.<!WebInspector.CSSMedia>}
93 * @this {!WebInspector.CSSStyleModel} 94 * @this {!WebInspector.CSSStyleModel}
94 */ 95 */
95 function callback(error, payload) 96 function parsePayload(error, payload)
96 { 97 {
97 var models = []; 98 return !error && payload ? WebInspector.CSSMedia.parseMediaArrayPayl oad(this, payload) : [];
98 if (!error && payload)
99 models = WebInspector.CSSMedia.parseMediaArrayPayload(this, payl oad);
100 userCallback(models);
101 } 99 }
102 this._agent.getMediaQueries(callback.bind(this)); 100
101 this._agent.getMediaQueries(parsePayload.bind(this))
102 .catchException([])
103 .then(userCallback);
103 }, 104 },
104 105
105 /** 106 /**
106 * @return {boolean} 107 * @return {boolean}
107 */ 108 */
108 isEnabled: function() 109 isEnabled: function()
109 { 110 {
110 return this._isEnabled; 111 return this._isEnabled;
111 }, 112 },
112 113
113 _wasEnabled: function() 114 /**
115 * @param {?Protocol.Error} error
116 */
117 _wasEnabled: function(error)
114 { 118 {
119 if (error) {
120 console.error("Failed to enabled CSS agent: " + error);
121 return;
122 }
115 this._isEnabled = true; 123 this._isEnabled = true;
116 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.ModelWas Enabled); 124 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.ModelWas Enabled);
117 }, 125 },
118 126
119 /** 127 /**
120 * @param {!DOMAgent.NodeId} nodeId 128 * @param {!DOMAgent.NodeId} nodeId
121 * @param {boolean} excludePseudo 129 * @param {boolean} excludePseudo
122 * @param {boolean} excludeInherited 130 * @param {boolean} excludeInherited
131 * @return {!Promise.<?WebInspector.CSSStyleModel.MatchedStyleResult>}
132 */
133 getMatchedStylesPromise: function(nodeId, excludePseudo, excludeInherited)
134 {
135 /**
136 * @param {?Protocol.Error} error
137 * @param {!Array.<!CSSAgent.RuleMatch>=} matchedPayload
138 * @param {!Array.<!CSSAgent.PseudoIdMatches>=} pseudoPayload
139 * @param {!Array.<!CSSAgent.InheritedStyleEntry>=} inheritedPayload
140 * @return {?WebInspector.CSSStyleModel.MatchedStyleResult}
141 * @this {WebInspector.CSSStyleModel}
142 */
143 function callback(error, matchedPayload, pseudoPayload, inheritedPayload )
144 {
145 if (error)
146 return null;
147
148 var matchedRules = WebInspector.CSSStyleModel.parseRuleMatchArrayPay load(this, matchedPayload);
149
150 var pseudoElements = [];
151 if (pseudoPayload) {
152 for (var i = 0; i < pseudoPayload.length; ++i) {
153 var entryPayload = pseudoPayload[i];
154 pseudoElements.push(new WebInspector.CSSStyleModel.PseudoEle mentMatches(entryPayload.pseudoId, WebInspector.CSSStyleModel.parseRuleMatchArra yPayload(this, entryPayload.matches)));
155 }
156 }
157
158 var inherited = [];
159 if (inheritedPayload) {
160 for (var i = 0; i < inheritedPayload.length; ++i) {
161 var entryPayload = inheritedPayload[i];
162 var inlineStyle = entryPayload.inlineStyle ? WebInspector.CS SStyleDeclaration.parsePayload(this, entryPayload.inlineStyle) : null;
163 var matchedCSSRules = entryPayload.matchedCSSRules ? WebInsp ector.CSSStyleModel.parseRuleMatchArrayPayload(this, entryPayload.matchedCSSRule s) : null;
164 inherited.push(new WebInspector.CSSStyleModel.InheritedMatch es(inlineStyle, matchedCSSRules));
165 }
166 }
167 return new WebInspector.CSSStyleModel.MatchedStyleResult(matchedRule s, inherited, pseudoElements);
168 }
169
170 return this._agent.getMatchedStylesForNode(nodeId, excludePseudo, exclud eInherited, callback.bind(this));
171 },
172
173 /**
174 * @param {!DOMAgent.NodeId} nodeId
175 * @param {boolean} excludePseudo
176 * @param {boolean} excludeInherited
123 * @param {function(?WebInspector.CSSStyleModel.MatchedStyleResult)} userCal lback 177 * @param {function(?WebInspector.CSSStyleModel.MatchedStyleResult)} userCal lback
124 */ 178 */
125 getMatchedStylesAsync: function(nodeId, excludePseudo, excludeInherited, use rCallback) 179 getMatchedStylesAsync: function(nodeId, excludePseudo, excludeInherited, use rCallback)
126 { 180 {
127 /** 181 this.getMatchedStylesPromise(nodeId, excludePseudo, excludeInherited)
128 * @param {?Protocol.Error} error 182 .catchException(null)
129 * @param {!Array.<!CSSAgent.RuleMatch>=} matchedPayload 183 .then(userCallback);
130 * @param {!Array.<!CSSAgent.PseudoIdMatches>=} pseudoPayload
131 * @param {!Array.<!CSSAgent.InheritedStyleEntry>=} inheritedPayload
132 * @this {WebInspector.CSSStyleModel}
133 */
134 function callback(error, matchedPayload, pseudoPayload, inheritedPayload )
135 {
136 if (error) {
137 if (userCallback)
138 userCallback(null);
139 return;
140 }
141
142 // Due to CSSOM inconsistencies, backend might send us illegal data. @see crbug.com/178410
143 var result = null;
144 try {
145 var matchedRules = WebInspector.CSSStyleModel.parseRuleMatchArra yPayload(this, matchedPayload);
146
147 var pseudoElements = [];
148 if (pseudoPayload) {
149 for (var i = 0; i < pseudoPayload.length; ++i) {
150 var entryPayload = pseudoPayload[i];
151 pseudoElements.push(new WebInspector.CSSStyleModel.Pseud oElementMatches(entryPayload.pseudoId, WebInspector.CSSStyleModel.parseRuleMatch ArrayPayload(this, entryPayload.matches)));
152 }
153 }
154
155 var inherited = [];
156 if (inheritedPayload) {
157 for (var i = 0; i < inheritedPayload.length; ++i) {
158 var entryPayload = inheritedPayload[i];
159 var inlineStyle = entryPayload.inlineStyle ? WebInspecto r.CSSStyleDeclaration.parsePayload(this, entryPayload.inlineStyle) : null;
160 var matchedCSSRules = entryPayload.matchedCSSRules ? Web Inspector.CSSStyleModel.parseRuleMatchArrayPayload(this, entryPayload.matchedCSS Rules) : null;
161 inherited.push(new WebInspector.CSSStyleModel.InheritedM atches(inlineStyle, matchedCSSRules));
162 }
163 }
164 result = new WebInspector.CSSStyleModel.MatchedStyleResult(match edRules, inherited, pseudoElements);
165 } catch (e) {
166 console.error(e);
167 }
168
169 if (userCallback)
170 userCallback(result);
171 }
172
173 this._agent.getMatchedStylesForNode(nodeId, excludePseudo, excludeInheri ted, callback.bind(this));
174 }, 184 },
175 185
176 /** 186 /**
177 * @param {!DOMAgent.NodeId} nodeId 187 * @param {!DOMAgent.NodeId} nodeId
178 * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback 188 * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback
179 */ 189 */
180 getComputedStyleAsync: function(nodeId, userCallback) 190 getComputedStyleAsync: function(nodeId, userCallback)
181 { 191 {
182 this._styleLoader.getComputedStyle(nodeId, userCallback); 192 this._styleLoader.getComputedStyle(nodeId, userCallback);
183 }, 193 },
184 194
185 /** 195 /**
186 * @param {number} nodeId 196 * @param {number} nodeId
187 * @param {function(?Array.<!CSSAgent.PlatformFontUsage>)} callback 197 * @param {function(?Array.<!CSSAgent.PlatformFontUsage>)} callback
188 */ 198 */
189 getPlatformFontsForNode: function(nodeId, callback) 199 getPlatformFontsForNode: function(nodeId, callback)
190 { 200 {
201 /**
202 * @param {?Protocol.Error} error
203 * @param {?Array.<!CSSAgent.PlatformFontUsage>} fonts
204 * @return {?Array.<!CSSAgent.PlatformFontUsage>}
205 */
191 function platformFontsCallback(error, fonts) 206 function platformFontsCallback(error, fonts)
192 { 207 {
193 if (error) 208 return !error && fonts ? fonts : null;
194 callback(null);
195 else
196 callback(fonts);
197 } 209 }
198 this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback); 210
211 this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback)
212 .catchException(null)
213 .then(callback)
199 }, 214 },
200 215
201 /** 216 /**
202 * @return {!Array.<!WebInspector.CSSStyleSheetHeader>} 217 * @return {!Array.<!WebInspector.CSSStyleSheetHeader>}
203 */ 218 */
204 allStyleSheets: function() 219 allStyleSheets: function()
205 { 220 {
206 var values = this._styleSheetIdToHeader.valuesArray(); 221 var values = this._styleSheetIdToHeader.valuesArray();
207 /** 222 /**
208 * @param {!WebInspector.CSSStyleSheetHeader} a 223 * @param {!WebInspector.CSSStyleSheetHeader} a
209 * @param {!WebInspector.CSSStyleSheetHeader} b 224 * @param {!WebInspector.CSSStyleSheetHeader} b
210 * @return {number} 225 * @return {number}
211 */ 226 */
212 function styleSheetComparator(a, b) 227 function styleSheetComparator(a, b)
213 { 228 {
214 if (a.sourceURL < b.sourceURL) 229 if (a.sourceURL < b.sourceURL)
215 return -1; 230 return -1;
216 else if (a.sourceURL > b.sourceURL) 231 else if (a.sourceURL > b.sourceURL)
217 return 1; 232 return 1;
218 return a.startLine - b.startLine || a.startColumn - b.startColumn; 233 return a.startLine - b.startLine || a.startColumn - b.startColumn;
219 } 234 }
220 values.sort(styleSheetComparator); 235 values.sort(styleSheetComparator);
221 236
222 return values; 237 return values;
223 }, 238 },
224 239
225 /** 240 /**
226 * @param {!DOMAgent.NodeId} nodeId 241 * @param {!DOMAgent.NodeId} nodeId
242 * @return {!Promise.<?WebInspector.CSSStyleModel.InlineStyleResult>}
243 */
244 getInlineStylesPromise: function(nodeId)
pfeldman 2015/06/25 16:16:11 inlineStylesPromise
lushnikov 2015/06/25 16:40:30 Done.
245 {
246 /**
247 * @param {?Protocol.Error} error
248 * @param {?CSSAgent.CSSStyle=} inlinePayload
249 * @param {?CSSAgent.CSSStyle=} attributesStylePayload
250 * @return {?WebInspector.CSSStyleModel.InlineStyleResult}
251 * @this {WebInspector.CSSStyleModel}
252 */
253 function callback(error, inlinePayload, attributesStylePayload)
254 {
255 if (error || !inlinePayload)
256 return null;
257 var inlineStyle = inlinePayload ? WebInspector.CSSStyleDeclaration.p arsePayload(this, inlinePayload) : null;
258 var attributesStyle = attributesStylePayload ? WebInspector.CSSStyle Declaration.parsePayload(this, attributesStylePayload) : null;
259 return new WebInspector.CSSStyleModel.InlineStyleResult(inlineStyle, attributesStyle);
260 }
261
262 return this._agent.getInlineStylesForNode(nodeId, callback.bind(this));
263 },
264
265 /**
266 * @param {!DOMAgent.NodeId} nodeId
227 * @param {function(?WebInspector.CSSStyleModel.InlineStyleResult)} userCall back 267 * @param {function(?WebInspector.CSSStyleModel.InlineStyleResult)} userCall back
228 */ 268 */
229 getInlineStylesAsync: function(nodeId, userCallback) 269 getInlineStylesAsync: function(nodeId, userCallback)
230 { 270 {
231 /** 271 this.getInlineStylesPromise(nodeId)
232 * @param {?Protocol.Error} error 272 .catchException(null)
233 * @param {?CSSAgent.CSSStyle=} inlinePayload 273 .then(userCallback);
234 * @param {?CSSAgent.CSSStyle=} attributesStylePayload
235 * @this {WebInspector.CSSStyleModel}
236 */
237 function callback(error, inlinePayload, attributesStylePayload)
238 {
239 if (error || !inlinePayload) {
240 userCallback(null);
241 return;
242 }
243 var inlineStyle = inlinePayload ? WebInspector.CSSStyleDeclaration.p arsePayload(this, inlinePayload) : null;
244 var attributesStyle = attributesStylePayload ? WebInspector.CSSStyle Declaration.parsePayload(this, attributesStylePayload) : null;
245 userCallback(new WebInspector.CSSStyleModel.InlineStyleResult(inline Style, attributesStyle));
246 }
247
248 this._agent.getInlineStylesForNode(nodeId, callback.bind(this));
249 }, 274 },
250 275
251 /** 276 /**
252 * @param {!WebInspector.DOMNode} node 277 * @param {!WebInspector.DOMNode} node
253 * @param {string} pseudoClass 278 * @param {string} pseudoClass
254 * @param {boolean} enable 279 * @param {boolean} enable
255 * @return {boolean} 280 * @return {boolean}
256 */ 281 */
257 forcePseudoState: function(node, pseudoClass, enable) 282 forcePseudoState: function(node, pseudoClass, enable)
258 { 283 {
(...skipping 13 matching lines...) Expand all
272 297
273 this._agent.forcePseudoState(node.id, pseudoClasses); 298 this._agent.forcePseudoState(node.id, pseudoClasses);
274 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.PseudoSt ateForced, { node: node, pseudoClass: pseudoClass, enable: enable }); 299 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.PseudoSt ateForced, { node: node, pseudoClass: pseudoClass, enable: enable });
275 return true; 300 return true;
276 }, 301 },
277 302
278 /** 303 /**
279 * @param {!CSSAgent.CSSRule} rule 304 * @param {!CSSAgent.CSSRule} rule
280 * @param {!DOMAgent.NodeId} nodeId 305 * @param {!DOMAgent.NodeId} nodeId
281 * @param {string} newSelector 306 * @param {string} newSelector
282 * @param {function(!WebInspector.CSSRule)} successCallback 307 * @param {function(?WebInspector.CSSRule)} userCallback
283 * @param {function()} failureCallback
284 */ 308 */
285 setRuleSelector: function(rule, nodeId, newSelector, successCallback, failur eCallback) 309 setRuleSelector: function(rule, nodeId, newSelector, userCallback)
286 { 310 {
287 /** 311 /**
288 * @param {!DOMAgent.NodeId} nodeId
289 * @param {function(!WebInspector.CSSRule)} successCallback
290 * @param {function()} failureCallback
291 * @param {?Protocol.Error} error 312 * @param {?Protocol.Error} error
292 * @param {string} newSelector 313 * @param {?CSSAgent.CSSRule} rulePayload
293 * @param {!CSSAgent.CSSRule} rulePayload 314 * @return {?CSSAgent.CSSRule}
294 * @this {WebInspector.CSSStyleModel} 315 * @this {WebInspector.CSSStyleModel}
295 */ 316 */
296 function callback(nodeId, successCallback, failureCallback, newSelector, error, rulePayload) 317 function callback(error, rulePayload)
297 { 318 {
298 this._pendingCommandsMajorState.pop(); 319 this._pendingCommandsMajorState.pop();
299 if (error) { 320 if (error || !rulePayload)
300 failureCallback(); 321 return null;
301 return;
302 }
303 this._domModel.markUndoableState(); 322 this._domModel.markUndoableState();
304 this._computeMatchingSelectors(rulePayload, nodeId, successCallback, failureCallback); 323 return rulePayload;
305 } 324 }
306 325
307 if (!rule.styleSheetId) 326 if (!rule.styleSheetId)
308 throw "No rule stylesheet id"; 327 throw "No rule stylesheet id";
309 WebInspector.userMetrics.StyleRuleEdited.record(); 328 WebInspector.userMetrics.StyleRuleEdited.record();
310 this._pendingCommandsMajorState.push(true); 329 this._pendingCommandsMajorState.push(true);
311 this._agent.setRuleSelector(rule.styleSheetId, rule.selectorRange, newSe lector, callback.bind(this, nodeId, successCallback, failureCallback, newSelecto r)); 330 this._agent.setRuleSelector(rule.styleSheetId, rule.selectorRange, newSe lector, callback.bind(this))
331 .then(this._computeMatchingSelectors.bind(this, nodeId))
332 .catchException(null)
333 .then(userCallback);
312 }, 334 },
313 335
314 /** 336 /**
315 * @param {!WebInspector.CSSMedia} media 337 * @param {!WebInspector.CSSMedia} media
316 * @param {string} newMediaText 338 * @param {string} newMediaText
317 * @param {function(!WebInspector.CSSMedia)} successCallback 339 * @param {function(?WebInspector.CSSMedia)} userCallback
318 * @param {function()} failureCallback
319 */ 340 */
320 setMediaText: function(media, newMediaText, successCallback, failureCallback ) 341 setMediaText: function(media, newMediaText, userCallback)
321 { 342 {
322 /** 343 /**
323 * @param {function(!WebInspector.CSSMedia)} successCallback
324 * @param {function()} failureCallback
325 * @param {?Protocol.Error} error 344 * @param {?Protocol.Error} error
326 * @param {!CSSAgent.CSSMedia} mediaPayload 345 * @param {!CSSAgent.CSSMedia} mediaPayload
346 * @return {?WebInspector.CSSMedia}
327 * @this {WebInspector.CSSStyleModel} 347 * @this {WebInspector.CSSStyleModel}
328 */ 348 */
329 function callback(successCallback, failureCallback, error, mediaPayload) 349 function parsePayload(error, mediaPayload)
330 { 350 {
331 this._pendingCommandsMajorState.pop(); 351 this._pendingCommandsMajorState.pop();
332 if (error) { 352 if (!mediaPayload)
333 failureCallback(); 353 return null;
334 return;
335 }
336 this._domModel.markUndoableState(); 354 this._domModel.markUndoableState();
337 successCallback(WebInspector.CSSMedia.parsePayload(this, mediaPayloa d)); 355 return WebInspector.CSSMedia.parsePayload(this, mediaPayload);
338 } 356 }
339 357
340 console.assert(!!media.parentStyleSheetId); 358 console.assert(!!media.parentStyleSheetId);
341 WebInspector.userMetrics.StyleRuleEdited.record(); 359 WebInspector.userMetrics.StyleRuleEdited.record();
342 this._pendingCommandsMajorState.push(true); 360 this._pendingCommandsMajorState.push(true);
343 this._agent.setMediaText(media.parentStyleSheetId, media.range, newMedia Text, callback.bind(this, successCallback, failureCallback)); 361 this._agent.setMediaText(media.parentStyleSheetId, media.range, newMedia Text, parsePayload.bind(this))
362 .catchException(null)
363 .then(userCallback);
344 }, 364 },
345 365
346 /** 366 /**
347 * @param {!CSSAgent.CSSRule} rulePayload
348 * @param {!DOMAgent.NodeId} nodeId 367 * @param {!DOMAgent.NodeId} nodeId
349 * @param {function(!WebInspector.CSSRule)} successCallback 368 * @param {?CSSAgent.CSSRule} rulePayload
350 * @param {function()} failureCallback 369 * @return {!Promise.<?WebInspector.CSSRule>}
351 */ 370 */
352 _computeMatchingSelectors: function(rulePayload, nodeId, successCallback, fa ilureCallback) 371 _computeMatchingSelectors: function(nodeId, rulePayload)
353 { 372 {
354 var ownerDocumentId = this._ownerDocumentId(nodeId); 373 var ownerDocumentId = this._ownerDocumentId(nodeId);
355 if (!ownerDocumentId) { 374 if (!ownerDocumentId || !rulePayload)
356 failureCallback(); 375 return Promise.resolve(/** @type {?WebInspector.CSSRule} */(null));
357 return;
358 }
359 var rule = WebInspector.CSSRule.parsePayload(this, rulePayload); 376 var rule = WebInspector.CSSRule.parsePayload(this, rulePayload);
360 var matchingSelectors = []; 377 var matchingSelectors = [];
361 var allSelectorsBarrier = new CallbackBarrier(); 378 var allSelectorsBarrier = new CallbackBarrier();
362 for (var i = 0; i < rule.selectors.length; ++i) { 379 for (var i = 0; i < rule.selectors.length; ++i) {
363 var selector = rule.selectors[i]; 380 var selector = rule.selectors[i];
364 var boundCallback = allSelectorsBarrier.createCallback(selectorQueri ed.bind(null, i, nodeId, matchingSelectors)); 381 var boundCallback = allSelectorsBarrier.createCallback(selectorQueri ed.bind(null, i, nodeId, matchingSelectors));
365 this._domModel.querySelectorAll(ownerDocumentId, selector.value, bou ndCallback); 382 this._domModel.querySelectorAll(ownerDocumentId, selector.value, bou ndCallback);
366 } 383 }
367 allSelectorsBarrier.callWhenDone(function() { 384 return new Promise(function(resolve) {
pfeldman 2015/06/25 16:16:11 extract function
lushnikov 2015/06/25 16:40:30 Done.
368 rule.matchingSelectors = matchingSelectors; 385 allSelectorsBarrier.callWhenDone(function() {
369 successCallback(rule); 386 rule.matchingSelectors = matchingSelectors;
387 resolve(rule);
388 });
370 }); 389 });
371 390
372 /** 391 /**
373 * @param {number} index 392 * @param {number} index
374 * @param {!DOMAgent.NodeId} nodeId 393 * @param {!DOMAgent.NodeId} nodeId
375 * @param {!Array.<number>} matchingSelectors 394 * @param {!Array.<number>} matchingSelectors
376 * @param {!Array.<!DOMAgent.NodeId>=} matchingNodeIds 395 * @param {!Array.<!DOMAgent.NodeId>=} matchingNodeIds
377 */ 396 */
378 function selectorQueried(index, nodeId, matchingSelectors, matchingNodeI ds) 397 function selectorQueried(index, nodeId, matchingSelectors, matchingNodeI ds)
379 { 398 {
380 if (!matchingNodeIds) 399 if (!matchingNodeIds)
381 return; 400 return;
382 if (matchingNodeIds.indexOf(nodeId) !== -1) 401 if (matchingNodeIds.indexOf(nodeId) !== -1)
383 matchingSelectors.push(index); 402 matchingSelectors.push(index);
384 } 403 }
385 }, 404 },
386 405
387 /** 406 /**
388 * @param {!CSSAgent.StyleSheetId} styleSheetId 407 * @param {!CSSAgent.StyleSheetId} styleSheetId
389 * @param {!WebInspector.DOMNode} node 408 * @param {!WebInspector.DOMNode} node
390 * @param {string} ruleText 409 * @param {string} ruleText
391 * @param {!WebInspector.TextRange} ruleLocation 410 * @param {!WebInspector.TextRange} ruleLocation
392 * @param {function(!WebInspector.CSSRule)} successCallback 411 * @param {function(?WebInspector.CSSRule)} userCallback
393 * @param {function()} failureCallback
394 */ 412 */
395 addRule: function(styleSheetId, node, ruleText, ruleLocation, successCallbac k, failureCallback) 413 addRule: function(styleSheetId, node, ruleText, ruleLocation, userCallback)
396 { 414 {
397 this._pendingCommandsMajorState.push(true); 415 this._pendingCommandsMajorState.push(true);
398 this._agent.addRule(styleSheetId, ruleText, ruleLocation, callback.bind( this)); 416 this._agent.addRule(styleSheetId, ruleText, ruleLocation, parsePayload.b ind(this))
417 .then(this._computeMatchingSelectors.bind(this, node.id))
418 .catchException(null)
419 .then(userCallback);
399 420
400 /** 421 /**
401 * @param {?Protocol.Error} error 422 * @param {?Protocol.Error} error
402 * @param {!CSSAgent.CSSRule} rulePayload 423 * @param {?CSSAgent.CSSRule} rulePayload
424 * @return {?CSSAgent.CSSRule}
403 * @this {WebInspector.CSSStyleModel} 425 * @this {WebInspector.CSSStyleModel}
404 */ 426 */
405 function callback(error, rulePayload) 427 function parsePayload(error, rulePayload)
406 { 428 {
407 this._pendingCommandsMajorState.pop(); 429 this._pendingCommandsMajorState.pop();
408 if (error) { 430 if (error || !rulePayload)
409 // Invalid syntax for a selector 431 return null;
410 failureCallback(); 432 this._domModel.markUndoableState();
411 } else { 433 return rulePayload;
412 this._domModel.markUndoableState();
413 this._computeMatchingSelectors(rulePayload, node.id, successCall back, failureCallback);
414 }
415 } 434 }
416 }, 435 },
417 436
418 /** 437 /**
419 * @param {!WebInspector.DOMNode} node 438 * @param {!WebInspector.DOMNode} node
420 * @param {function(?WebInspector.CSSStyleSheetHeader)} callback 439 * @param {function(?WebInspector.CSSStyleSheetHeader)} userCallback
421 */ 440 */
422 requestViaInspectorStylesheet: function(node, callback) 441 requestViaInspectorStylesheet: function(node, userCallback)
423 { 442 {
424 var frameId = node.frameId() || this.target().resourceTreeModel.mainFram e.id; 443 var frameId = node.frameId() || this.target().resourceTreeModel.mainFram e.id;
425 var headers = this._styleSheetIdToHeader.valuesArray(); 444 var headers = this._styleSheetIdToHeader.valuesArray();
426 for (var i = 0; i < headers.length; ++i) { 445 for (var i = 0; i < headers.length; ++i) {
427 var styleSheetHeader = headers[i]; 446 var styleSheetHeader = headers[i];
428 if (styleSheetHeader.frameId === frameId && styleSheetHeader.isViaIn spector()) { 447 if (styleSheetHeader.frameId === frameId && styleSheetHeader.isViaIn spector()) {
429 callback(styleSheetHeader); 448 userCallback(styleSheetHeader);
430 return; 449 return;
431 } 450 }
432 } 451 }
433 452
434 /** 453 /**
454 * @param {?Protocol.Error} error
455 * @param {?CSSAgent.StyleSheetId} styleSheetId
456 * @return {?WebInspector.CSSStyleSheetHeader}
435 * @this {WebInspector.CSSStyleModel} 457 * @this {WebInspector.CSSStyleModel}
436 * @param {?Protocol.Error} error
437 * @param {!CSSAgent.StyleSheetId} styleSheetId
438 */ 458 */
439 function innerCallback(error, styleSheetId) 459 function innerCallback(error, styleSheetId)
440 { 460 {
441 if (error) { 461 return !error && styleSheetId ? this._styleSheetIdToHeader.get(style SheetId) || null : null;
442 console.error(error);
443 callback(null);
444 }
445
446 callback(this._styleSheetIdToHeader.get(styleSheetId) || null);
447 } 462 }
448 463
449 this._agent.createStyleSheet(frameId, innerCallback.bind(this)); 464 this._agent.createStyleSheet(frameId, innerCallback.bind(this))
465 .catchException(null)
466 .then(userCallback)
450 }, 467 },
451 468
452 mediaQueryResultChanged: function() 469 mediaQueryResultChanged: function()
453 { 470 {
454 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.MediaQue ryResultChanged); 471 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.MediaQue ryResultChanged);
455 }, 472 },
456 473
457 /** 474 /**
458 * @param {!CSSAgent.StyleSheetId} id 475 * @param {!CSSAgent.StyleSheetId} id
459 * @return {?WebInspector.CSSStyleSheetHeader} 476 * @return {?WebInspector.CSSStyleSheetHeader}
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.Styl eSheetRemoved, headers[i]); 627 this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.Styl eSheetRemoved, headers[i]);
611 }, 628 },
612 629
613 _suspendStateChanged: function() 630 _suspendStateChanged: function()
614 { 631 {
615 if (WebInspector.targetManager.allTargetsSuspended()) { 632 if (WebInspector.targetManager.allTargetsSuspended()) {
616 this._agent.disable(); 633 this._agent.disable();
617 this._isEnabled = false; 634 this._isEnabled = false;
618 } else { 635 } else {
619 this._resetStyleSheets(); 636 this._resetStyleSheets();
620 this._agent.enable(this._wasEnabled.bind(this)); 637 this._agent.enable().then(this._wasEnabled.bind(this));
621 } 638 }
622 }, 639 },
623 640
624 __proto__: WebInspector.SDKModel.prototype 641 __proto__: WebInspector.SDKModel.prototype
625 } 642 }
626 643
627 /** 644 /**
628 * @constructor 645 * @constructor
629 * @extends {WebInspector.SDKObject} 646 * @extends {WebInspector.SDKObject}
630 * @param {!WebInspector.CSSStyleModel} cssModel 647 * @param {!WebInspector.CSSStyleModel} cssModel
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 { 912 {
896 index = (typeof index === "undefined") ? this.pastLastSourcePropertyInde x() : index; 913 index = (typeof index === "undefined") ? this.pastLastSourcePropertyInde x() : index;
897 var property = new WebInspector.CSSProperty(this, index, "", "", false, false, true, false, "", this._insertionRange(index)); 914 var property = new WebInspector.CSSProperty(this, index, "", "", false, false, true, false, "", this._insertionRange(index));
898 property._setActive(true); 915 property._setActive(true);
899 return property; 916 return property;
900 }, 917 },
901 918
902 /** 919 /**
903 * @param {string} text 920 * @param {string} text
904 * @param {boolean} majorChange 921 * @param {boolean} majorChange
905 * @param {function(?WebInspector.CSSStyleDeclaration)} callback 922 * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback
906 */ 923 */
907 setText: function(text, majorChange, callback) 924 setText: function(text, majorChange, userCallback)
908 { 925 {
909 if (!this.styleSheetId) { 926 if (!this.styleSheetId) {
910 callback(null); 927 userCallback(null);
911 return; 928 return;
912 } 929 }
913 930
914 /** 931 /**
915 * @param {?Protocol.Error} error 932 * @param {?Protocol.Error} error
916 * @param {!CSSAgent.CSSStyle} stylePayload 933 * @param {?CSSAgent.CSSStyle} stylePayload
934 * @return {?WebInspector.CSSStyleDeclaration}
917 * @this {WebInspector.CSSStyleDeclaration} 935 * @this {WebInspector.CSSStyleDeclaration}
918 */ 936 */
919 function mycallback(error, stylePayload) 937 function parsePayload(error, stylePayload)
920 { 938 {
921 this._cssModel._pendingCommandsMajorState.pop(); 939 this._cssModel._pendingCommandsMajorState.pop();
922 if (!error) { 940 if (error || !stylePayload)
923 if (majorChange) 941 return null;
924 this._cssModel._domModel.markUndoableState(); 942
925 callback(WebInspector.CSSStyleDeclaration.parsePayload(this._css Model, stylePayload)); 943 if (majorChange)
926 return; 944 this._cssModel._domModel.markUndoableState();
927 } 945 return WebInspector.CSSStyleDeclaration.parsePayload(this._cssModel, stylePayload);
928 callback(null);
929 } 946 }
930 947
931 this._cssModel._pendingCommandsMajorState.push(majorChange); 948 this._cssModel._pendingCommandsMajorState.push(majorChange);
932 this._cssModel._agent.setStyleText(this.styleSheetId, this.range.seriali zeToObject(), text, mycallback.bind(this)); 949 this._cssModel._agent.setStyleText(this.styleSheetId, this.range.seriali zeToObject(), text, parsePayload.bind(this))
950 .catchException(null)
951 .then(userCallback)
933 }, 952 },
934 953
935 /** 954 /**
936 * @param {number} index 955 * @param {number} index
937 * @param {string} name 956 * @param {string} name
938 * @param {string} value 957 * @param {string} value
939 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback 958 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback
940 */ 959 */
941 insertPropertyAt: function(index, name, value, userCallback) 960 insertPropertyAt: function(index, name, value, userCallback)
942 { 961 {
(...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after
1762 * @return {string} 1781 * @return {string}
1763 */ 1782 */
1764 _trimSourceURL: function(text) 1783 _trimSourceURL: function(text)
1765 { 1784 {
1766 var sourceURLRegex = /\n[\040\t]*\/\*[#@][\040\t]sourceURL=[\040\t]*([^\ s]*)[\040\t]*\*\/[\040\t]*$/mg; 1785 var sourceURLRegex = /\n[\040\t]*\/\*[#@][\040\t]sourceURL=[\040\t]*([^\ s]*)[\040\t]*\*\/[\040\t]*$/mg;
1767 return text.replace(sourceURLRegex, ""); 1786 return text.replace(sourceURLRegex, "");
1768 }, 1787 },
1769 1788
1770 /** 1789 /**
1771 * @override 1790 * @override
1772 * @param {function(string)} callback 1791 * @param {function(string)} userCallback
1773 */ 1792 */
1774 requestContent: function(callback) 1793 requestContent: function(userCallback)
1775 { 1794 {
1776 this._cssModel._agent.getStyleSheetText(this.id, textCallback.bind(this) ); 1795 this._cssModel._agent.getStyleSheetText(this.id, textCallback.bind(this) )
1796 .catchException("")
1797 .then(userCallback)
1777 1798
1778 /** 1799 /**
1800 * @param {?Protocol.Error} error
1801 * @param {?string} text
1802 * @return {string}
1779 * @this {WebInspector.CSSStyleSheetHeader} 1803 * @this {WebInspector.CSSStyleSheetHeader}
1780 */ 1804 */
1781 function textCallback(error, text) 1805 function textCallback(error, text)
1782 { 1806 {
1783 if (error) { 1807 if (error || !text) {
1784 WebInspector.console.error("Failed to get text for stylesheet " + this.id + ": " + error); 1808 WebInspector.console.error("Failed to get text for stylesheet " + this.id + ": " + error)
1785 text = ""; 1809 text = "";
1786 // Fall through. 1810 // Fall through.
1787 } 1811 }
1788 text = this._trimSourceURL(text); 1812 return this._trimSourceURL(text);
1789 callback(text);
1790 } 1813 }
1791 }, 1814 },
1792 1815
1793 /** 1816 /**
1794 * @override 1817 * @override
1795 */ 1818 */
1796 searchInContent: function(query, caseSensitive, isRegex, callback) 1819 searchInContent: function(query, caseSensitive, isRegex, callback)
1797 { 1820 {
1798 function performSearch(content) 1821 function performSearch(content)
1799 { 1822 {
1800 callback(WebInspector.ContentProvider.performSearchInContent(content , query, caseSensitive, isRegex)); 1823 callback(WebInspector.ContentProvider.performSearchInContent(content , query, caseSensitive, isRegex));
1801 } 1824 }
1802 1825
1803 // searchInContent should call back later. 1826 // searchInContent should call back later.
1804 this.requestContent(performSearch); 1827 this.requestContent(performSearch);
1805 }, 1828 },
1806 1829
1807 /** 1830 /**
1808 * @param {string} newText 1831 * @param {string} newText
1809 * @param {function(?Protocol.Error)} callback 1832 * @param {function(?Protocol.Error)} callback
1810 */ 1833 */
1811 setContent: function(newText, callback) 1834 setContent: function(newText, callback)
1812 { 1835 {
1813 newText = this._trimSourceURL(newText); 1836 newText = this._trimSourceURL(newText);
1814 if (this.hasSourceURL) 1837 if (this.hasSourceURL)
1815 newText += "\n/*# sourceURL=" + this.sourceURL + " */"; 1838 newText += "\n/*# sourceURL=" + this.sourceURL + " */";
1816 this._cssModel._agent.setStyleSheetText(this.id, newText, callback); 1839 this._cssModel._agent.setStyleSheetText(this.id, newText, extractProtoco lError)
1840 .then(callback);
1841
1842 /**
1843 * @param {?Protocol.Error} error
1844 */
1845 function extractProtocolError(error)
1846 {
1847 return error || null;
1848 }
1817 }, 1849 },
1818 1850
1819 /** 1851 /**
1820 * @return {boolean} 1852 * @return {boolean}
1821 */ 1853 */
1822 isViaInspector: function() 1854 isViaInspector: function()
1823 { 1855 {
1824 return this.origin === "inspector"; 1856 return this.origin === "inspector";
1825 } 1857 }
1826 } 1858 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1890 */ 1922 */
1891 getComputedStyle: function(nodeId, userCallback) 1923 getComputedStyle: function(nodeId, userCallback)
1892 { 1924 {
1893 if (this._nodeIdToCallbackData[nodeId]) { 1925 if (this._nodeIdToCallbackData[nodeId]) {
1894 this._nodeIdToCallbackData[nodeId].push(userCallback); 1926 this._nodeIdToCallbackData[nodeId].push(userCallback);
1895 return; 1927 return;
1896 } 1928 }
1897 1929
1898 this._nodeIdToCallbackData[nodeId] = [userCallback]; 1930 this._nodeIdToCallbackData[nodeId] = [userCallback];
1899 1931
1900 this._cssModel._agent.getComputedStyleForNode(nodeId, resultCallback.bin d(this, nodeId)); 1932 this._cssModel._agent.getComputedStyleForNode(nodeId, parsePayload.bind( this))
1933 .catchException(null)
1934 .then(broadcast.bind(this, nodeId))
1935
1936 /**
1937 * @param {?Protocol.Error} error
1938 * @param {!Array.<!CSSAgent.CSSComputedStyleProperty>} computedPayload
1939 * @return {?WebInspector.CSSStyleDeclaration}
1940 * @this {WebInspector.CSSStyleModel.ComputedStyleLoader}
1941 */
1942 function parsePayload(error, computedPayload)
1943 {
1944 return !error && computedPayload ? WebInspector.CSSStyleDeclaration. parseComputedStylePayload(this._cssModel, computedPayload) : null;
1945 }
1901 1946
1902 /** 1947 /**
1903 * @param {!DOMAgent.NodeId} nodeId 1948 * @param {!DOMAgent.NodeId} nodeId
1904 * @param {?Protocol.Error} error 1949 * @param {?WebInspector.CSSStyleDeclaration} computedStyle
1905 * @param {!Array.<!CSSAgent.CSSComputedStyleProperty>} computedPayload
1906 * @this {WebInspector.CSSStyleModel.ComputedStyleLoader} 1950 * @this {WebInspector.CSSStyleModel.ComputedStyleLoader}
1907 */ 1951 */
1908 function resultCallback(nodeId, error, computedPayload) 1952 function broadcast(nodeId, computedStyle)
1909 { 1953 {
1910 var computedStyle = (error || !computedPayload) ? null : WebInspecto r.CSSStyleDeclaration.parseComputedStylePayload(this._cssModel, computedPayload) ;
1911 var callbacks = this._nodeIdToCallbackData[nodeId]; 1954 var callbacks = this._nodeIdToCallbackData[nodeId];
1912 1955
1913 // The loader has been reset. 1956 // The loader has been reset.
1914 if (!callbacks) 1957 if (!callbacks)
1915 return; 1958 return;
1916 1959
1917 delete this._nodeIdToCallbackData[nodeId]; 1960 delete this._nodeIdToCallbackData[nodeId];
1918 for (var i = 0; i < callbacks.length; ++i) 1961 for (var i = 0; i < callbacks.length; ++i)
1919 callbacks[i](computedStyle); 1962 callbacks[i](computedStyle);
1920 } 1963 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1980 * @constructor 2023 * @constructor
1981 * @param {?WebInspector.CSSStyleDeclaration} inlineStyle 2024 * @param {?WebInspector.CSSStyleDeclaration} inlineStyle
1982 * @param {?WebInspector.CSSStyleDeclaration} attributesStyle 2025 * @param {?WebInspector.CSSStyleDeclaration} attributesStyle
1983 */ 2026 */
1984 WebInspector.CSSStyleModel.InlineStyleResult = function(inlineStyle, attributesS tyle) 2027 WebInspector.CSSStyleModel.InlineStyleResult = function(inlineStyle, attributesS tyle)
1985 { 2028 {
1986 this.inlineStyle = inlineStyle; 2029 this.inlineStyle = inlineStyle;
1987 this.attributesStyle = attributesStyle; 2030 this.attributesStyle = attributesStyle;
1988 } 2031 }
1989 2032
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698