Index: third_party/WebKit/Source/devtools/front_end/audits/AuditRules.js |
diff --git a/third_party/WebKit/Source/devtools/front_end/audits/AuditRules.js b/third_party/WebKit/Source/devtools/front_end/audits/AuditRules.js |
index 7731fac6ece3dbe29b5a60f872fbc2a67fdb342e..493cdfd8611a598588dd3e5d8117c893bb0ff914 100644 |
--- a/third_party/WebKit/Source/devtools/front_end/audits/AuditRules.js |
+++ b/third_party/WebKit/Source/devtools/front_end/audits/AuditRules.js |
@@ -27,19 +27,17 @@ |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
- |
WebInspector.AuditRules.IPAddressRegexp = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; |
-WebInspector.AuditRules.CacheableResponseCodes = |
-{ |
- 200: true, |
- 203: true, |
- 206: true, |
- 300: true, |
- 301: true, |
- 410: true, |
+WebInspector.AuditRules.CacheableResponseCodes = { |
+ 200: true, |
+ 203: true, |
+ 206: true, |
+ 300: true, |
+ 301: true, |
+ 410: true, |
- 304: true // Underlying request is cacheable |
+ 304: true // Underlying request is cacheable |
}; |
/** |
@@ -48,459 +46,446 @@ WebInspector.AuditRules.CacheableResponseCodes = |
* @param {boolean} needFullResources |
* @return {!Object.<string, !Array.<!WebInspector.NetworkRequest|string>>} |
*/ |
-WebInspector.AuditRules.getDomainToResourcesMap = function(requests, types, needFullResources) |
-{ |
- var domainToResourcesMap = {}; |
- for (var i = 0, size = requests.length; i < size; ++i) { |
- var request = requests[i]; |
- if (types && types.indexOf(request.resourceType()) === -1) |
- continue; |
- var parsedURL = request.url.asParsedURL(); |
- if (!parsedURL) |
- continue; |
- var domain = parsedURL.host; |
- var domainResources = domainToResourcesMap[domain]; |
- if (domainResources === undefined) { |
- domainResources = []; |
- domainToResourcesMap[domain] = domainResources; |
- } |
- domainResources.push(needFullResources ? request : request.url); |
+WebInspector.AuditRules.getDomainToResourcesMap = function(requests, types, needFullResources) { |
+ var domainToResourcesMap = {}; |
+ for (var i = 0, size = requests.length; i < size; ++i) { |
+ var request = requests[i]; |
+ if (types && types.indexOf(request.resourceType()) === -1) |
+ continue; |
+ var parsedURL = request.url.asParsedURL(); |
+ if (!parsedURL) |
+ continue; |
+ var domain = parsedURL.host; |
+ var domainResources = domainToResourcesMap[domain]; |
+ if (domainResources === undefined) { |
+ domainResources = []; |
+ domainToResourcesMap[domain] = domainResources; |
} |
- return domainToResourcesMap; |
+ domainResources.push(needFullResources ? request : request.url); |
+ } |
+ return domainToResourcesMap; |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.GzipRule = function() |
-{ |
- WebInspector.AuditRule.call(this, "network-gzip", WebInspector.UIString("Enable gzip compression")); |
-}; |
- |
-WebInspector.AuditRules.GzipRule.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var totalSavings = 0; |
- var compressedSize = 0; |
- var candidateSize = 0; |
- var summary = result.addChild("", true); |
- for (var i = 0, length = requests.length; i < length; ++i) { |
- var request = requests[i]; |
- if (request.cached() || request.statusCode === 304) |
- continue; // Do not test cached resources. |
- if (this._shouldCompress(request)) { |
- var size = request.resourceSize; |
- candidateSize += size; |
- if (this._isCompressed(request)) { |
- compressedSize += size; |
- continue; |
- } |
- var savings = 2 * size / 3; |
- totalSavings += savings; |
- summary.addFormatted("%r could save ~%s", request.url, Number.bytesToString(savings)); |
- result.violationCount++; |
- } |
+WebInspector.AuditRules.GzipRule = class extends WebInspector.AuditRule { |
+ constructor() { |
+ super('network-gzip', WebInspector.UIString('Enable gzip compression')); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var totalSavings = 0; |
+ var compressedSize = 0; |
+ var candidateSize = 0; |
+ var summary = result.addChild('', true); |
+ for (var i = 0, length = requests.length; i < length; ++i) { |
+ var request = requests[i]; |
+ if (request.cached() || request.statusCode === 304) |
+ continue; // Do not test cached resources. |
+ if (this._shouldCompress(request)) { |
+ var size = request.resourceSize; |
+ candidateSize += size; |
+ if (this._isCompressed(request)) { |
+ compressedSize += size; |
+ continue; |
} |
- if (!totalSavings) { |
- callback(null); |
- return; |
- } |
- summary.value = WebInspector.UIString("Compressing the following resources with gzip could reduce their transfer size by about two thirds (~%s):", Number.bytesToString(totalSavings)); |
- callback(result); |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- */ |
- _isCompressed: function(request) |
- { |
- var encodingHeader = request.responseHeaderValue("Content-Encoding"); |
- if (!encodingHeader) |
- return false; |
- |
- return /\b(?:gzip|deflate)\b/.test(encodingHeader); |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- */ |
- _shouldCompress: function(request) |
- { |
- return request.resourceType().isTextType() && request.parsedURL.host && request.resourceSize !== undefined && request.resourceSize > 150; |
- }, |
- |
- __proto__: WebInspector.AuditRule.prototype |
+ var savings = 2 * size / 3; |
+ totalSavings += savings; |
+ summary.addFormatted('%r could save ~%s', request.url, Number.bytesToString(savings)); |
+ result.violationCount++; |
+ } |
+ } |
+ if (!totalSavings) { |
+ callback(null); |
+ return; |
+ } |
+ summary.value = WebInspector.UIString( |
+ 'Compressing the following resources with gzip could reduce their transfer size by about two thirds (~%s):', |
+ Number.bytesToString(totalSavings)); |
+ callback(result); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ */ |
+ _isCompressed(request) { |
+ var encodingHeader = request.responseHeaderValue('Content-Encoding'); |
+ if (!encodingHeader) |
+ return false; |
+ |
+ return /\b(?:gzip|deflate)\b/.test(encodingHeader); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ */ |
+ _shouldCompress(request) { |
+ return request.resourceType().isTextType() && request.parsedURL.host && request.resourceSize !== undefined && |
+ request.resourceSize > 150; |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
- * @param {string} id |
- * @param {string} name |
- * @param {!WebInspector.ResourceType} type |
- * @param {string} resourceTypeName |
- * @param {boolean} allowedPerDomain |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CombineExternalResourcesRule = function(id, name, type, resourceTypeName, allowedPerDomain) |
-{ |
- WebInspector.AuditRule.call(this, id, name); |
+WebInspector.AuditRules.CombineExternalResourcesRule = class extends WebInspector.AuditRule { |
+ /** |
+ * @param {string} id |
+ * @param {string} name |
+ * @param {!WebInspector.ResourceType} type |
+ * @param {string} resourceTypeName |
+ * @param {boolean} allowedPerDomain |
+ */ |
+ constructor(id, name, type, resourceTypeName, allowedPerDomain) { |
+ super(id, name); |
this._type = type; |
this._resourceTypeName = resourceTypeName; |
this._allowedPerDomain = allowedPerDomain; |
-}; |
- |
-WebInspector.AuditRules.CombineExternalResourcesRule.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap(requests, [this._type], false); |
- var penalizedResourceCount = 0; |
- // TODO: refactor according to the chosen i18n approach |
- var summary = result.addChild("", true); |
- for (var domain in domainToResourcesMap) { |
- var domainResources = domainToResourcesMap[domain]; |
- var extraResourceCount = domainResources.length - this._allowedPerDomain; |
- if (extraResourceCount <= 0) |
- continue; |
- penalizedResourceCount += extraResourceCount - 1; |
- summary.addChild(WebInspector.UIString("%d %s resources served from %s.", domainResources.length, this._resourceTypeName, WebInspector.AuditRuleResult.resourceDomain(domain))); |
- result.violationCount += domainResources.length; |
- } |
- if (!penalizedResourceCount) { |
- callback(null); |
- return; |
- } |
- |
- summary.value = WebInspector.UIString("There are multiple resources served from same domain. Consider combining them into as few files as possible."); |
- callback(result); |
- }, |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap(requests, [this._type], false); |
+ var penalizedResourceCount = 0; |
+ // TODO: refactor according to the chosen i18n approach |
+ var summary = result.addChild('', true); |
+ for (var domain in domainToResourcesMap) { |
+ var domainResources = domainToResourcesMap[domain]; |
+ var extraResourceCount = domainResources.length - this._allowedPerDomain; |
+ if (extraResourceCount <= 0) |
+ continue; |
+ penalizedResourceCount += extraResourceCount - 1; |
+ summary.addChild(WebInspector.UIString( |
+ '%d %s resources served from %s.', domainResources.length, this._resourceTypeName, |
+ WebInspector.AuditRuleResult.resourceDomain(domain))); |
+ result.violationCount += domainResources.length; |
+ } |
+ if (!penalizedResourceCount) { |
+ callback(null); |
+ return; |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ summary.value = WebInspector.UIString( |
+ 'There are multiple resources served from same domain. Consider combining them into as few files as possible.'); |
+ callback(result); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRules.CombineExternalResourcesRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CombineJsResourcesRule = function(allowedPerDomain) { |
- WebInspector.AuditRules.CombineExternalResourcesRule.call(this, "page-externaljs", WebInspector.UIString("Combine external JavaScript"), WebInspector.resourceTypes.Script, "JavaScript", allowedPerDomain); |
-}; |
- |
-WebInspector.AuditRules.CombineJsResourcesRule.prototype = { |
- __proto__: WebInspector.AuditRules.CombineExternalResourcesRule.prototype |
+WebInspector.AuditRules.CombineJsResourcesRule = class extends WebInspector.AuditRules.CombineExternalResourcesRule { |
+ constructor(allowedPerDomain) { |
+ super( |
+ 'page-externaljs', WebInspector.UIString('Combine external JavaScript'), WebInspector.resourceTypes.Script, |
+ 'JavaScript', allowedPerDomain); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRules.CombineExternalResourcesRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CombineCssResourcesRule = function(allowedPerDomain) { |
- WebInspector.AuditRules.CombineExternalResourcesRule.call(this, "page-externalcss", WebInspector.UIString("Combine external CSS"), WebInspector.resourceTypes.Stylesheet, "CSS", allowedPerDomain); |
-}; |
- |
-WebInspector.AuditRules.CombineCssResourcesRule.prototype = { |
- __proto__: WebInspector.AuditRules.CombineExternalResourcesRule.prototype |
+WebInspector.AuditRules.CombineCssResourcesRule = class extends WebInspector.AuditRules.CombineExternalResourcesRule { |
+ constructor(allowedPerDomain) { |
+ super( |
+ 'page-externalcss', WebInspector.UIString('Combine external CSS'), WebInspector.resourceTypes.Stylesheet, 'CSS', |
+ allowedPerDomain); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.MinimizeDnsLookupsRule = function(hostCountThreshold) { |
- WebInspector.AuditRule.call(this, "network-minimizelookups", WebInspector.UIString("Minimize DNS lookups")); |
+WebInspector.AuditRules.MinimizeDnsLookupsRule = class extends WebInspector.AuditRule { |
+ constructor(hostCountThreshold) { |
+ super('network-minimizelookups', WebInspector.UIString('Minimize DNS lookups')); |
this._hostCountThreshold = hostCountThreshold; |
-}; |
- |
-WebInspector.AuditRules.MinimizeDnsLookupsRule.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var summary = result.addChild(""); |
- var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap(requests, null, false); |
- for (var domain in domainToResourcesMap) { |
- if (domainToResourcesMap[domain].length > 1) |
- continue; |
- var parsedURL = domain.asParsedURL(); |
- if (!parsedURL) |
- continue; |
- if (!parsedURL.host.search(WebInspector.AuditRules.IPAddressRegexp)) |
- continue; // an IP address |
- summary.addSnippet(domain); |
- result.violationCount++; |
- } |
- if (!summary.children || summary.children.length <= this._hostCountThreshold) { |
- callback(null); |
- return; |
- } |
- |
- summary.value = WebInspector.UIString("The following domains only serve one resource each. If possible, avoid the extra DNS lookups by serving these resources from existing domains."); |
- callback(result); |
- }, |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var summary = result.addChild(''); |
+ var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap(requests, null, false); |
+ for (var domain in domainToResourcesMap) { |
+ if (domainToResourcesMap[domain].length > 1) |
+ continue; |
+ var parsedURL = domain.asParsedURL(); |
+ if (!parsedURL) |
+ continue; |
+ if (!parsedURL.host.search(WebInspector.AuditRules.IPAddressRegexp)) |
+ continue; // an IP address |
+ summary.addSnippet(domain); |
+ result.violationCount++; |
+ } |
+ if (!summary.children || summary.children.length <= this._hostCountThreshold) { |
+ callback(null); |
+ return; |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ summary.value = WebInspector.UIString( |
+ 'The following domains only serve one resource each. If possible, avoid the extra DNS lookups by serving these resources from existing domains.'); |
+ callback(result); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.ParallelizeDownloadRule = function(optimalHostnameCount, minRequestThreshold, minBalanceThreshold) |
-{ |
- WebInspector.AuditRule.call(this, "network-parallelizehosts", WebInspector.UIString("Parallelize downloads across hostnames")); |
+WebInspector.AuditRules.ParallelizeDownloadRule = class extends WebInspector.AuditRule { |
+ constructor(optimalHostnameCount, minRequestThreshold, minBalanceThreshold) { |
+ super('network-parallelizehosts', WebInspector.UIString('Parallelize downloads across hostnames')); |
this._optimalHostnameCount = optimalHostnameCount; |
this._minRequestThreshold = minRequestThreshold; |
this._minBalanceThreshold = minBalanceThreshold; |
-}; |
- |
-WebInspector.AuditRules.ParallelizeDownloadRule.prototype = { |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
/** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
+ * @param {string} a |
+ * @param {string} b |
*/ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- /** |
- * @param {string} a |
- * @param {string} b |
- */ |
- function hostSorter(a, b) |
- { |
- var aCount = domainToResourcesMap[a].length; |
- var bCount = domainToResourcesMap[b].length; |
- return (aCount < bCount) ? 1 : (aCount === bCount) ? 0 : -1; |
- } |
- |
- var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap( |
- requests, |
- [WebInspector.resourceTypes.Stylesheet, WebInspector.resourceTypes.Image], |
- true); |
- |
- var hosts = []; |
- for (var url in domainToResourcesMap) |
- hosts.push(url); |
+ function hostSorter(a, b) { |
+ var aCount = domainToResourcesMap[a].length; |
+ var bCount = domainToResourcesMap[b].length; |
+ return (aCount < bCount) ? 1 : (aCount === bCount) ? 0 : -1; |
+ } |
- if (!hosts.length) { |
- callback(null); // no hosts (local file or something) |
- return; |
- } |
+ var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap( |
+ requests, [WebInspector.resourceTypes.Stylesheet, WebInspector.resourceTypes.Image], true); |
- hosts.sort(hostSorter); |
+ var hosts = []; |
+ for (var url in domainToResourcesMap) |
+ hosts.push(url); |
- var optimalHostnameCount = this._optimalHostnameCount; |
- if (hosts.length > optimalHostnameCount) |
- hosts.splice(optimalHostnameCount); |
+ if (!hosts.length) { |
+ callback(null); // no hosts (local file or something) |
+ return; |
+ } |
- var busiestHostResourceCount = domainToResourcesMap[hosts[0]].length; |
- var requestCountAboveThreshold = busiestHostResourceCount - this._minRequestThreshold; |
- if (requestCountAboveThreshold <= 0) { |
- callback(null); |
- return; |
- } |
+ hosts.sort(hostSorter); |
- var avgResourcesPerHost = 0; |
- for (var i = 0, size = hosts.length; i < size; ++i) |
- avgResourcesPerHost += domainToResourcesMap[hosts[i]].length; |
+ var optimalHostnameCount = this._optimalHostnameCount; |
+ if (hosts.length > optimalHostnameCount) |
+ hosts.splice(optimalHostnameCount); |
- // Assume optimal parallelization. |
- avgResourcesPerHost /= optimalHostnameCount; |
- avgResourcesPerHost = Math.max(avgResourcesPerHost, 1); |
+ var busiestHostResourceCount = domainToResourcesMap[hosts[0]].length; |
+ var requestCountAboveThreshold = busiestHostResourceCount - this._minRequestThreshold; |
+ if (requestCountAboveThreshold <= 0) { |
+ callback(null); |
+ return; |
+ } |
- var pctAboveAvg = (requestCountAboveThreshold / avgResourcesPerHost) - 1.0; |
- var minBalanceThreshold = this._minBalanceThreshold; |
- if (pctAboveAvg < minBalanceThreshold) { |
- callback(null); |
- return; |
- } |
+ var avgResourcesPerHost = 0; |
+ for (var i = 0, size = hosts.length; i < size; ++i) |
+ avgResourcesPerHost += domainToResourcesMap[hosts[i]].length; |
- var requestsOnBusiestHost = domainToResourcesMap[hosts[0]]; |
- var entry = result.addChild(WebInspector.UIString("This page makes %d parallelizable requests to %s. Increase download parallelization by distributing the following requests across multiple hostnames.", busiestHostResourceCount, hosts[0]), true); |
- for (var i = 0; i < requestsOnBusiestHost.length; ++i) |
- entry.addURL(requestsOnBusiestHost[i].url); |
+ // Assume optimal parallelization. |
+ avgResourcesPerHost /= optimalHostnameCount; |
+ avgResourcesPerHost = Math.max(avgResourcesPerHost, 1); |
- result.violationCount = requestsOnBusiestHost.length; |
- callback(result); |
- }, |
+ var pctAboveAvg = (requestCountAboveThreshold / avgResourcesPerHost) - 1.0; |
+ var minBalanceThreshold = this._minBalanceThreshold; |
+ if (pctAboveAvg < minBalanceThreshold) { |
+ callback(null); |
+ return; |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ var requestsOnBusiestHost = domainToResourcesMap[hosts[0]]; |
+ var entry = result.addChild( |
+ WebInspector.UIString( |
+ 'This page makes %d parallelizable requests to %s. Increase download parallelization by distributing the following requests across multiple hostnames.', |
+ busiestHostResourceCount, hosts[0]), |
+ true); |
+ for (var i = 0; i < requestsOnBusiestHost.length; ++i) |
+ entry.addURL(requestsOnBusiestHost[i].url); |
+ |
+ result.violationCount = requestsOnBusiestHost.length; |
+ callback(result); |
+ } |
}; |
/** |
- * The reported CSS rule size is incorrect (parsed != original in WebKit), |
- * so use percentages instead, which gives a better approximation. |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.UnusedCssRule = function() |
-{ |
- WebInspector.AuditRule.call(this, "page-unusedcss", WebInspector.UIString("Remove unused CSS rules")); |
-}; |
+WebInspector.AuditRules.UnusedCssRule = class extends WebInspector.AuditRule { |
+ /** |
+ * The reported CSS rule size is incorrect (parsed != original in WebKit), |
+ * so use percentages instead, which gives a better approximation. |
+ */ |
+ constructor() { |
+ super('page-unusedcss', WebInspector.UIString('Remove unused CSS rules')); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var domModel = WebInspector.DOMModel.fromTarget(target); |
+ var cssModel = WebInspector.CSSModel.fromTarget(target); |
+ if (!domModel || !cssModel) { |
+ callback(null); |
+ return; |
+ } |
-WebInspector.AuditRules.UnusedCssRule.prototype = { |
/** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
+ * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleSheets |
*/ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var domModel = WebInspector.DOMModel.fromTarget(target); |
- var cssModel = WebInspector.CSSModel.fromTarget(target); |
- if (!domModel || !cssModel) { |
- callback(null); |
- return; |
+ function evalCallback(styleSheets) { |
+ if (!styleSheets.length) |
+ return callback(null); |
+ |
+ var selectors = []; |
+ var testedSelectors = {}; |
+ for (var i = 0; i < styleSheets.length; ++i) { |
+ var styleSheet = styleSheets[i]; |
+ for (var curRule = 0; curRule < styleSheet.rules.length; ++curRule) { |
+ var selectorText = styleSheet.rules[curRule].selectorText; |
+ if (testedSelectors[selectorText]) |
+ continue; |
+ selectors.push(selectorText); |
+ testedSelectors[selectorText] = 1; |
+ } |
+ } |
+ |
+ var foundSelectors = {}; |
+ |
+ /** |
+ * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleSheets |
+ */ |
+ function selectorsCallback(styleSheets) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
} |
- /** |
- * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleSheets |
- */ |
- function evalCallback(styleSheets) { |
- if (!styleSheets.length) |
- return callback(null); |
- |
- var selectors = []; |
- var testedSelectors = {}; |
- for (var i = 0; i < styleSheets.length; ++i) { |
- var styleSheet = styleSheets[i]; |
- for (var curRule = 0; curRule < styleSheet.rules.length; ++curRule) { |
- var selectorText = styleSheet.rules[curRule].selectorText; |
- if (testedSelectors[selectorText]) |
- continue; |
- selectors.push(selectorText); |
- testedSelectors[selectorText] = 1; |
- } |
- } |
- |
- var foundSelectors = {}; |
- |
- /** |
- * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleSheets |
- */ |
- function selectorsCallback(styleSheets) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- var inlineBlockOrdinal = 0; |
- var totalStylesheetSize = 0; |
- var totalUnusedStylesheetSize = 0; |
- var summary; |
- |
- for (var i = 0; i < styleSheets.length; ++i) { |
- var styleSheet = styleSheets[i]; |
- var unusedRules = []; |
- for (var curRule = 0; curRule < styleSheet.rules.length; ++curRule) { |
- var rule = styleSheet.rules[curRule]; |
- if (!testedSelectors[rule.selectorText] || foundSelectors[rule.selectorText]) |
- continue; |
- unusedRules.push(rule.selectorText); |
- } |
- totalStylesheetSize += styleSheet.rules.length; |
- totalUnusedStylesheetSize += unusedRules.length; |
- |
- if (!unusedRules.length) |
- continue; |
- |
- var resource = WebInspector.resourceForURL(styleSheet.sourceURL); |
- var isInlineBlock = resource && resource.request && resource.request.resourceType() === WebInspector.resourceTypes.Document; |
- var url = !isInlineBlock ? WebInspector.AuditRuleResult.linkifyDisplayName(styleSheet.sourceURL) : WebInspector.UIString("Inline block #%d", ++inlineBlockOrdinal); |
- var pctUnused = Math.round(100 * unusedRules.length / styleSheet.rules.length); |
- if (!summary) |
- summary = result.addChild("", true); |
- var entry = summary.addFormatted("%s: %d% is not used by the current page.", url, pctUnused); |
- |
- for (var j = 0; j < unusedRules.length; ++j) |
- entry.addSnippet(unusedRules[j]); |
- |
- result.violationCount += unusedRules.length; |
- } |
- |
- if (!totalUnusedStylesheetSize) |
- return callback(null); |
- |
- var totalUnusedPercent = Math.round(100 * totalUnusedStylesheetSize / totalStylesheetSize); |
- summary.value = WebInspector.UIString("%s rules (%d%) of CSS not used by the current page.", totalUnusedStylesheetSize, totalUnusedPercent); |
- |
- callback(result); |
- } |
- |
- /** |
- * @param {?function()} boundSelectorsCallback |
- * @param {string} selector |
- * @param {?DOMAgent.NodeId} nodeId |
- */ |
- function queryCallback(boundSelectorsCallback, selector, nodeId) |
- { |
- if (nodeId) |
- foundSelectors[selector] = true; |
- if (boundSelectorsCallback) |
- boundSelectorsCallback(); |
- } |
- |
- /** |
- * @param {!Array.<string>} selectors |
- * @param {!WebInspector.DOMDocument} document |
- */ |
- function documentLoaded(selectors, document) { |
- var pseudoSelectorRegexp = /::?(?:[\w-]+)(?:\(.*?\))?/g; |
- if (!selectors.length) { |
- selectorsCallback([]); |
- return; |
- } |
- for (var i = 0; i < selectors.length; ++i) { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- var effectiveSelector = selectors[i].replace(pseudoSelectorRegexp, ""); |
- domModel.querySelector(document.id, effectiveSelector, queryCallback.bind(null, i === selectors.length - 1 ? selectorsCallback.bind(null, styleSheets) : null, selectors[i])); |
- } |
- } |
- |
- domModel.requestDocument(documentLoaded.bind(null, selectors)); |
+ var inlineBlockOrdinal = 0; |
+ var totalStylesheetSize = 0; |
+ var totalUnusedStylesheetSize = 0; |
+ var summary; |
+ |
+ for (var i = 0; i < styleSheets.length; ++i) { |
+ var styleSheet = styleSheets[i]; |
+ var unusedRules = []; |
+ for (var curRule = 0; curRule < styleSheet.rules.length; ++curRule) { |
+ var rule = styleSheet.rules[curRule]; |
+ if (!testedSelectors[rule.selectorText] || foundSelectors[rule.selectorText]) |
+ continue; |
+ unusedRules.push(rule.selectorText); |
+ } |
+ totalStylesheetSize += styleSheet.rules.length; |
+ totalUnusedStylesheetSize += unusedRules.length; |
+ |
+ if (!unusedRules.length) |
+ continue; |
+ |
+ var resource = WebInspector.resourceForURL(styleSheet.sourceURL); |
+ var isInlineBlock = |
+ resource && resource.request && resource.request.resourceType() === WebInspector.resourceTypes.Document; |
+ var url = !isInlineBlock ? WebInspector.AuditRuleResult.linkifyDisplayName(styleSheet.sourceURL) : |
+ WebInspector.UIString('Inline block #%d', ++inlineBlockOrdinal); |
+ var pctUnused = Math.round(100 * unusedRules.length / styleSheet.rules.length); |
+ if (!summary) |
+ summary = result.addChild('', true); |
+ var entry = summary.addFormatted('%s: %d% is not used by the current page.', url, pctUnused); |
+ |
+ for (var j = 0; j < unusedRules.length; ++j) |
+ entry.addSnippet(unusedRules[j]); |
+ |
+ result.violationCount += unusedRules.length; |
} |
- var styleSheetInfos = cssModel.allStyleSheets(); |
- if (!styleSheetInfos || !styleSheetInfos.length) { |
- evalCallback([]); |
+ if (!totalUnusedStylesheetSize) |
+ return callback(null); |
+ |
+ var totalUnusedPercent = Math.round(100 * totalUnusedStylesheetSize / totalStylesheetSize); |
+ summary.value = WebInspector.UIString( |
+ '%s rules (%d%) of CSS not used by the current page.', totalUnusedStylesheetSize, totalUnusedPercent); |
+ |
+ callback(result); |
+ } |
+ |
+ /** |
+ * @param {?function()} boundSelectorsCallback |
+ * @param {string} selector |
+ * @param {?DOMAgent.NodeId} nodeId |
+ */ |
+ function queryCallback(boundSelectorsCallback, selector, nodeId) { |
+ if (nodeId) |
+ foundSelectors[selector] = true; |
+ if (boundSelectorsCallback) |
+ boundSelectorsCallback(); |
+ } |
+ |
+ /** |
+ * @param {!Array.<string>} selectors |
+ * @param {!WebInspector.DOMDocument} document |
+ */ |
+ function documentLoaded(selectors, document) { |
+ var pseudoSelectorRegexp = /::?(?:[\w-]+)(?:\(.*?\))?/g; |
+ if (!selectors.length) { |
+ selectorsCallback([]); |
+ return; |
+ } |
+ for (var i = 0; i < selectors.length; ++i) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
return; |
+ } |
+ var effectiveSelector = selectors[i].replace(pseudoSelectorRegexp, ''); |
+ domModel.querySelector( |
+ document.id, effectiveSelector, |
+ queryCallback.bind( |
+ null, i === selectors.length - 1 ? selectorsCallback.bind(null, styleSheets) : null, selectors[i])); |
} |
- var styleSheetProcessor = new WebInspector.AuditRules.StyleSheetProcessor(styleSheetInfos, progress, evalCallback); |
- styleSheetProcessor.run(); |
- }, |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ domModel.requestDocument(documentLoaded.bind(null, selectors)); |
+ } |
+ |
+ var styleSheetInfos = cssModel.allStyleSheets(); |
+ if (!styleSheetInfos || !styleSheetInfos.length) { |
+ evalCallback([]); |
+ return; |
+ } |
+ var styleSheetProcessor = new WebInspector.AuditRules.StyleSheetProcessor(styleSheetInfos, progress, evalCallback); |
+ styleSheetProcessor.run(); |
+ } |
}; |
/** |
@@ -509,1056 +494,992 @@ WebInspector.AuditRules.UnusedCssRule.prototype = { |
WebInspector.AuditRules.ParsedStyleSheet; |
/** |
- * @constructor |
- * @param {!Array.<!WebInspector.CSSStyleSheetHeader>} styleSheetHeaders |
- * @param {!WebInspector.Progress} progress |
- * @param {function(!Array.<!WebInspector.AuditRules.ParsedStyleSheet>)} styleSheetsParsedCallback |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.StyleSheetProcessor = function(styleSheetHeaders, progress, styleSheetsParsedCallback) |
-{ |
+WebInspector.AuditRules.StyleSheetProcessor = class { |
+ /** |
+ * @param {!Array.<!WebInspector.CSSStyleSheetHeader>} styleSheetHeaders |
+ * @param {!WebInspector.Progress} progress |
+ * @param {function(!Array.<!WebInspector.AuditRules.ParsedStyleSheet>)} styleSheetsParsedCallback |
+ */ |
+ constructor(styleSheetHeaders, progress, styleSheetsParsedCallback) { |
this._styleSheetHeaders = styleSheetHeaders; |
this._progress = progress; |
this._styleSheets = []; |
this._styleSheetsParsedCallback = styleSheetsParsedCallback; |
-}; |
+ } |
-WebInspector.AuditRules.StyleSheetProcessor.prototype = { |
- run: function() |
- { |
- this._parser = new WebInspector.CSSParser(); |
- this._processNextStyleSheet(); |
- }, |
- |
- _terminateWorker: function() |
- { |
- if (this._parser) { |
- this._parser.dispose(); |
- delete this._parser; |
- } |
- }, |
- |
- _finish: function() |
- { |
- this._terminateWorker(); |
- this._styleSheetsParsedCallback(this._styleSheets); |
- }, |
- |
- _processNextStyleSheet: function() |
- { |
- if (!this._styleSheetHeaders.length) { |
- this._finish(); |
- return; |
- } |
- this._currentStyleSheetHeader = this._styleSheetHeaders.shift(); |
- this._parser.fetchAndParse(this._currentStyleSheetHeader, this._onStyleSheetParsed.bind(this)); |
- }, |
+ run() { |
+ this._parser = new WebInspector.CSSParser(); |
+ this._processNextStyleSheet(); |
+ } |
- /** |
- * @param {!Array.<!WebInspector.CSSParser.Rule>} rules |
- */ |
- _onStyleSheetParsed: function(rules) |
- { |
- if (this._progress.isCanceled()) { |
- this._finish(); |
- return; |
- } |
+ _terminateWorker() { |
+ if (this._parser) { |
+ this._parser.dispose(); |
+ delete this._parser; |
+ } |
+ } |
- var styleRules = []; |
- for (var i = 0; i < rules.length; ++i) { |
- var rule = rules[i]; |
- if (rule.selectorText) |
- styleRules.push(rule); |
- } |
- this._styleSheets.push({ |
- sourceURL: this._currentStyleSheetHeader.sourceURL, |
- rules: styleRules |
- }); |
- this._processNextStyleSheet(); |
- }, |
+ _finish() { |
+ this._terminateWorker(); |
+ this._styleSheetsParsedCallback(this._styleSheets); |
+ } |
+ |
+ _processNextStyleSheet() { |
+ if (!this._styleSheetHeaders.length) { |
+ this._finish(); |
+ return; |
+ } |
+ this._currentStyleSheetHeader = this._styleSheetHeaders.shift(); |
+ this._parser.fetchAndParse(this._currentStyleSheetHeader, this._onStyleSheetParsed.bind(this)); |
+ } |
+ |
+ /** |
+ * @param {!Array.<!WebInspector.CSSParser.Rule>} rules |
+ */ |
+ _onStyleSheetParsed(rules) { |
+ if (this._progress.isCanceled()) { |
+ this._finish(); |
+ return; |
+ } |
+ |
+ var styleRules = []; |
+ for (var i = 0; i < rules.length; ++i) { |
+ var rule = rules[i]; |
+ if (rule.selectorText) |
+ styleRules.push(rule); |
+ } |
+ this._styleSheets.push({sourceURL: this._currentStyleSheetHeader.sourceURL, rules: styleRules}); |
+ this._processNextStyleSheet(); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CacheControlRule = function(id, name) |
-{ |
- WebInspector.AuditRule.call(this, id, name); |
+WebInspector.AuditRules.CacheControlRule = class extends WebInspector.AuditRule { |
+ constructor(id, name) { |
+ super(id, name); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(!WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var cacheableAndNonCacheableResources = this._cacheableAndNonCacheableResources(requests); |
+ if (cacheableAndNonCacheableResources[0].length) |
+ this.runChecks(cacheableAndNonCacheableResources[0], result); |
+ this.handleNonCacheableResources(cacheableAndNonCacheableResources[1], result); |
+ |
+ callback(result); |
+ } |
+ |
+ handleNonCacheableResources(requests, result) { |
+ } |
+ |
+ _cacheableAndNonCacheableResources(requests) { |
+ var processedResources = [[], []]; |
+ for (var i = 0; i < requests.length; ++i) { |
+ var request = requests[i]; |
+ if (!this.isCacheableResource(request)) |
+ continue; |
+ if (this._isExplicitlyNonCacheable(request)) |
+ processedResources[1].push(request); |
+ else |
+ processedResources[0].push(request); |
+ } |
+ return processedResources; |
+ } |
+ |
+ execCheck(messageText, requestCheckFunction, requests, result) { |
+ var requestCount = requests.length; |
+ var urls = []; |
+ for (var i = 0; i < requestCount; ++i) { |
+ if (requestCheckFunction.call(this, requests[i])) |
+ urls.push(requests[i].url); |
+ } |
+ if (urls.length) { |
+ var entry = result.addChild(messageText, true); |
+ entry.addURLs(urls); |
+ result.violationCount += urls.length; |
+ } |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @param {number} timeMs |
+ * @return {boolean} |
+ */ |
+ freshnessLifetimeGreaterThan(request, timeMs) { |
+ var dateHeader = this.responseHeader(request, 'Date'); |
+ if (!dateHeader) |
+ return false; |
+ |
+ var dateHeaderMs = Date.parse(dateHeader); |
+ if (isNaN(dateHeaderMs)) |
+ return false; |
+ |
+ var freshnessLifetimeMs; |
+ var maxAgeMatch = this.responseHeaderMatch(request, 'Cache-Control', 'max-age=(\\d+)'); |
+ |
+ if (maxAgeMatch) |
+ freshnessLifetimeMs = (maxAgeMatch[1]) ? 1000 * maxAgeMatch[1] : 0; |
+ else { |
+ var expiresHeader = this.responseHeader(request, 'Expires'); |
+ if (expiresHeader) { |
+ var expDate = Date.parse(expiresHeader); |
+ if (!isNaN(expDate)) |
+ freshnessLifetimeMs = expDate - dateHeaderMs; |
+ } |
+ } |
+ |
+ return (isNaN(freshnessLifetimeMs)) ? false : freshnessLifetimeMs > timeMs; |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @param {string} header |
+ * @return {string|undefined} |
+ */ |
+ responseHeader(request, header) { |
+ return request.responseHeaderValue(header); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @param {string} header |
+ * @return {boolean} |
+ */ |
+ hasResponseHeader(request, header) { |
+ return request.responseHeaderValue(header) !== undefined; |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @return {boolean} |
+ */ |
+ isCompressible(request) { |
+ return request.resourceType().isTextType(); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @return {boolean} |
+ */ |
+ isPubliclyCacheable(request) { |
+ if (this._isExplicitlyNonCacheable(request)) |
+ return false; |
+ |
+ if (this.responseHeaderMatch(request, 'Cache-Control', 'public')) |
+ return true; |
+ |
+ return request.url.indexOf('?') === -1 && !this.responseHeaderMatch(request, 'Cache-Control', 'private'); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @param {string} header |
+ * @param {string} regexp |
+ * @return {?Array.<string>} |
+ */ |
+ responseHeaderMatch(request, header, regexp) { |
+ return request.responseHeaderValue(header) ? request.responseHeaderValue(header).match(new RegExp(regexp, 'im')) : |
+ null; |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @return {boolean} |
+ */ |
+ hasExplicitExpiration(request) { |
+ return this.hasResponseHeader(request, 'Date') && |
+ (this.hasResponseHeader(request, 'Expires') || !!this.responseHeaderMatch(request, 'Cache-Control', 'max-age')); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @return {boolean} |
+ */ |
+ _isExplicitlyNonCacheable(request) { |
+ var hasExplicitExp = this.hasExplicitExpiration(request); |
+ return !!this.responseHeaderMatch(request, 'Cache-Control', '(no-cache|no-store)') || |
+ !!this.responseHeaderMatch(request, 'Pragma', 'no-cache') || |
+ (hasExplicitExp && !this.freshnessLifetimeGreaterThan(request, 0)) || |
+ (!hasExplicitExp && !!request.url && request.url.indexOf('?') >= 0) || |
+ (!hasExplicitExp && !this.isCacheableResource(request)); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.NetworkRequest} request |
+ * @return {boolean} |
+ */ |
+ isCacheableResource(request) { |
+ return request.statusCode !== undefined && WebInspector.AuditRules.CacheableResponseCodes[request.statusCode]; |
+ } |
}; |
WebInspector.AuditRules.CacheControlRule.MillisPerMonth = 1000 * 60 * 60 * 24 * 30; |
-WebInspector.AuditRules.CacheControlRule.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(!WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var cacheableAndNonCacheableResources = this._cacheableAndNonCacheableResources(requests); |
- if (cacheableAndNonCacheableResources[0].length) |
- this.runChecks(cacheableAndNonCacheableResources[0], result); |
- this.handleNonCacheableResources(cacheableAndNonCacheableResources[1], result); |
- |
- callback(result); |
- }, |
- |
- handleNonCacheableResources: function(requests, result) |
- { |
- }, |
- |
- _cacheableAndNonCacheableResources: function(requests) |
- { |
- var processedResources = [[], []]; |
- for (var i = 0; i < requests.length; ++i) { |
- var request = requests[i]; |
- if (!this.isCacheableResource(request)) |
- continue; |
- if (this._isExplicitlyNonCacheable(request)) |
- processedResources[1].push(request); |
- else |
- processedResources[0].push(request); |
- } |
- return processedResources; |
- }, |
- |
- execCheck: function(messageText, requestCheckFunction, requests, result) |
- { |
- var requestCount = requests.length; |
- var urls = []; |
- for (var i = 0; i < requestCount; ++i) { |
- if (requestCheckFunction.call(this, requests[i])) |
- urls.push(requests[i].url); |
- } |
- if (urls.length) { |
- var entry = result.addChild(messageText, true); |
- entry.addURLs(urls); |
- result.violationCount += urls.length; |
- } |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @param {number} timeMs |
- * @return {boolean} |
- */ |
- freshnessLifetimeGreaterThan: function(request, timeMs) |
- { |
- var dateHeader = this.responseHeader(request, "Date"); |
- if (!dateHeader) |
- return false; |
- |
- var dateHeaderMs = Date.parse(dateHeader); |
- if (isNaN(dateHeaderMs)) |
- return false; |
- |
- var freshnessLifetimeMs; |
- var maxAgeMatch = this.responseHeaderMatch(request, "Cache-Control", "max-age=(\\d+)"); |
- |
- if (maxAgeMatch) |
- freshnessLifetimeMs = (maxAgeMatch[1]) ? 1000 * maxAgeMatch[1] : 0; |
- else { |
- var expiresHeader = this.responseHeader(request, "Expires"); |
- if (expiresHeader) { |
- var expDate = Date.parse(expiresHeader); |
- if (!isNaN(expDate)) |
- freshnessLifetimeMs = expDate - dateHeaderMs; |
- } |
- } |
- |
- return (isNaN(freshnessLifetimeMs)) ? false : freshnessLifetimeMs > timeMs; |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @param {string} header |
- * @return {string|undefined} |
- */ |
- responseHeader: function(request, header) |
- { |
- return request.responseHeaderValue(header); |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @param {string} header |
- * @return {boolean} |
- */ |
- hasResponseHeader: function(request, header) |
- { |
- return request.responseHeaderValue(header) !== undefined; |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @return {boolean} |
- */ |
- isCompressible: function(request) |
- { |
- return request.resourceType().isTextType(); |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @return {boolean} |
- */ |
- isPubliclyCacheable: function(request) |
- { |
- if (this._isExplicitlyNonCacheable(request)) |
- return false; |
- |
- if (this.responseHeaderMatch(request, "Cache-Control", "public")) |
- return true; |
- |
- return request.url.indexOf("?") === -1 && !this.responseHeaderMatch(request, "Cache-Control", "private"); |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @param {string} header |
- * @param {string} regexp |
- * @return {?Array.<string>} |
- */ |
- responseHeaderMatch: function(request, header, regexp) |
- { |
- return request.responseHeaderValue(header) |
- ? request.responseHeaderValue(header).match(new RegExp(regexp, "im")) |
- : null; |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @return {boolean} |
- */ |
- hasExplicitExpiration: function(request) |
- { |
- return this.hasResponseHeader(request, "Date") && |
- (this.hasResponseHeader(request, "Expires") || !!this.responseHeaderMatch(request, "Cache-Control", "max-age")); |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @return {boolean} |
- */ |
- _isExplicitlyNonCacheable: function(request) |
- { |
- var hasExplicitExp = this.hasExplicitExpiration(request); |
- return !!this.responseHeaderMatch(request, "Cache-Control", "(no-cache|no-store)") || |
- !!this.responseHeaderMatch(request, "Pragma", "no-cache") || |
- (hasExplicitExp && !this.freshnessLifetimeGreaterThan(request, 0)) || |
- (!hasExplicitExp && !!request.url && request.url.indexOf("?") >= 0) || |
- (!hasExplicitExp && !this.isCacheableResource(request)); |
- }, |
- |
- /** |
- * @param {!WebInspector.NetworkRequest} request |
- * @return {boolean} |
- */ |
- isCacheableResource: function(request) |
- { |
- return request.statusCode !== undefined && WebInspector.AuditRules.CacheableResponseCodes[request.statusCode]; |
- }, |
- |
- __proto__: WebInspector.AuditRule.prototype |
-}; |
- |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRules.CacheControlRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.BrowserCacheControlRule = function() |
-{ |
- WebInspector.AuditRules.CacheControlRule.call(this, "http-browsercache", WebInspector.UIString("Leverage browser caching")); |
-}; |
- |
-WebInspector.AuditRules.BrowserCacheControlRule.prototype = { |
- handleNonCacheableResources: function(requests, result) |
- { |
- if (requests.length) { |
- var entry = result.addChild(WebInspector.UIString("The following resources are explicitly non-cacheable. Consider making them cacheable if possible:"), true); |
- result.violationCount += requests.length; |
- for (var i = 0; i < requests.length; ++i) |
- entry.addURL(requests[i].url); |
- } |
- }, |
- |
- runChecks: function(requests, result, callback) |
- { |
- this.execCheck(WebInspector.UIString("The following resources are missing a cache expiration. Resources that do not specify an expiration may not be cached by browsers:"), |
- this._missingExpirationCheck, requests, result); |
- this.execCheck(WebInspector.UIString("The following resources specify a \"Vary\" header that disables caching in most versions of Internet Explorer:"), |
- this._varyCheck, requests, result); |
- this.execCheck(WebInspector.UIString("The following cacheable resources have a short freshness lifetime:"), |
- this._oneMonthExpirationCheck, requests, result); |
- |
- // Unable to implement the favicon check due to the WebKit limitations. |
- this.execCheck(WebInspector.UIString("To further improve cache hit rate, specify an expiration one year in the future for the following cacheable resources:"), |
- this._oneYearExpirationCheck, requests, result); |
- }, |
- |
- _missingExpirationCheck: function(request) |
- { |
- return this.isCacheableResource(request) && !this.hasResponseHeader(request, "Set-Cookie") && !this.hasExplicitExpiration(request); |
- }, |
- |
- _varyCheck: function(request) |
- { |
- var varyHeader = this.responseHeader(request, "Vary"); |
- if (varyHeader) { |
- varyHeader = varyHeader.replace(/User-Agent/gi, ""); |
- varyHeader = varyHeader.replace(/Accept-Encoding/gi, ""); |
- varyHeader = varyHeader.replace(/[, ]*/g, ""); |
- } |
- return varyHeader && varyHeader.length && this.isCacheableResource(request) && this.freshnessLifetimeGreaterThan(request, 0); |
- }, |
- |
- _oneMonthExpirationCheck: function(request) |
- { |
- return this.isCacheableResource(request) && |
- !this.hasResponseHeader(request, "Set-Cookie") && |
- !this.freshnessLifetimeGreaterThan(request, WebInspector.AuditRules.CacheControlRule.MillisPerMonth) && |
- this.freshnessLifetimeGreaterThan(request, 0); |
- }, |
- |
- _oneYearExpirationCheck: function(request) |
- { |
- return this.isCacheableResource(request) && |
- !this.hasResponseHeader(request, "Set-Cookie") && |
- !this.freshnessLifetimeGreaterThan(request, 11 * WebInspector.AuditRules.CacheControlRule.MillisPerMonth) && |
- this.freshnessLifetimeGreaterThan(request, WebInspector.AuditRules.CacheControlRule.MillisPerMonth); |
- }, |
- |
- __proto__: WebInspector.AuditRules.CacheControlRule.prototype |
+WebInspector.AuditRules.BrowserCacheControlRule = class extends WebInspector.AuditRules.CacheControlRule { |
+ constructor() { |
+ super('http-browsercache', WebInspector.UIString('Leverage browser caching')); |
+ } |
+ |
+ /** |
+ * @override |
+ */ |
+ handleNonCacheableResources(requests, result) { |
+ if (requests.length) { |
+ var entry = result.addChild( |
+ WebInspector.UIString( |
+ 'The following resources are explicitly non-cacheable. Consider making them cacheable if possible:'), |
+ true); |
+ result.violationCount += requests.length; |
+ for (var i = 0; i < requests.length; ++i) |
+ entry.addURL(requests[i].url); |
+ } |
+ } |
+ |
+ runChecks(requests, result, callback) { |
+ this.execCheck( |
+ WebInspector.UIString( |
+ 'The following resources are missing a cache expiration. Resources that do not specify an expiration may not be cached by browsers:'), |
+ this._missingExpirationCheck, requests, result); |
+ this.execCheck( |
+ WebInspector.UIString( |
+ 'The following resources specify a "Vary" header that disables caching in most versions of Internet Explorer:'), |
+ this._varyCheck, requests, result); |
+ this.execCheck( |
+ WebInspector.UIString('The following cacheable resources have a short freshness lifetime:'), |
+ this._oneMonthExpirationCheck, requests, result); |
+ |
+ // Unable to implement the favicon check due to the WebKit limitations. |
+ this.execCheck( |
+ WebInspector.UIString( |
+ 'To further improve cache hit rate, specify an expiration one year in the future for the following cacheable resources:'), |
+ this._oneYearExpirationCheck, requests, result); |
+ } |
+ |
+ _missingExpirationCheck(request) { |
+ return this.isCacheableResource(request) && !this.hasResponseHeader(request, 'Set-Cookie') && |
+ !this.hasExplicitExpiration(request); |
+ } |
+ |
+ _varyCheck(request) { |
+ var varyHeader = this.responseHeader(request, 'Vary'); |
+ if (varyHeader) { |
+ varyHeader = varyHeader.replace(/User-Agent/gi, ''); |
+ varyHeader = varyHeader.replace(/Accept-Encoding/gi, ''); |
+ varyHeader = varyHeader.replace(/[, ]*/g, ''); |
+ } |
+ return varyHeader && varyHeader.length && this.isCacheableResource(request) && |
+ this.freshnessLifetimeGreaterThan(request, 0); |
+ } |
+ |
+ _oneMonthExpirationCheck(request) { |
+ return this.isCacheableResource(request) && !this.hasResponseHeader(request, 'Set-Cookie') && |
+ !this.freshnessLifetimeGreaterThan(request, WebInspector.AuditRules.CacheControlRule.MillisPerMonth) && |
+ this.freshnessLifetimeGreaterThan(request, 0); |
+ } |
+ |
+ _oneYearExpirationCheck(request) { |
+ return this.isCacheableResource(request) && !this.hasResponseHeader(request, 'Set-Cookie') && |
+ !this.freshnessLifetimeGreaterThan(request, 11 * WebInspector.AuditRules.CacheControlRule.MillisPerMonth) && |
+ this.freshnessLifetimeGreaterThan(request, WebInspector.AuditRules.CacheControlRule.MillisPerMonth); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.ImageDimensionsRule = function() |
-{ |
- WebInspector.AuditRule.call(this, "page-imagedims", WebInspector.UIString("Specify image dimensions")); |
-}; |
- |
-WebInspector.AuditRules.ImageDimensionsRule.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var domModel = WebInspector.DOMModel.fromTarget(target); |
- var cssModel = WebInspector.CSSModel.fromTarget(target); |
- if (!domModel || !cssModel) { |
- callback(null); |
- return; |
- } |
+WebInspector.AuditRules.ImageDimensionsRule = class extends WebInspector.AuditRule { |
+ constructor() { |
+ super('page-imagedims', WebInspector.UIString('Specify image dimensions')); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var domModel = WebInspector.DOMModel.fromTarget(target); |
+ var cssModel = WebInspector.CSSModel.fromTarget(target); |
+ if (!domModel || !cssModel) { |
+ callback(null); |
+ return; |
+ } |
- var urlToNoDimensionCount = {}; |
- |
- function doneCallback() |
- { |
- for (var url in urlToNoDimensionCount) { |
- var entry = entry || result.addChild(WebInspector.UIString("A width and height should be specified for all images in order to speed up page display. The following image(s) are missing a width and/or height:"), true); |
- var format = "%r"; |
- if (urlToNoDimensionCount[url] > 1) |
- format += " (%d uses)"; |
- entry.addFormatted(format, url, urlToNoDimensionCount[url]); |
- result.violationCount++; |
- } |
- callback(entry ? result : null); |
- } |
+ var urlToNoDimensionCount = {}; |
- function imageStylesReady(imageId, styles) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- const node = domModel.nodeForId(imageId); |
- var src = node.getAttribute("src"); |
- if (!src.asParsedURL()) { |
- for (var frameOwnerCandidate = node; frameOwnerCandidate; frameOwnerCandidate = frameOwnerCandidate.parentNode) { |
- if (frameOwnerCandidate.baseURL) { |
- var completeSrc = WebInspector.ParsedURL.completeURL(frameOwnerCandidate.baseURL, src); |
- break; |
- } |
- } |
- } |
- if (completeSrc) |
- src = completeSrc; |
- |
- if (styles.computedStyle.get("position") === "absolute") |
- return; |
- |
- var widthFound = false; |
- var heightFound = false; |
- for (var i = 0; !(widthFound && heightFound) && i < styles.nodeStyles.length; ++i) { |
- var style = styles.nodeStyles[i]; |
- if (style.getPropertyValue("width") !== "") |
- widthFound = true; |
- if (style.getPropertyValue("height") !== "") |
- heightFound = true; |
- } |
- |
- if (!widthFound || !heightFound) { |
- if (src in urlToNoDimensionCount) |
- ++urlToNoDimensionCount[src]; |
- else |
- urlToNoDimensionCount[src] = 1; |
- } |
- } |
+ function doneCallback() { |
+ for (var url in urlToNoDimensionCount) { |
+ var entry = entry || |
+ result.addChild( |
+ WebInspector.UIString( |
+ 'A width and height should be specified for all images in order to speed up page display. The following image(s) are missing a width and/or height:'), |
+ true); |
+ var format = '%r'; |
+ if (urlToNoDimensionCount[url] > 1) |
+ format += ' (%d uses)'; |
+ entry.addFormatted(format, url, urlToNoDimensionCount[url]); |
+ result.violationCount++; |
+ } |
+ callback(entry ? result : null); |
+ } |
- /** |
- * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
- */ |
- function getStyles(nodeIds) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- var targetResult = {}; |
- |
- /** |
- * @param {?WebInspector.CSSMatchedStyles} matchedStyleResult |
- */ |
- function matchedCallback(matchedStyleResult) |
- { |
- if (!matchedStyleResult) |
- return; |
- targetResult.nodeStyles = matchedStyleResult.nodeStyles(); |
- } |
- |
- /** |
- * @param {?Map.<string, string>} computedStyle |
- */ |
- function computedCallback(computedStyle) |
- { |
- targetResult.computedStyle = computedStyle; |
- } |
- |
- if (!nodeIds || !nodeIds.length) |
- doneCallback(); |
- |
- var nodePromises = []; |
- for (var i = 0; nodeIds && i < nodeIds.length; ++i) { |
- var stylePromises = [ |
- cssModel.matchedStylesPromise(nodeIds[i]).then(matchedCallback), |
- cssModel.computedStylePromise(nodeIds[i]).then(computedCallback) |
- ]; |
- var nodePromise = Promise.all(stylePromises).then(imageStylesReady.bind(null, nodeIds[i], targetResult)); |
- nodePromises.push(nodePromise); |
- } |
- Promise.all(nodePromises) |
- .catchException(null) |
- .then(doneCallback); |
+ function imageStylesReady(imageId, styles) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ |
+ const node = domModel.nodeForId(imageId); |
+ var src = node.getAttribute('src'); |
+ if (!src.asParsedURL()) { |
+ for (var frameOwnerCandidate = node; frameOwnerCandidate; |
+ frameOwnerCandidate = frameOwnerCandidate.parentNode) { |
+ if (frameOwnerCandidate.baseURL) { |
+ var completeSrc = WebInspector.ParsedURL.completeURL(frameOwnerCandidate.baseURL, src); |
+ break; |
+ } |
} |
+ } |
+ if (completeSrc) |
+ src = completeSrc; |
+ |
+ if (styles.computedStyle.get('position') === 'absolute') |
+ return; |
+ |
+ var widthFound = false; |
+ var heightFound = false; |
+ for (var i = 0; !(widthFound && heightFound) && i < styles.nodeStyles.length; ++i) { |
+ var style = styles.nodeStyles[i]; |
+ if (style.getPropertyValue('width') !== '') |
+ widthFound = true; |
+ if (style.getPropertyValue('height') !== '') |
+ heightFound = true; |
+ } |
+ |
+ if (!widthFound || !heightFound) { |
+ if (src in urlToNoDimensionCount) |
+ ++urlToNoDimensionCount[src]; |
+ else |
+ urlToNoDimensionCount[src] = 1; |
+ } |
+ } |
- function onDocumentAvailable(root) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- domModel.querySelectorAll(root.id, "img[src]", getStyles); |
- } |
+ /** |
+ * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
+ */ |
+ function getStyles(nodeIds) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ var targetResult = {}; |
+ |
+ /** |
+ * @param {?WebInspector.CSSMatchedStyles} matchedStyleResult |
+ */ |
+ function matchedCallback(matchedStyleResult) { |
+ if (!matchedStyleResult) |
+ return; |
+ targetResult.nodeStyles = matchedStyleResult.nodeStyles(); |
+ } |
+ |
+ /** |
+ * @param {?Map.<string, string>} computedStyle |
+ */ |
+ function computedCallback(computedStyle) { |
+ targetResult.computedStyle = computedStyle; |
+ } |
+ |
+ if (!nodeIds || !nodeIds.length) |
+ doneCallback(); |
+ |
+ var nodePromises = []; |
+ for (var i = 0; nodeIds && i < nodeIds.length; ++i) { |
+ var stylePromises = [ |
+ cssModel.matchedStylesPromise(nodeIds[i]).then(matchedCallback), |
+ cssModel.computedStylePromise(nodeIds[i]).then(computedCallback) |
+ ]; |
+ var nodePromise = Promise.all(stylePromises).then(imageStylesReady.bind(null, nodeIds[i], targetResult)); |
+ nodePromises.push(nodePromise); |
+ } |
+ Promise.all(nodePromises).catchException(null).then(doneCallback); |
+ } |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- domModel.requestDocument(onDocumentAvailable); |
- }, |
+ function onDocumentAvailable(root) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ domModel.querySelectorAll(root.id, 'img[src]', getStyles); |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ domModel.requestDocument(onDocumentAvailable); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CssInHeadRule = function() |
-{ |
- WebInspector.AuditRule.call(this, "page-cssinhead", WebInspector.UIString("Put CSS in the document head")); |
-}; |
- |
-WebInspector.AuditRules.CssInHeadRule.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var domModel = WebInspector.DOMModel.fromTarget(target); |
- if (!domModel) { |
- callback(null); |
- return; |
- } |
+WebInspector.AuditRules.CssInHeadRule = class extends WebInspector.AuditRule { |
+ constructor() { |
+ super('page-cssinhead', WebInspector.UIString('Put CSS in the document head')); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var domModel = WebInspector.DOMModel.fromTarget(target); |
+ if (!domModel) { |
+ callback(null); |
+ return; |
+ } |
- function evalCallback(evalResult) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- if (!evalResult) |
- return callback(null); |
- |
- var summary = result.addChild(""); |
- |
- for (var url in evalResult) { |
- var urlViolations = evalResult[url]; |
- if (urlViolations[0]) { |
- result.addFormatted("%s style block(s) in the %r body should be moved to the document head.", urlViolations[0], url); |
- result.violationCount += urlViolations[0]; |
- } |
- for (var i = 0; i < urlViolations[1].length; ++i) |
- result.addFormatted("Link node %r should be moved to the document head in %r", urlViolations[1][i], url); |
- result.violationCount += urlViolations[1].length; |
- } |
- summary.value = WebInspector.UIString("CSS in the document body adversely impacts rendering performance."); |
- callback(result); |
- } |
+ function evalCallback(evalResult) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
- /** |
- * @param {!WebInspector.DOMNode} root |
- * @param {!Array.<!DOMAgent.NodeId>=} inlineStyleNodeIds |
- * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
- */ |
- function externalStylesheetsReceived(root, inlineStyleNodeIds, nodeIds) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- if (!nodeIds) |
- return; |
- var externalStylesheetNodeIds = nodeIds; |
- var result = null; |
- if (inlineStyleNodeIds.length || externalStylesheetNodeIds.length) { |
- var urlToViolationsArray = {}; |
- var externalStylesheetHrefs = []; |
- for (var j = 0; j < externalStylesheetNodeIds.length; ++j) { |
- var linkNode = domModel.nodeForId(externalStylesheetNodeIds[j]); |
- var completeHref = WebInspector.ParsedURL.completeURL(linkNode.ownerDocument.baseURL, linkNode.getAttribute("href")); |
- externalStylesheetHrefs.push(completeHref || "<empty>"); |
- } |
- urlToViolationsArray[root.documentURL] = [inlineStyleNodeIds.length, externalStylesheetHrefs]; |
- result = urlToViolationsArray; |
- } |
- evalCallback(result); |
- } |
+ if (!evalResult) |
+ return callback(null); |
- /** |
- * @param {!WebInspector.DOMNode} root |
- * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
- */ |
- function inlineStylesReceived(root, nodeIds) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- if (!nodeIds) |
- return; |
- domModel.querySelectorAll(root.id, "body link[rel~='stylesheet'][href]", externalStylesheetsReceived.bind(null, root, nodeIds)); |
- } |
+ var summary = result.addChild(''); |
- /** |
- * @param {!WebInspector.DOMNode} root |
- */ |
- function onDocumentAvailable(root) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- domModel.querySelectorAll(root.id, "body style", inlineStylesReceived.bind(null, root)); |
+ for (var url in evalResult) { |
+ var urlViolations = evalResult[url]; |
+ if (urlViolations[0]) { |
+ result.addFormatted( |
+ '%s style block(s) in the %r body should be moved to the document head.', urlViolations[0], url); |
+ result.violationCount += urlViolations[0]; |
} |
+ for (var i = 0; i < urlViolations[1].length; ++i) |
+ result.addFormatted('Link node %r should be moved to the document head in %r', urlViolations[1][i], url); |
+ result.violationCount += urlViolations[1].length; |
+ } |
+ summary.value = WebInspector.UIString('CSS in the document body adversely impacts rendering performance.'); |
+ callback(result); |
+ } |
- domModel.requestDocument(onDocumentAvailable); |
- }, |
- |
- __proto__: WebInspector.AuditRule.prototype |
-}; |
- |
-/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
- */ |
-WebInspector.AuditRules.StylesScriptsOrderRule = function() |
-{ |
- WebInspector.AuditRule.call(this, "page-stylescriptorder", WebInspector.UIString("Optimize the order of styles and scripts")); |
-}; |
- |
-WebInspector.AuditRules.StylesScriptsOrderRule.prototype = { |
/** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
+ * @param {!WebInspector.DOMNode} root |
+ * @param {!Array.<!DOMAgent.NodeId>=} inlineStyleNodeIds |
+ * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
*/ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var domModel = WebInspector.DOMModel.fromTarget(target); |
- if (!domModel) { |
- callback(null); |
- return; |
- } |
- |
- function evalCallback(resultValue) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- if (!resultValue) |
- return callback(null); |
- |
- var lateCssUrls = resultValue[0]; |
- var cssBeforeInlineCount = resultValue[1]; |
- |
- if (lateCssUrls.length) { |
- var entry = result.addChild(WebInspector.UIString("The following external CSS files were included after an external JavaScript file in the document head. To ensure CSS files are downloaded in parallel, always include external CSS before external JavaScript."), true); |
- entry.addURLs(lateCssUrls); |
- result.violationCount += lateCssUrls.length; |
- } |
- |
- if (cssBeforeInlineCount) { |
- result.addChild(WebInspector.UIString(" %d inline script block%s found in the head between an external CSS file and another resource. To allow parallel downloading, move the inline script before the external CSS file, or after the next resource.", cssBeforeInlineCount, cssBeforeInlineCount > 1 ? "s were" : " was")); |
- result.violationCount += cssBeforeInlineCount; |
- } |
- callback(result); |
- } |
- |
- /** |
- * @param {!Array.<!DOMAgent.NodeId>} lateStyleIds |
- * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
- */ |
- function cssBeforeInlineReceived(lateStyleIds, nodeIds) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- if (!nodeIds) |
- return; |
- |
- var cssBeforeInlineCount = nodeIds.length; |
- var result = null; |
- if (lateStyleIds.length || cssBeforeInlineCount) { |
- var lateStyleUrls = []; |
- for (var i = 0; i < lateStyleIds.length; ++i) { |
- var lateStyleNode = domModel.nodeForId(lateStyleIds[i]); |
- var completeHref = WebInspector.ParsedURL.completeURL(lateStyleNode.ownerDocument.baseURL, lateStyleNode.getAttribute("href")); |
- lateStyleUrls.push(completeHref || "<empty>"); |
- } |
- result = [ lateStyleUrls, cssBeforeInlineCount ]; |
- } |
- |
- evalCallback(result); |
+ function externalStylesheetsReceived(root, inlineStyleNodeIds, nodeIds) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ |
+ if (!nodeIds) |
+ return; |
+ var externalStylesheetNodeIds = nodeIds; |
+ var result = null; |
+ if (inlineStyleNodeIds.length || externalStylesheetNodeIds.length) { |
+ var urlToViolationsArray = {}; |
+ var externalStylesheetHrefs = []; |
+ for (var j = 0; j < externalStylesheetNodeIds.length; ++j) { |
+ var linkNode = domModel.nodeForId(externalStylesheetNodeIds[j]); |
+ var completeHref = |
+ WebInspector.ParsedURL.completeURL(linkNode.ownerDocument.baseURL, linkNode.getAttribute('href')); |
+ externalStylesheetHrefs.push(completeHref || '<empty>'); |
} |
+ urlToViolationsArray[root.documentURL] = [inlineStyleNodeIds.length, externalStylesheetHrefs]; |
+ result = urlToViolationsArray; |
+ } |
+ evalCallback(result); |
+ } |
- /** |
- * @param {!WebInspector.DOMDocument} root |
- * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
- */ |
- function lateStylesReceived(root, nodeIds) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- if (!nodeIds) |
- return; |
- |
- domModel.querySelectorAll(root.id, "head link[rel~='stylesheet'][href] ~ script:not([src])", cssBeforeInlineReceived.bind(null, nodeIds)); |
- } |
+ /** |
+ * @param {!WebInspector.DOMNode} root |
+ * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
+ */ |
+ function inlineStylesReceived(root, nodeIds) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ |
+ if (!nodeIds) |
+ return; |
+ domModel.querySelectorAll( |
+ root.id, 'body link[rel~=\'stylesheet\'][href]', externalStylesheetsReceived.bind(null, root, nodeIds)); |
+ } |
- /** |
- * @param {!WebInspector.DOMDocument} root |
- */ |
- function onDocumentAvailable(root) |
- { |
- if (progress.isCanceled()) { |
- callback(null); |
- return; |
- } |
- |
- domModel.querySelectorAll(root.id, "head script[src] ~ link[rel~='stylesheet'][href]", lateStylesReceived.bind(null, root)); |
- } |
+ /** |
+ * @param {!WebInspector.DOMNode} root |
+ */ |
+ function onDocumentAvailable(root) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
- domModel.requestDocument(onDocumentAvailable); |
- }, |
+ domModel.querySelectorAll(root.id, 'body style', inlineStylesReceived.bind(null, root)); |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ domModel.requestDocument(onDocumentAvailable); |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CSSRuleBase = function(id, name) |
-{ |
- WebInspector.AuditRule.call(this, id, name); |
-}; |
+WebInspector.AuditRules.StylesScriptsOrderRule = class extends WebInspector.AuditRule { |
+ constructor() { |
+ super('page-stylescriptorder', WebInspector.UIString('Optimize the order of styles and scripts')); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var domModel = WebInspector.DOMModel.fromTarget(target); |
+ if (!domModel) { |
+ callback(null); |
+ return; |
+ } |
-WebInspector.AuditRules.CSSRuleBase.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(?WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var cssModel = WebInspector.CSSModel.fromTarget(target); |
- if (!cssModel) { |
- callback(null); |
- return; |
- } |
+ function evalCallback(resultValue) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
- var headers = cssModel.allStyleSheets(); |
- if (!headers.length) { |
- callback(null); |
- return; |
- } |
- var activeHeaders = []; |
- for (var i = 0; i < headers.length; ++i) { |
- if (!headers[i].disabled) |
- activeHeaders.push(headers[i]); |
- } |
+ if (!resultValue) |
+ return callback(null); |
- var styleSheetProcessor = new WebInspector.AuditRules.StyleSheetProcessor(activeHeaders, progress, this._styleSheetsLoaded.bind(this, result, callback, progress)); |
- styleSheetProcessor.run(); |
- }, |
+ var lateCssUrls = resultValue[0]; |
+ var cssBeforeInlineCount = resultValue[1]; |
- /** |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(!WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleSheets |
- */ |
- _styleSheetsLoaded: function(result, callback, progress, styleSheets) |
- { |
- for (var i = 0; i < styleSheets.length; ++i) |
- this._visitStyleSheet(styleSheets[i], result); |
- callback(result); |
- }, |
+ if (lateCssUrls.length) { |
+ var entry = result.addChild( |
+ WebInspector.UIString( |
+ 'The following external CSS files were included after an external JavaScript file in the document head. To ensure CSS files are downloaded in parallel, always include external CSS before external JavaScript.'), |
+ true); |
+ entry.addURLs(lateCssUrls); |
+ result.violationCount += lateCssUrls.length; |
+ } |
+ |
+ if (cssBeforeInlineCount) { |
+ result.addChild(WebInspector.UIString( |
+ ' %d inline script block%s found in the head between an external CSS file and another resource. To allow parallel downloading, move the inline script before the external CSS file, or after the next resource.', |
+ cssBeforeInlineCount, cssBeforeInlineCount > 1 ? 's were' : ' was')); |
+ result.violationCount += cssBeforeInlineCount; |
+ } |
+ callback(result); |
+ } |
/** |
- * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
- * @param {!WebInspector.AuditRuleResult} result |
+ * @param {!Array.<!DOMAgent.NodeId>} lateStyleIds |
+ * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
*/ |
- _visitStyleSheet: function(styleSheet, result) |
- { |
- this.visitStyleSheet(styleSheet, result); |
- |
- for (var i = 0; i < styleSheet.rules.length; ++i) |
- this._visitRule(styleSheet, styleSheet.rules[i], result); |
- |
- this.didVisitStyleSheet(styleSheet, result); |
- }, |
+ function cssBeforeInlineReceived(lateStyleIds, nodeIds) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ |
+ if (!nodeIds) |
+ return; |
+ |
+ var cssBeforeInlineCount = nodeIds.length; |
+ var result = null; |
+ if (lateStyleIds.length || cssBeforeInlineCount) { |
+ var lateStyleUrls = []; |
+ for (var i = 0; i < lateStyleIds.length; ++i) { |
+ var lateStyleNode = domModel.nodeForId(lateStyleIds[i]); |
+ var completeHref = WebInspector.ParsedURL.completeURL( |
+ lateStyleNode.ownerDocument.baseURL, lateStyleNode.getAttribute('href')); |
+ lateStyleUrls.push(completeHref || '<empty>'); |
+ } |
+ result = [lateStyleUrls, cssBeforeInlineCount]; |
+ } |
- /** |
- * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
- * @param {!WebInspector.CSSParser.StyleRule} rule |
- * @param {!WebInspector.AuditRuleResult} result |
- */ |
- _visitRule: function(styleSheet, rule, result) |
- { |
- this.visitRule(styleSheet, rule, result); |
- var allProperties = rule.properties; |
- for (var i = 0; i < allProperties.length; ++i) |
- this.visitProperty(styleSheet, rule, allProperties[i], result); |
- this.didVisitRule(styleSheet, rule, result); |
- }, |
+ evalCallback(result); |
+ } |
/** |
- * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
- * @param {!WebInspector.AuditRuleResult} result |
+ * @param {!WebInspector.DOMDocument} root |
+ * @param {!Array.<!DOMAgent.NodeId>=} nodeIds |
*/ |
- visitStyleSheet: function(styleSheet, result) |
- { |
- // Subclasses can implement. |
- }, |
+ function lateStylesReceived(root, nodeIds) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ |
+ if (!nodeIds) |
+ return; |
+ |
+ domModel.querySelectorAll( |
+ root.id, 'head link[rel~=\'stylesheet\'][href] ~ script:not([src])', |
+ cssBeforeInlineReceived.bind(null, nodeIds)); |
+ } |
/** |
- * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
- * @param {!WebInspector.AuditRuleResult} result |
+ * @param {!WebInspector.DOMDocument} root |
*/ |
- didVisitStyleSheet: function(styleSheet, result) |
- { |
- // Subclasses can implement. |
- }, |
+ function onDocumentAvailable(root) { |
+ if (progress.isCanceled()) { |
+ callback(null); |
+ return; |
+ } |
+ |
+ domModel.querySelectorAll( |
+ root.id, 'head script[src] ~ link[rel~=\'stylesheet\'][href]', lateStylesReceived.bind(null, root)); |
+ } |
- /** |
- * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
- * @param {!WebInspector.CSSParser.StyleRule} rule |
- * @param {!WebInspector.AuditRuleResult} result |
- */ |
- visitRule: function(styleSheet, rule, result) |
- { |
- // Subclasses can implement. |
- }, |
+ domModel.requestDocument(onDocumentAvailable); |
+ } |
+}; |
- /** |
- * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
- * @param {!WebInspector.CSSParser.StyleRule} rule |
- * @param {!WebInspector.AuditRuleResult} result |
- */ |
- didVisitRule: function(styleSheet, rule, result) |
- { |
- // Subclasses can implement. |
- }, |
+/** |
+ * @unrestricted |
+ */ |
+WebInspector.AuditRules.CSSRuleBase = class extends WebInspector.AuditRule { |
+ constructor(id, name) { |
+ super(id, name); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(?WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var cssModel = WebInspector.CSSModel.fromTarget(target); |
+ if (!cssModel) { |
+ callback(null); |
+ return; |
+ } |
- /** |
- * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
- * @param {!WebInspector.CSSParser.StyleRule} rule |
- * @param {!WebInspector.CSSParser.Property} property |
- * @param {!WebInspector.AuditRuleResult} result |
- */ |
- visitProperty: function(styleSheet, rule, property, result) |
- { |
- // Subclasses can implement. |
- }, |
+ var headers = cssModel.allStyleSheets(); |
+ if (!headers.length) { |
+ callback(null); |
+ return; |
+ } |
+ var activeHeaders = []; |
+ for (var i = 0; i < headers.length; ++i) { |
+ if (!headers[i].disabled) |
+ activeHeaders.push(headers[i]); |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ var styleSheetProcessor = new WebInspector.AuditRules.StyleSheetProcessor( |
+ activeHeaders, progress, this._styleSheetsLoaded.bind(this, result, callback, progress)); |
+ styleSheetProcessor.run(); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(!WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ * @param {!Array.<!WebInspector.AuditRules.ParsedStyleSheet>} styleSheets |
+ */ |
+ _styleSheetsLoaded(result, callback, progress, styleSheets) { |
+ for (var i = 0; i < styleSheets.length; ++i) |
+ this._visitStyleSheet(styleSheets[i], result); |
+ callback(result); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
+ * @param {!WebInspector.AuditRuleResult} result |
+ */ |
+ _visitStyleSheet(styleSheet, result) { |
+ this.visitStyleSheet(styleSheet, result); |
+ |
+ for (var i = 0; i < styleSheet.rules.length; ++i) |
+ this._visitRule(styleSheet, styleSheet.rules[i], result); |
+ |
+ this.didVisitStyleSheet(styleSheet, result); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
+ * @param {!WebInspector.CSSParser.StyleRule} rule |
+ * @param {!WebInspector.AuditRuleResult} result |
+ */ |
+ _visitRule(styleSheet, rule, result) { |
+ this.visitRule(styleSheet, rule, result); |
+ var allProperties = rule.properties; |
+ for (var i = 0; i < allProperties.length; ++i) |
+ this.visitProperty(styleSheet, rule, allProperties[i], result); |
+ this.didVisitRule(styleSheet, rule, result); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
+ * @param {!WebInspector.AuditRuleResult} result |
+ */ |
+ visitStyleSheet(styleSheet, result) { |
+ // Subclasses can implement. |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
+ * @param {!WebInspector.AuditRuleResult} result |
+ */ |
+ didVisitStyleSheet(styleSheet, result) { |
+ // Subclasses can implement. |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
+ * @param {!WebInspector.CSSParser.StyleRule} rule |
+ * @param {!WebInspector.AuditRuleResult} result |
+ */ |
+ visitRule(styleSheet, rule, result) { |
+ // Subclasses can implement. |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
+ * @param {!WebInspector.CSSParser.StyleRule} rule |
+ * @param {!WebInspector.AuditRuleResult} result |
+ */ |
+ didVisitRule(styleSheet, rule, result) { |
+ // Subclasses can implement. |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.AuditRules.ParsedStyleSheet} styleSheet |
+ * @param {!WebInspector.CSSParser.StyleRule} rule |
+ * @param {!WebInspector.CSSParser.Property} property |
+ * @param {!WebInspector.AuditRuleResult} result |
+ */ |
+ visitProperty(styleSheet, rule, property, result) { |
+ // Subclasses can implement. |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRule} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CookieRuleBase = function(id, name) |
-{ |
- WebInspector.AuditRule.call(this, id, name); |
-}; |
- |
-WebInspector.AuditRules.CookieRuleBase.prototype = { |
- /** |
- * @override |
- * @param {!WebInspector.Target} target |
- * @param {!Array.<!WebInspector.NetworkRequest>} requests |
- * @param {!WebInspector.AuditRuleResult} result |
- * @param {function(!WebInspector.AuditRuleResult)} callback |
- * @param {!WebInspector.Progress} progress |
- */ |
- doRun: function(target, requests, result, callback, progress) |
- { |
- var self = this; |
- function resultCallback(receivedCookies) |
- { |
- if (progress.isCanceled()) { |
- callback(result); |
- return; |
- } |
- |
- self.processCookies(receivedCookies, requests, result); |
- callback(result); |
- } |
- |
- WebInspector.Cookies.getCookiesAsync(resultCallback); |
- }, |
+WebInspector.AuditRules.CookieRuleBase = class extends WebInspector.AuditRule { |
+ constructor(id, name) { |
+ super(id, name); |
+ } |
+ |
+ /** |
+ * @override |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest>} requests |
+ * @param {!WebInspector.AuditRuleResult} result |
+ * @param {function(!WebInspector.AuditRuleResult)} callback |
+ * @param {!WebInspector.Progress} progress |
+ */ |
+ doRun(target, requests, result, callback, progress) { |
+ var self = this; |
+ function resultCallback(receivedCookies) { |
+ if (progress.isCanceled()) { |
+ callback(result); |
+ return; |
+ } |
- mapResourceCookies: function(requestsByDomain, allCookies, callback) |
- { |
- for (var i = 0; i < allCookies.length; ++i) { |
- for (var requestDomain in requestsByDomain) { |
- if (WebInspector.Cookies.cookieDomainMatchesResourceDomain(allCookies[i].domain(), requestDomain)) |
- this._callbackForResourceCookiePairs(requestsByDomain[requestDomain], allCookies[i], callback); |
- } |
- } |
- }, |
+ self.processCookies(receivedCookies, requests, result); |
+ callback(result); |
+ } |
- _callbackForResourceCookiePairs: function(requests, cookie, callback) |
- { |
- if (!requests) |
- return; |
- for (var i = 0; i < requests.length; ++i) { |
- if (WebInspector.Cookies.cookieMatchesResourceURL(cookie, requests[i].url)) |
- callback(requests[i], cookie); |
- } |
- }, |
+ WebInspector.Cookies.getCookiesAsync(resultCallback); |
+ } |
- __proto__: WebInspector.AuditRule.prototype |
+ mapResourceCookies(requestsByDomain, allCookies, callback) { |
+ for (var i = 0; i < allCookies.length; ++i) { |
+ for (var requestDomain in requestsByDomain) { |
+ if (WebInspector.Cookies.cookieDomainMatchesResourceDomain(allCookies[i].domain(), requestDomain)) |
+ this._callbackForResourceCookiePairs(requestsByDomain[requestDomain], allCookies[i], callback); |
+ } |
+ } |
+ } |
+ |
+ _callbackForResourceCookiePairs(requests, cookie, callback) { |
+ if (!requests) |
+ return; |
+ for (var i = 0; i < requests.length; ++i) { |
+ if (WebInspector.Cookies.cookieMatchesResourceURL(cookie, requests[i].url)) |
+ callback(requests[i], cookie); |
+ } |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRules.CookieRuleBase} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.CookieSizeRule = function(avgBytesThreshold) |
-{ |
- WebInspector.AuditRules.CookieRuleBase.call(this, "http-cookiesize", WebInspector.UIString("Minimize cookie size")); |
+WebInspector.AuditRules.CookieSizeRule = class extends WebInspector.AuditRules.CookieRuleBase { |
+ constructor(avgBytesThreshold) { |
+ super('http-cookiesize', WebInspector.UIString('Minimize cookie size')); |
this._avgBytesThreshold = avgBytesThreshold; |
this._maxBytesThreshold = 1000; |
-}; |
- |
-WebInspector.AuditRules.CookieSizeRule.prototype = { |
- _average: function(cookieArray) |
- { |
- var total = 0; |
- for (var i = 0; i < cookieArray.length; ++i) |
- total += cookieArray[i].size(); |
- return cookieArray.length ? Math.round(total / cookieArray.length) : 0; |
- }, |
- |
- _max: function(cookieArray) |
- { |
- var result = 0; |
- for (var i = 0; i < cookieArray.length; ++i) |
- result = Math.max(cookieArray[i].size(), result); |
- return result; |
- }, |
- |
- processCookies: function(allCookies, requests, result) |
- { |
- function maxSizeSorter(a, b) |
- { |
- return b.maxCookieSize - a.maxCookieSize; |
- } |
- |
- function avgSizeSorter(a, b) |
- { |
- return b.avgCookieSize - a.avgCookieSize; |
- } |
+ } |
+ |
+ _average(cookieArray) { |
+ var total = 0; |
+ for (var i = 0; i < cookieArray.length; ++i) |
+ total += cookieArray[i].size(); |
+ return cookieArray.length ? Math.round(total / cookieArray.length) : 0; |
+ } |
+ |
+ _max(cookieArray) { |
+ var result = 0; |
+ for (var i = 0; i < cookieArray.length; ++i) |
+ result = Math.max(cookieArray[i].size(), result); |
+ return result; |
+ } |
+ |
+ processCookies(allCookies, requests, result) { |
+ function maxSizeSorter(a, b) { |
+ return b.maxCookieSize - a.maxCookieSize; |
+ } |
- var cookiesPerResourceDomain = {}; |
+ function avgSizeSorter(a, b) { |
+ return b.avgCookieSize - a.avgCookieSize; |
+ } |
- function collectorCallback(request, cookie) |
- { |
- var cookies = cookiesPerResourceDomain[request.parsedURL.host]; |
- if (!cookies) { |
- cookies = []; |
- cookiesPerResourceDomain[request.parsedURL.host] = cookies; |
- } |
- cookies.push(cookie); |
- } |
+ var cookiesPerResourceDomain = {}; |
- if (!allCookies.length) |
- return; |
+ function collectorCallback(request, cookie) { |
+ var cookies = cookiesPerResourceDomain[request.parsedURL.host]; |
+ if (!cookies) { |
+ cookies = []; |
+ cookiesPerResourceDomain[request.parsedURL.host] = cookies; |
+ } |
+ cookies.push(cookie); |
+ } |
- var sortedCookieSizes = []; |
+ if (!allCookies.length) |
+ return; |
- var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap(requests, |
- null, |
- true); |
- this.mapResourceCookies(domainToResourcesMap, allCookies, collectorCallback); |
- |
- for (var requestDomain in cookiesPerResourceDomain) { |
- var cookies = cookiesPerResourceDomain[requestDomain]; |
- sortedCookieSizes.push({ |
- domain: requestDomain, |
- avgCookieSize: this._average(cookies), |
- maxCookieSize: this._max(cookies) |
- }); |
- } |
- var avgAllCookiesSize = this._average(allCookies); |
+ var sortedCookieSizes = []; |
- var hugeCookieDomains = []; |
- sortedCookieSizes.sort(maxSizeSorter); |
+ var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap(requests, null, true); |
+ this.mapResourceCookies(domainToResourcesMap, allCookies, collectorCallback); |
- for (var i = 0, len = sortedCookieSizes.length; i < len; ++i) { |
- var maxCookieSize = sortedCookieSizes[i].maxCookieSize; |
- if (maxCookieSize > this._maxBytesThreshold) |
- hugeCookieDomains.push(WebInspector.AuditRuleResult.resourceDomain(sortedCookieSizes[i].domain) + ": " + Number.bytesToString(maxCookieSize)); |
- } |
+ for (var requestDomain in cookiesPerResourceDomain) { |
+ var cookies = cookiesPerResourceDomain[requestDomain]; |
+ sortedCookieSizes.push( |
+ {domain: requestDomain, avgCookieSize: this._average(cookies), maxCookieSize: this._max(cookies)}); |
+ } |
+ var avgAllCookiesSize = this._average(allCookies); |
- var bigAvgCookieDomains = []; |
- sortedCookieSizes.sort(avgSizeSorter); |
- for (var i = 0, len = sortedCookieSizes.length; i < len; ++i) { |
- var domain = sortedCookieSizes[i].domain; |
- var avgCookieSize = sortedCookieSizes[i].avgCookieSize; |
- if (avgCookieSize > this._avgBytesThreshold && avgCookieSize < this._maxBytesThreshold) |
- bigAvgCookieDomains.push(WebInspector.AuditRuleResult.resourceDomain(domain) + ": " + Number.bytesToString(avgCookieSize)); |
- } |
- result.addChild(WebInspector.UIString("The average cookie size for all requests on this page is %s", Number.bytesToString(avgAllCookiesSize))); |
+ var hugeCookieDomains = []; |
+ sortedCookieSizes.sort(maxSizeSorter); |
- if (hugeCookieDomains.length) { |
- var entry = result.addChild(WebInspector.UIString("The following domains have a cookie size in excess of 1KB. This is harmful because requests with cookies larger than 1KB typically cannot fit into a single network packet."), true); |
- entry.addURLs(hugeCookieDomains); |
- result.violationCount += hugeCookieDomains.length; |
- } |
+ for (var i = 0, len = sortedCookieSizes.length; i < len; ++i) { |
+ var maxCookieSize = sortedCookieSizes[i].maxCookieSize; |
+ if (maxCookieSize > this._maxBytesThreshold) |
+ hugeCookieDomains.push( |
+ WebInspector.AuditRuleResult.resourceDomain(sortedCookieSizes[i].domain) + ': ' + |
+ Number.bytesToString(maxCookieSize)); |
+ } |
- if (bigAvgCookieDomains.length) { |
- var entry = result.addChild(WebInspector.UIString("The following domains have an average cookie size in excess of %d bytes. Reducing the size of cookies for these domains can reduce the time it takes to send requests.", this._avgBytesThreshold), true); |
- entry.addURLs(bigAvgCookieDomains); |
- result.violationCount += bigAvgCookieDomains.length; |
- } |
- }, |
+ var bigAvgCookieDomains = []; |
+ sortedCookieSizes.sort(avgSizeSorter); |
+ for (var i = 0, len = sortedCookieSizes.length; i < len; ++i) { |
+ var domain = sortedCookieSizes[i].domain; |
+ var avgCookieSize = sortedCookieSizes[i].avgCookieSize; |
+ if (avgCookieSize > this._avgBytesThreshold && avgCookieSize < this._maxBytesThreshold) |
+ bigAvgCookieDomains.push( |
+ WebInspector.AuditRuleResult.resourceDomain(domain) + ': ' + Number.bytesToString(avgCookieSize)); |
+ } |
+ result.addChild(WebInspector.UIString( |
+ 'The average cookie size for all requests on this page is %s', Number.bytesToString(avgAllCookiesSize))); |
+ |
+ if (hugeCookieDomains.length) { |
+ var entry = result.addChild( |
+ WebInspector.UIString( |
+ 'The following domains have a cookie size in excess of 1KB. This is harmful because requests with cookies larger than 1KB typically cannot fit into a single network packet.'), |
+ true); |
+ entry.addURLs(hugeCookieDomains); |
+ result.violationCount += hugeCookieDomains.length; |
+ } |
- __proto__: WebInspector.AuditRules.CookieRuleBase.prototype |
+ if (bigAvgCookieDomains.length) { |
+ var entry = result.addChild( |
+ WebInspector.UIString( |
+ 'The following domains have an average cookie size in excess of %d bytes. Reducing the size of cookies for these domains can reduce the time it takes to send requests.', |
+ this._avgBytesThreshold), |
+ true); |
+ entry.addURLs(bigAvgCookieDomains); |
+ result.violationCount += bigAvgCookieDomains.length; |
+ } |
+ } |
}; |
/** |
- * @constructor |
- * @extends {WebInspector.AuditRules.CookieRuleBase} |
+ * @unrestricted |
*/ |
-WebInspector.AuditRules.StaticCookielessRule = function(minResources) |
-{ |
- WebInspector.AuditRules.CookieRuleBase.call(this, "http-staticcookieless", WebInspector.UIString("Serve static content from a cookieless domain")); |
+WebInspector.AuditRules.StaticCookielessRule = class extends WebInspector.AuditRules.CookieRuleBase { |
+ constructor(minResources) { |
+ super('http-staticcookieless', WebInspector.UIString('Serve static content from a cookieless domain')); |
this._minResources = minResources; |
-}; |
- |
-WebInspector.AuditRules.StaticCookielessRule.prototype = { |
- processCookies: function(allCookies, requests, result) |
- { |
- var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap(requests, |
- [WebInspector.resourceTypes.Stylesheet, |
- WebInspector.resourceTypes.Image], |
- true); |
- var totalStaticResources = 0; |
- for (var domain in domainToResourcesMap) |
- totalStaticResources += domainToResourcesMap[domain].length; |
- if (totalStaticResources < this._minResources) |
- return; |
- var matchingResourceData = {}; |
- this.mapResourceCookies(domainToResourcesMap, allCookies, this._collectorCallback.bind(this, matchingResourceData)); |
- |
- var badUrls = []; |
- var cookieBytes = 0; |
- for (var url in matchingResourceData) { |
- badUrls.push(url); |
- cookieBytes += matchingResourceData[url]; |
- } |
- if (badUrls.length < this._minResources) |
- return; |
- |
- var entry = result.addChild(WebInspector.UIString("%s of cookies were sent with the following static resources. Serve these static resources from a domain that does not set cookies:", Number.bytesToString(cookieBytes)), true); |
- entry.addURLs(badUrls); |
- result.violationCount = badUrls.length; |
- }, |
- |
- _collectorCallback: function(matchingResourceData, request, cookie) |
- { |
- matchingResourceData[request.url] = (matchingResourceData[request.url] || 0) + cookie.size(); |
- }, |
- |
- __proto__: WebInspector.AuditRules.CookieRuleBase.prototype |
+ } |
+ |
+ processCookies(allCookies, requests, result) { |
+ var domainToResourcesMap = WebInspector.AuditRules.getDomainToResourcesMap( |
+ requests, [WebInspector.resourceTypes.Stylesheet, WebInspector.resourceTypes.Image], true); |
+ var totalStaticResources = 0; |
+ for (var domain in domainToResourcesMap) |
+ totalStaticResources += domainToResourcesMap[domain].length; |
+ if (totalStaticResources < this._minResources) |
+ return; |
+ var matchingResourceData = {}; |
+ this.mapResourceCookies(domainToResourcesMap, allCookies, this._collectorCallback.bind(this, matchingResourceData)); |
+ |
+ var badUrls = []; |
+ var cookieBytes = 0; |
+ for (var url in matchingResourceData) { |
+ badUrls.push(url); |
+ cookieBytes += matchingResourceData[url]; |
+ } |
+ if (badUrls.length < this._minResources) |
+ return; |
+ |
+ var entry = result.addChild( |
+ WebInspector.UIString( |
+ '%s of cookies were sent with the following static resources. Serve these static resources from a domain that does not set cookies:', |
+ Number.bytesToString(cookieBytes)), |
+ true); |
+ entry.addURLs(badUrls); |
+ result.violationCount = badUrls.length; |
+ } |
+ |
+ _collectorCallback(matchingResourceData, request, cookie) { |
+ matchingResourceData[request.url] = (matchingResourceData[request.url] || 0) + cookie.size(); |
+ } |
}; |