-
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 all 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<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> | ||
|
||
{{#createTable tableHeadings results }} | ||
|
||
{{#if rows}} | ||
<details class="subitem__details"> | ||
<summary class="subitem__detail">More information</summary> | ||
<table class="table_list {{#if_not_eq headings.length 2}}multicolumn{{/if_not_eq}}" | ||
cellspacing="0"> | ||
<thead> | ||
<tr> | ||
{{#each headings}}<th>{{this}}</th>{{/each}} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{{#each rows}} | ||
<tr> | ||
{{#each cols}} | ||
<td>{{ sanitize this}}</td> | ||
{{/each}} | ||
</tr> | ||
{{/each}} | ||
</tbody> | ||
</table> | ||
</details> | ||
{{/if}} | ||
|
||
{{/createTable}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @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 => { | ||
let output = ''; | ||
|
||
if (!result || !result.results || !result.tableHeadings) { | ||
return output; | ||
} | ||
|
||
const table = Table.createTable(result.tableHeadings, result.results); | ||
|
||
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>} headings for the table. The order of this | ||
* object's key/value pairs determines the order of the HTML table headings. | ||
* There is special handling for certain keys: | ||
* code: wraps the value in ticks as a markdown code snippet. | ||
* lineCol: combines the values for the line and col keys into a single | ||
* value "line/col". | ||
* All other values are passed through as is. | ||
* @param {!Array<!Object>} results Audit results. | ||
* @return {{headings: !Array<string>, rows: !Array<{cols: !Array<*>}>}} | ||
*/ | ||
static createTable(headings, results) { | ||
headings = headings || {}; | ||
results = results || []; | ||
|
||
const headingKeys = Object.keys(headings); | ||
|
||
const rows = results.map(result => { | ||
const cols = headingKeys.map(key => { | ||
switch (key) { | ||
case 'code': | ||
return '`' + result[key].trim() + '`'; | ||
case 'lineCol': | ||
return `${result.line}:${result.col}`; | ||
default: | ||
return result[key]; | ||
} | ||
}); | ||
|
||
return {cols}; | ||
}); | ||
|
||
headings = headingKeys.map(key => headings[key]); | ||
|
||
return {headings, rows}; | ||
} | ||
|
||
static getHelpers() { | ||
return { | ||
createTable(headings, results, opts) { | ||
return opts.fn(Table.createTable(headings, results)); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
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.
no changes needed to the mapping?
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.
nope. The table formatter will pull from the
groupedResults
object as needed, based on the keys used in the first arg tocreateTable({url: 'URL', lineCol: 'Line/Col', type: 'Type', code: 'Snippet'})
.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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Yea.