Skip to content

Commit 0433755

Browse files
authored
fix: improve HTML report formatting (#1621)
* fix: improve HTML report formatting automatic dark-mode support. * Delete current.html * Update html.js
1 parent 619c190 commit 0433755

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

dist/cli/formatters/html.js

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/formatters/html.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ const htmlFormatter: FormatterCallback = function (formatter) {
1010
'\n<meta name="viewport" content="width=device-width, initial-scale=1">'
1111
fileContent += '\n<title>HTML Hint Violation Report</title>'
1212
fileContent += '\n<meta name="generator" content="HTMLHint">'
13+
fileContent +=
14+
'\n<style>body{font-family:Arial,helvetica,sans-serif;} footer{margin-top:20px;text-align:center;opacity:0.5;}</style>'
15+
fileContent +=
16+
'\n<style>table{border-collapse:collapse;width:100%;} th,td{border:1px solid rgb(128,128,128,0.4);padding:8px;text-align:left;} th{background-color:rgb(128,128,128,0.2);}</style>'
17+
fileContent +=
18+
'\n<style>@media (prefers-color-scheme: dark) {body {background-color:#333;color:#fff;}}</style>'
1319
fileContent += '\n</head>'
14-
fileContent += '\n<body style="font-family: Arial, helvetica, sans-serif;">'
20+
fileContent += '\n<body>'
1521
fileContent += '\n<h1>Violation Report</h1>'
1622
fileContent += '\n<main>'
17-
fileContent += '\n<table style="border:1px solid #ccc;margin-bottom:20px;">'
23+
fileContent += '\n<table>'
1824
fileContent +=
1925
'\n<tr><th>Number#</th><th>File Name</th><th>Line Number</th><th>Message</th></tr>'
2026

@@ -31,7 +37,7 @@ const htmlFormatter: FormatterCallback = function (formatter) {
3137

3238
fileContent += '</table>'
3339
fileContent +=
34-
'\n<footer><small style="opacity:0.5">Generated by <a href="https://htmlhint.com" target="_blank" rel="noopener">HTMLHint</a></small></footer>'
40+
'\n<footer><small>Generated by <a href="https://htmlhint.com" target="_blank" rel="noopener">HTMLHint</a></small></footer>'
3541
fileContent += '\n</main>'
3642
fileContent += '\n</body>'
3743
fileContent += '</html>'

test/cli/formatters/html.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
<meta name="viewport" content="width=device-width, initial-scale=1">
55
<title>HTML Hint Violation Report</title>
66
<meta name="generator" content="HTMLHint">
7+
<style>body{font-family:Arial,helvetica,sans-serif;} footer{margin-top:20px;text-align:center;opacity:0.5;}</style>
8+
<style>table{border-collapse:collapse;width:100%;} th,td{border:1px solid rgb(128,128,128,0.4);padding:8px;text-align:left;} th{background-color:rgb(128,128,128,0.2);}</style>
9+
<style>@media (prefers-color-scheme: dark) {body {background-color:#333;color:#fff;}}</style>
710
</head>
8-
<body style="font-family: Arial, helvetica, sans-serif;">
11+
<body>
912
<h1>Violation Report</h1>
1013
<main>
11-
<table style="border:1px solid #ccc;margin-bottom:20px;">
14+
<table>
1215
<tr><th>Number#</th><th>File Name</th><th>Line Number</th><th>Message</th></tr>
1316
<tr><td>1</td><td>{{path}}</td><td>8</td><td>The value of attribute [ bad ] must be in double quotes.</td></tr>
1417
<tr><td>2</td><td>{{path}}</td><td>8</td><td>The value of attribute [ bad ] must be in double quotes.</td></tr>
@@ -30,6 +33,6 @@ <h1>Violation Report</h1>
3033
<tr><td>18</td><td>{{path}}</td><td>25</td><td>Tag must be paired, no start tag: [ </div> ]</td></tr>
3134
<tr><td>19</td><td>{{path}}</td><td>26</td><td>Tag must be paired, no start tag: [ </bad> ]</td></tr>
3235
<tr><td>20</td><td>{{path}}</td><td>27</td><td>Tag must be paired, no start tag: [ </bad> ]</td></tr></table>
33-
<footer><small style="opacity:0.5">Generated by <a href="https://htmlhint.com" target="_blank" rel="noopener">HTMLHint</a></small></footer>
36+
<footer><small>Generated by <a href="https://htmlhint.com" target="_blank" rel="noopener">HTMLHint</a></small></footer>
3437
</main>
3538
</body></html>

test/cli/formatters/html.spec.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@ const path = require('path')
44

55
describe('CLI', () => {
66
describe('Formatter: html', () => {
7+
// Increase timeout to 60 seconds to avoid timeout errors
8+
jest.setTimeout(60000)
9+
710
it('should have stdout output with formatter html', (done) => {
811
const expected = fs
912
.readFileSync(path.resolve(__dirname, 'html.html'), 'utf8')
1013
.replace(/\{\{path\}\}/g, path.resolve(__dirname, 'example.html'))
1114

12-
const expectedParts = expected.split('\n')
15+
const expectedParts = expected
16+
.replace(/\r\n|\r|\n/g, '\n')
17+
.split('\n')
18+
.map((line) => {
19+
// Normalize CSS in style tags to match the minified version in the formatter
20+
return line.replace(
21+
/<style>(.*?)<\/style>/g,
22+
(match, p1) => `<style>${p1.replace(/\s+/g, '')}</style>`
23+
)
24+
})
25+
.filter((line) => line.trim() !== '')
1326

1427
ChildProcess.exec(
1528
[
@@ -25,7 +38,17 @@ describe('CLI', () => {
2538

2639
expect(stdout).not.toBe('')
2740

28-
const stdoutParts = stdout.split('\n')
41+
const stdoutParts = stdout
42+
.replace(/\r\n|\r|\n/g, '\n')
43+
.split('\n')
44+
.map((line) => {
45+
// Normalize CSS in style tags to match the expected output
46+
return line.replace(
47+
/<style>(.*?)<\/style>/g,
48+
(match, p1) => `<style>${p1.replace(/\s+/g, '')}</style>`
49+
)
50+
})
51+
.filter((line) => line.trim() !== '')
2952

3053
expect(stdoutParts.length).toBe(expectedParts.length)
3154

0 commit comments

Comments
 (0)