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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/audits/AuditRules.js

Issue 2455943003: Backend for css rule tracking (Closed)
Patch Set: Backend for css rule tracking Created 4 years, 1 month 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
OLDNEW
1 /* 1 /*
valih 2016/10/31 18:59:05 this file is a mistake... ignore the changes here
caseq 2016/10/31 19:06:06 let's revert it from the next one then :-)
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
11 * copyright notice, this list of conditions and the following disclaimer 11 * copyright notice, this list of conditions and the following disclaimer
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 /** 366 /**
367 * @override 367 * @override
368 * @param {!WebInspector.Target} target 368 * @param {!WebInspector.Target} target
369 * @param {!Array.<!WebInspector.NetworkRequest>} requests 369 * @param {!Array.<!WebInspector.NetworkRequest>} requests
370 * @param {!WebInspector.AuditRuleResult} result 370 * @param {!WebInspector.AuditRuleResult} result
371 * @param {function(?WebInspector.AuditRuleResult)} callback 371 * @param {function(?WebInspector.AuditRuleResult)} callback
372 * @param {!WebInspector.Progress} progress 372 * @param {!WebInspector.Progress} progress
373 */ 373 */
374 doRun: function(target, requests, result, callback, progress) 374 doRun: function(target, requests, result, callback, progress)
375 { 375 {
376 WebInspector.CSSModel.fromTarget(target).startSelectorRecording(true);
376 var domModel = WebInspector.DOMModel.fromTarget(target); 377 var domModel = WebInspector.DOMModel.fromTarget(target);
377 var cssModel = WebInspector.CSSModel.fromTarget(target); 378 var cssModel = WebInspector.CSSModel.fromTarget(target);
378 if (!domModel || !cssModel) { 379 if (!domModel || !cssModel) {
379 callback(null); 380 callback(null);
380 return; 381 return;
381 } 382 }
382 383
383 /** 384 /**
384 * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleShee ts 385 * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleShee ts
385 */ 386 */
(...skipping 19 matching lines...) Expand all
405 /** 406 /**
406 * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} style Sheets 407 * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} style Sheets
407 */ 408 */
408 function selectorsCallback(styleSheets) 409 function selectorsCallback(styleSheets)
409 { 410 {
410 if (progress.isCanceled()) { 411 if (progress.isCanceled()) {
411 callback(null); 412 callback(null);
412 return; 413 return;
413 } 414 }
414 415
415 var inlineBlockOrdinal = 0; 416 WebInspector.CSSModel.fromTarget(target).ruleListPromise().then( ruleListReceived);
416 var totalStylesheetSize = 0;
417 var totalUnusedStylesheetSize = 0;
418 var summary;
419 417
420 for (var i = 0; i < styleSheets.length; ++i) { 418 function ruleListReceived(usedRulesList) {
421 var styleSheet = styleSheets[i]; 419
422 var unusedRules = []; 420 var inlineBlockOrdinal = 0;
423 for (var curRule = 0; curRule < styleSheet.rules.length; ++c urRule) { 421 var totalStylesheetSize = 0;
424 var rule = styleSheet.rules[curRule]; 422 var totalUnusedStylesheetSize = 0;
425 if (!testedSelectors[rule.selectorText] || foundSelector s[rule.selectorText]) 423 var summary;
424
425 function getRange(range, URL) {
426 return [range.startLine, range.startColumn, URL];
427 }
428
429 var cssModel = WebInspector.CSSModel.fromTarget(target);
430
431 var testedRules = {};
432 for (var i = 0; i < usedRulesList.length; ++i) {
433 if (usedRulesList[i].wasUsed) {
434 var styleSheetHeader = cssModel.styleSheetHeaderForI d(usedRulesList[i].styleSheetId);
435 testedRules[getRange(usedRulesList[i].range, styleSh eetHeader.sourceURL)] = 1;
436 }
437 }
438
439 for (var i = 0; i < styleSheets.length; ++i) {
440 var styleSheet = styleSheets[i];
441 var unusedRules = [];
442
443 for (var curRule = 0; curRule < styleSheet.rules.length; ++curRule) {
444 var rule = styleSheet.rules[curRule];
445
446 if (testedRules[getRange(rule.styleRange, styleSheet .sourceURL)])
447 continue;
448 unusedRules.push(rule);
449 }
450
451 totalStylesheetSize += styleSheet.rules.length;
452 totalUnusedStylesheetSize += unusedRules.length;
453
454 if (!unusedRules.length)
426 continue; 455 continue;
427 unusedRules.push(rule.selectorText); 456
457 var resource = WebInspector.resourceForURL(styleSheet.so urceURL);
458 var isInlineBlock = resource && resource.request && reso urce.request.resourceType() === WebInspector.resourceTypes.Document;
459 var url = !isInlineBlock ? WebInspector.AuditRuleResult. linkifyDisplayName(styleSheet.sourceURL) : WebInspector.UIString("Inline block # %d", ++inlineBlockOrdinal);
460 var pctUnused = Math.round(100 * unusedRules.length / st yleSheet.rules.length);
461 if (!summary)
462 summary = result.addChild("", true);
463 var entry = summary.addFormatted("%s: %d% is not used by the current page.", url, pctUnused);
464
465 for (var j = 0; j < unusedRules.length; ++j)
466 entry.addSnippet(unusedRules[j].selectorText + " Li ne: " + unusedRules[j].styleRange.startLine);
467
468 result.violationCount += unusedRules.length;
428 } 469 }
429 totalStylesheetSize += styleSheet.rules.length;
430 totalUnusedStylesheetSize += unusedRules.length;
431 470
432 if (!unusedRules.length) 471 if (!totalUnusedStylesheetSize)
433 continue; 472 return callback(null);
434 473
435 var resource = WebInspector.resourceForURL(styleSheet.source URL); 474 var totalUnusedPercent = Math.round(100 * totalUnusedStylesh eetSize / totalStylesheetSize);
436 var isInlineBlock = resource && resource.request && resource .request.resourceType() === WebInspector.resourceTypes.Document; 475 summary.value = WebInspector.UIString("%s rules (%d%) of CSS not used by the current page.", totalUnusedStylesheetSize, totalUnusedPercent);
437 var url = !isInlineBlock ? WebInspector.AuditRuleResult.link ifyDisplayName(styleSheet.sourceURL) : WebInspector.UIString("Inline block #%d", ++inlineBlockOrdinal);
438 var pctUnused = Math.round(100 * unusedRules.length / styleS heet.rules.length);
439 if (!summary)
440 summary = result.addChild("", true);
441 var entry = summary.addFormatted("%s: %d% is not used by the current page.", url, pctUnused);
442 476
443 for (var j = 0; j < unusedRules.length; ++j) 477 callback(result);
444 entry.addSnippet(unusedRules[j]);
445
446 result.violationCount += unusedRules.length;
447 } 478 }
448
449 if (!totalUnusedStylesheetSize)
450 return callback(null);
451
452 var totalUnusedPercent = Math.round(100 * totalUnusedStylesheetS ize / totalStylesheetSize);
453 summary.value = WebInspector.UIString("%s rules (%d%) of CSS not used by the current page.", totalUnusedStylesheetSize, totalUnusedPercent);
454
455 callback(result);
456 } 479 }
457 480
458 /** 481 /**
459 * @param {?function()} boundSelectorsCallback 482 * @param {?function()} boundSelectorsCallback
460 * @param {string} selector 483 * @param {string} selector
461 * @param {?DOMAgent.NodeId} nodeId 484 * @param {?DOMAgent.NodeId} nodeId
462 */ 485 */
463 function queryCallback(boundSelectorsCallback, selector, nodeId) 486 function queryCallback(boundSelectorsCallback, selector, nodeId)
464 { 487 {
465 if (nodeId) 488 if (nodeId)
(...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after
1555 result.violationCount = badUrls.length; 1578 result.violationCount = badUrls.length;
1556 }, 1579 },
1557 1580
1558 _collectorCallback: function(matchingResourceData, request, cookie) 1581 _collectorCallback: function(matchingResourceData, request, cookie)
1559 { 1582 {
1560 matchingResourceData[request.url] = (matchingResourceData[request.url] | | 0) + cookie.size(); 1583 matchingResourceData[request.url] = (matchingResourceData[request.url] | | 0) + cookie.size();
1561 }, 1584 },
1562 1585
1563 __proto__: WebInspector.AuditRules.CookieRuleBase.prototype 1586 __proto__: WebInspector.AuditRules.CookieRuleBase.prototype
1564 }; 1587 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698