-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add table list formatter for HTML audits #1505
Changes from 5 commits
c624b6e
7056a53
fc593f3
0283448
1644176
2cb4469
9a82143
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,21 +89,25 @@ class UsesOptimizedImages extends Audit { | |
const url = URL.getDisplayName(image.url); | ||
const webpSavings = UsesOptimizedImages.computeSavings(image, 'webp'); | ||
|
||
let label = `${originalKb} KB total, webp savings: ${webpSavings.percent}%`; | ||
if (webpSavings.bytes > WEBP_ALREADY_OPTIMIZED_THRESHOLD_IN_BYTES) { | ||
hasAllEfficientImages = false; | ||
} | ||
|
||
let jpegSavings; | ||
if (/(jpeg|bmp)/.test(image.mimeType)) { | ||
const jpegSavings = UsesOptimizedImages.computeSavings(image, 'jpeg'); | ||
jpegSavings = UsesOptimizedImages.computeSavings(image, 'jpeg'); | ||
if (jpegSavings.bytes > 0) { | ||
hasAllEfficientImages = false; | ||
label += `, jpeg savings: ${jpegSavings.percent}%`; | ||
} | ||
} | ||
|
||
totalWastedBytes += webpSavings.bytes; | ||
results.push({url, label}); | ||
results.push({ | ||
url, | ||
total: `${originalKb} KB`, | ||
webpSavings: `${webpSavings ? webpSavings.percent + '%' : ''}`, | ||
jpegSavings: `${jpegSavings ? jpegSavings.percent + '%' : ''}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either add a check for it to be positive here or move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably also move these out of `` strings :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
}); | ||
return results; | ||
}, []); | ||
|
||
|
@@ -118,13 +122,26 @@ class UsesOptimizedImages extends Audit { | |
debugString = `Lighthouse was unable to decode some of your images: ${urls.join(', ')}`; | ||
} | ||
|
||
const createTable = Formatter.getByName( | ||
Formatter.SUPPORTED_FORMATS.TABLE).createTable; | ||
|
||
const table = createTable({ | ||
url: 'URL', | ||
total: 'Original (KB)', | ||
webpSavings: 'WebP savings', | ||
jpegSavings: 'JPEG savings' | ||
}, results); | ||
|
||
return UsesOptimizedImages.generateAuditResult({ | ||
displayValue, | ||
debugString, | ||
rawValue: hasAllEfficientImages && totalWastedBytes < TOTAL_WASTED_BYTES_THRESHOLD, | ||
extendedInfo: { | ||
formatter: Formatter.SUPPORTED_FORMATS.URLLIST, | ||
value: results | ||
formatter: Formatter.SUPPORTED_FORMATS.TABLE, | ||
value: { | ||
results, | ||
table | ||
} | ||
} | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,11 +80,21 @@ class PassiveEventsAudit extends Audit { | |
|
||
const groupedResults = EventHelpers.groupCodeSnippetsByLocation(results); | ||
|
||
const createTable = Formatter.getByName( | ||
Formatter.SUPPORTED_FORMATS.TABLE).createTable; | ||
|
||
const table = createTable({ | ||
url: 'URL', lineCol: 'Line/Col', type: 'Type', code: 'Snippet' | ||
}, groupedResults); | ||
|
||
return PassiveEventsAudit.generateAuditResult({ | ||
rawValue: groupedResults.length === 0, | ||
extendedInfo: { | ||
formatter: Formatter.SUPPORTED_FORMATS.URLLIST, | ||
value: groupedResults | ||
formatter: Formatter.SUPPORTED_FORMATS.TABLE, | ||
value: { | ||
results: groupedResults, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no changes needed to the mapping? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope. The table formatter will pull from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah so the extra info was already in there and was being ignored by URLLIST? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea. |
||
table | ||
} | ||
}, | ||
debugString | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<style> | ||
.table_list { | ||
margin-top: 8px; | ||
border: 1px solid #EBEBEB; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we care about alphabetizing our rules? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a PR that alphabetizing selectors was brought up (can't find it now), but we deemed it unrealistic without tooling. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a-ok by me! |
||
max-width: 100%; | ||
} | ||
.table_list th, | ||
.table_list td { | ||
overflow: auto; | ||
} | ||
.table_list th { | ||
background-color: #eee; | ||
padding: 15px 10px; | ||
} | ||
.table_list td { | ||
padding: 10px; | ||
} | ||
.table_list th:first-of-type, | ||
.table_list td:first-of-type { | ||
min-width: 40%; | ||
white-space: nowrap; | ||
} | ||
.table_list tr:nth-child(even) { | ||
background-color: #fafafa; | ||
} | ||
.table_list tr:hover { | ||
background-color: #fafafa; | ||
} | ||
.table_list code { | ||
white-space: pre; | ||
font-family: monospace; | ||
} | ||
|
||
.table_list.multicolumn { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
.table_list.multicolumn tr { | ||
display: flex; | ||
} | ||
.table_list.multicolumn th, | ||
.table_list.multicolumn td { | ||
flex: 1; | ||
} | ||
</style> | ||
|
||
{{#if table.rows}} | ||
<details class="subitem__details"> | ||
<summary class="subitem__detail">More information</summary> | ||
<table class="table_list {{#if_not_eq table.headings.length 2}}multicolumn{{/if_not_eq}}" | ||
cellspacing="0"> | ||
<thead> | ||
<tr> | ||
{{#each table.headings}}<th>{{this}}</th>{{/each}} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{{#each table.rows}} | ||
<tr> | ||
{{#each cols}} | ||
<td>{{ sanitize this}}</td> | ||
{{/each}} | ||
</tr> | ||
{{/each}} | ||
</tbody> | ||
</table> | ||
</details> | ||
{{/if}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Formatter = require('./formatter'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const html = fs.readFileSync(path.join(__dirname, 'partials/table.html'), 'utf8'); | ||
|
||
class Table extends Formatter { | ||
static getFormatter(type) { | ||
switch (type) { | ||
case 'pretty': | ||
return result => { | ||
if (!result) { | ||
return ''; | ||
} | ||
if (!result.table || !Array.isArray(result.table.headings) || | ||
!Array.isArray(result.table.rows)) { | ||
return ''; | ||
} | ||
|
||
let output = ''; | ||
result.table.rows.forEach(row => { | ||
output += ' '; | ||
row.cols.forEach(col => { | ||
// Omit code snippet cols. | ||
if (col.startsWith('`') && col.endsWith('`')) { | ||
return; | ||
} | ||
output += `${col} `; | ||
}); | ||
output += '\n'; | ||
}); | ||
return output; | ||
}; | ||
|
||
case 'html': | ||
// Returns a handlebars string to be used by the Report. | ||
return html; | ||
|
||
default: | ||
throw new Error('Unknown formatter type'); | ||
} | ||
} | ||
|
||
/** | ||
* Preps a formatted table (headings/col vals) for output. | ||
* @param {!Object<string, string>} headings for the table. The order of this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* object's key/value will be the order of the table's headings. | ||
* @param {!Array<*>} results Audit results. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like you maybe expect this to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @return {!{headings: string, rows: [{cols: [*]}]}} headings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks. done. |
||
*/ | ||
static createTable(headings, results) { | ||
const headingKeys = Object.keys(headings); | ||
|
||
const rows = results.map(result => { | ||
const cols = headingKeys.map(key => { | ||
switch (key) { | ||
case 'code': | ||
// Wrap code snippets in markdown ticks. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth documenting these special keys in jsdoc? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. added to the function description. If we ad more one-offs, that'll be easy to doc more. |
||
return '`' + result[key].trim() + '`'; | ||
case 'lineCol': | ||
// Create a combined line/col numbers for the lineCol key. | ||
return `${result.line}:${result.col}`; | ||
default: | ||
return result[key]; | ||
} | ||
}); | ||
|
||
return {cols}; | ||
}); | ||
|
||
headings = headingKeys.map(key => headings[key]); | ||
|
||
return {headings, rows}; | ||
} | ||
} | ||
|
||
module.exports = Table; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't need the check here for
webpSavings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done