11const app = document . querySelector ( '#app' ) ;
2- const violations = fetch ( 'violations.json' ) . then ( response => response . json ( ) ) ;
2+ const dialog = document . querySelector ( '.dialog-violations' ) ;
3+ const dialogViolationsList = dialog . querySelector ( '.dialog-violations__list' ) ;
4+ const dialogViolationsTitle = dialog . querySelector ( '.dialog-violations__title' ) ;
5+
36// Helper function to convert to sentence case
47const toSentenceCase = ( string ) => {
58 return string
@@ -8,6 +11,7 @@ const toSentenceCase = (string) => {
811 . map ( word => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) )
912 . join ( ' ' ) ;
1013} ;
14+
1115const replaceHyphensWithSpaces = ( string ) => string . replace ( / - / g, ' ' ) ;
1216
1317// Helper function to escape HTML
@@ -20,12 +24,12 @@ const escapeHtml = (unsafe) => {
2024 . replace ( / ' / g, "'" ) ;
2125} ;
2226
23- const dialog = document . querySelector ( '.dialog-violations' ) ;
24- const violationsList = dialog . querySelector ( '.dialog-violations__list' ) ;
27+ const violations = fetch ( 'violations.json' ) . then ( response => response . json ( ) ) ;
2528
2629violations . then ( data => {
2730 const numberOfURLsTested = data . numberOfURLsTested ;
2831 const allViolations = data . violations ;
32+ const numberOfViolations = allViolations . length ;
2933
3034 // Group violations by type
3135 const violationsByType = allViolations . reduce ( ( acc , violation ) => {
@@ -43,36 +47,63 @@ violations.then(data => {
4347 return acc ;
4448 } , { } ) ;
4549
46- // Count the number of nodes in color-contrast violations
47- // const numberOfColorContrastNodes = violationVariables['color-contrast'].reduce((acc, violation) => acc + violation.nodes.length, 0);
48-
4950 // Get unique URLs with violations
5051 const uniqueURLsWithViolations = [ ...new Set ( allViolations . map ( violation => violation . url ) ) ] ;
5152
53+ // Count the number of issues for each impact level
54+ const impactCounts = allViolations . reduce ( ( acc , violation ) => {
55+ const impact = violation . impact || 'unknown' ;
56+ if ( ! acc [ impact ] ) {
57+ acc [ impact ] = 0 ;
58+ }
59+ acc [ impact ] += 1 ;
60+ return acc ;
61+ } , { } ) ;
62+
63+ // Set the content of the app element
5264 app . innerHTML = `
5365 <h2 id="summary">Summary</h2>
5466 <h3>URLs with violations</h3>
5567 <p>We tested <strong>${ numberOfURLsTested } URLs</strong> and found <strong>${ uniqueURLsWithViolations . length } URLs</strong> with accessibility violations <strong>(${ ( uniqueURLsWithViolations . length / numberOfURLsTested * 100 ) . toFixed ( 2 ) } %</strong> of URLs).</p>
68+ <p>The total number of violations found was <strong>${ numberOfViolations } </strong></p>
69+
70+ <h3>Issues by Impact</h3>
71+ <ul>
72+ ${ Object . entries ( impactCounts ) . map ( ( [ impact , count ] ) => `
73+ <li>${ toSentenceCase ( impact ) } : ${ count } </li>
74+ ` ) . join ( '' ) }
75+ </ul>
76+
77+ <h3>Issues by Type</h3>
78+ <ul>
79+ ${ violationTypes . map ( type => `
80+ <li>
81+ ${ violationVariables [ type ] . length } ${ replaceHyphensWithSpaces ( toSentenceCase ( type ) ) }
82+ </li>
83+ ` ) . join ( '' ) }
84+ </ul>
85+
5686 <ul>
5787 ${ uniqueURLsWithViolations . map ( url => `
5888 <li>
5989 <a href="${ url } ">${ url } </a>
6090 </li>
6191 ` ) . join ( '' ) }
6292 </ul>
93+
6394 <h2 id="violations">Accessibility Violations</h2>
64- <div class="grid grid--2 ">
95+ <div class="grid">
6596 ${ violationTypes . map ( type => `
6697 <article class="violations">
6798 <h3>${ toSentenceCase ( replaceHyphensWithSpaces ( type ) ) } </h3>
6899 <p>There are <strong>${ violationVariables [ type ] . length } ${ type } violations</strong> across <strong>${ uniqueURLsWithViolations . length } URLs</strong>.</p>
100+ <p>Impact: <span class="impact impact--${ violationVariables [ type ] [ 0 ] [ 'impact' ] } ">${ toSentenceCase ( violationVariables [ type ] [ 0 ] [ 'impact' ] ) } </span></p>
101+ <p>Description: ${ violationVariables [ type ] [ 0 ] [ 'description' ] } </p>
69102 <ol class="violations__list">
70103 ${ violationVariables [ type ] . map ( ( violation , index ) => `
71104 <li>
72105 <a href="${ violation . url } ">${ violation . url } </a>
73106 <ul>
74- <li>Description: ${ violation . description } </li>
75- <li>Impact: <span class="impact impact--${ violation . impact } ">${ toSentenceCase ( violation . impact ) } </span></li>
76107 <li>Instances: ${ violation . nodes . length } - <button data-index="${ index } " data-type="${ type } " class="view-violations" type="button">View All</button></li>
77108 </ul>
78109 </li>
@@ -89,26 +120,34 @@ violations.then(data => {
89120 const type = event . target . getAttribute ( 'data-type' ) ;
90121 const index = event . target . getAttribute ( 'data-index' ) ;
91122 const violation = violationVariables [ type ] [ index ] ;
92- violationsList . innerHTML = '' ;
123+ const numberOfInstances = violation . nodes . length ;
124+ const violationSummary = violation . nodes [ 0 ] [ 'failureSummary' ] ;
125+ dialogViolationsList . innerHTML = '' ;
126+ dialogViolationsTitle . innerHTML = '' ;
127+
128+ dialogViolationsTitle . innerHTML = `
129+ <h2>${ numberOfInstances } ${ toSentenceCase ( replaceHyphensWithSpaces ( type ) ) } Violations</h2>
130+ <p>URL: <a href="${ violation . url } ">${ violation . url } </a></p>
131+ <p>Summary: ${ violationSummary } </p>
132+ ` ;
93133
94134 violation . nodes . forEach ( node => {
95- console . log ( node ) ;
96135 const listItem = document . createElement ( 'li' ) ;
97136 listItem . innerHTML = `
98- <p><strong>HTML:</strong></p >
99- < code>${ escapeHtml ( node . html ) } </code>
100- <p ><strong>Target:</strong> ${ node . target . join ( ' > ' ) } </p >
101- <p><strong>Summary:</strong> ${ node . failureSummary } </p >
137+ <ul >
138+ <li><strong>HTML:</strong> < code>${ escapeHtml ( node . html ) } </code></li >
139+ <li ><strong>Target:</strong> ${ node . target . join ( ' > ' ) } </li >
140+ </ul >
102141 ` ;
103- violationsList . appendChild ( listItem ) ;
142+ dialogViolationsList . appendChild ( listItem ) ;
104143 } ) ;
105144
106145 dialog . showModal ( ) ;
107146 } ) ;
108147 } ) ;
109148
110149 // Event listener for the close dialog button
111- dialog . querySelector ( '.dialog-close ' ) . addEventListener ( 'click' , ( ) => {
150+ dialog . querySelector ( '.dialog-violations__close ' ) . addEventListener ( 'click' , ( ) => {
112151 document . querySelector ( '.dialog-violations' ) . close ( ) ;
113152 } ) ;
114153
0 commit comments