-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add table list formatter for HTML audits (#1505)
- Loading branch information
1 parent
59581c7
commit e1fc9e0
Showing
12 changed files
with
330 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<style> | ||
.table_list { | ||
margin-top: 8px; | ||
border: 1px solid #EBEBEB; | ||
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.