-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathindex.js
More file actions
1030 lines (894 loc) · 34.4 KB
/
Copy pathindex.js
File metadata and controls
1030 lines (894 loc) · 34.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable padded-blocks, no-multiple-empty-lines */
/* global edacFrontendHighlighterApp */
import { computePosition, autoUpdate } from '@floating-ui/dom';
import { createFocusTrap } from 'focus-trap';
import { isFocusable } from 'tabbable';
import { __, _n } from '@wordpress/i18n';
import { saveFixSettings } from '../common/saveFixSettingsRest';
import { fillFixesModal, fixSettingsModalInit, openFixesModal } from './fixesModal';
class AccessibilityCheckerHighlight {
/**
* Constructor
* @param {Object} settings
*/
constructor( settings = {} ) {
const defaultSettings = {
showIgnored: false,
};
this.settings = { ...defaultSettings, ...settings };
this._scanAttempted = false;
this.highlightPanel = this.addHighlightPanel();
this.nextButton = document.querySelector( '#edac-highlight-next' );
this.previousButton = document.querySelector( '#edac-highlight-previous' );
this.panelToggle = document.querySelector( '#edac-highlight-panel-toggle' );
this.closePanel = document.querySelector( '#edac-highlight-panel-controls-close' );
this.panelDescription = document.querySelector( '#edac-highlight-panel-description' );
this.panelControls = document.querySelector( '#edac-highlight-panel-controls' );
this.descriptionCloseButton = document.querySelector( '.edac-highlight-panel-description-close' );
this.issues = null;
this.fixes = null;
this.currentButtonIndex = null;
this.urlParameter = this.get_url_parameter( 'edac' );
this.currentIssueStatus = null;
this.tooltips = [];
this.panelControlsFocusTrap = createFocusTrap( '#' + this.panelControls.id, {
clickOutsideDeactivates: true,
escapeDeactivates: () => {
this.panelClose();
},
} );
this.panelDescriptionFocusTrap = createFocusTrap( '#' + this.panelDescription.id, {
clickOutsideDeactivates: true,
escapeDeactivates: () => {
this.descriptionClose();
},
} );
this.disableStylesButton = document.querySelector( '#edac-highlight-disable-styles' );
this.stylesDisabled = false;
this.originalCss = [];
this.init();
}
/**
* This function initializes the component by setting up event listeners
* and managing the initial state of the panel based on the URL parameter.
*/
init() {
// Add event listeners for 'next' and 'previous' buttons
this.nextButton.addEventListener( 'click', () => {
this.highlightFocusNext();
this.focusTrapDescription();
} );
this.previousButton.addEventListener( 'click', () => {
this.highlightFocusPrevious();
this.focusTrapDescription();
} );
// Manage panel open/close operations
this.panelToggle.addEventListener( 'click', () => {
this.panelOpen();
this.focusTrapControls();
} );
this.closePanel.addEventListener( 'click', () => {
this.panelClose();
this.panelControlsFocusTrap.deactivate();
this.panelDescriptionFocusTrap.deactivate();
this.enableStyles();
} );
// Close description when close button is clicked
this.descriptionCloseButton.addEventListener( 'click', () => this.descriptionClose() );
// Handle disable/enable styles
this.disableStylesButton.addEventListener( 'click', () => {
if ( this.stylesDisabled ) {
this.enableStyles();
} else {
this.disableStyles();
}
} );
// Open panel if a URL parameter exists
if ( this.urlParameter ) {
this.panelOpen( this.urlParameter );
}
}
/**
* This function tries to find an element on the page that matches a given HTML snippet.
* It parses the HTML snippet, and compares the outer HTML of the parsed element
* with all elements present on the page. If a match is found, it
* adds a tooltip, checks if the element is focusable, and then returns the element.
* If no matching element is found, or if the parsed HTML snippet does not contain an element,
* it returns null.
*
* @param {Object} value - Object containing the HTML snippet to be matched.
* @param {number} index - Index of the element being searched.
* @return {HTMLElement|null} - Returns the matching HTML element, or null if no match is found.
*/
findElement( value, index ) {
// Parse the HTML snippet
let htmlToFind = value.object;
const parser = new DOMParser();
const parsedHtml = parser.parseFromString( htmlToFind, 'text/html' );
const firstParsedElement = parsedHtml.body.firstElementChild;
if ( firstParsedElement ) {
htmlToFind = firstParsedElement.outerHTML;
}
// Compare the outer HTML of the parsed element with all elements on the page
const allElements = document.body.querySelectorAll( '*' );
for ( const element of allElements ) {
if ( element.outerHTML.replace( /\W/g, '' ) === htmlToFind.replace( /\W/g, '' ) ) {
const tooltip = this.addTooltip( element, value, index, this.issues.length );
this.issues[ index ].tooltip = tooltip.tooltip;
this.tooltips.push( tooltip );
return element;
}
}
// If no matching element is found, return null
return null;
}
/**
* This function makes an AJAX call to the server to retrieve the list of issues.
*
* Note: This function assumes that `edacFrontendHighlighterApp` is a global variable containing necessary data.
*/
highlightAjax() {
const self = this;
return new Promise( function( resolve, reject ) {
const xhr = new XMLHttpRequest();
const url = edacFrontendHighlighterApp.ajaxurl + '?action=edac_frontend_highlight_ajax&post_id=' + edacFrontendHighlighterApp.postID + '&nonce=' + edacFrontendHighlighterApp.nonce;
self.showWait( true );
xhr.open( 'GET', url );
xhr.onload = function() {
if ( xhr.status === 200 ) {
self.showWait( false );
const response = JSON.parse( xhr.responseText );
if ( true === response.success ) {
const responseJson = JSON.parse( response.data );
if ( self.settings.showIgnored ) {
resolve( {
issues: responseJson.issues,
fixes: responseJson.fixes,
} );
} else {
resolve(
{
issues: responseJson.issues.filter( ( item ) => {
// When rules are filtered off from php we can get null values for some properties
// here. This should be fixed upstream but handling it here as well for robustness.
if ( item.rule_type === null ) {
return false;
}
return ( item.id === self.urlParameter || item.rule_type !== 'ignored' );
} ),
fixes: responseJson.fixes,
},
);
}
} else if ( ! self._scanAttempted && response.data?.[ 0 ]?.code === -3 ) {
// Only try kickoffScan once per highlightAjax call
self._scanAttempted = true;
self.kickoffScan();
// After kickoffScan, try highlightAjax again, but only once
setTimeout( () => {
self.highlightAjax().then( resolve ).catch( reject );
}, 5000 ); // Wait 5s for scan to complete.
} else {
// Default: resolve with empty issues/fixes
resolve( { issues: [], fixes: [] } );
}
} else {
self.showWait( false );
reject( {
status: xhr.status,
statusText: xhr.statusText,
} );
}
};
xhr.onerror = function() {
self.showWait( false );
reject( {
status: xhr.status,
statusText: xhr.statusText,
} );
};
xhr.send();
} );
}
/**
* This function toggles showing Wait
* @param {boolean} status
*/
showWait( status = true ) {
if ( status ) {
document.querySelector( 'body' ).classList.add( 'edac-app-wait' );
} else {
document.querySelector( 'body' ).classList.remove( 'edac-app-wait' );
}
}
/**
* This function removes the highlight/tooltip buttons and runs cleanups for each.
*/
removeHighlightButtons() {
this.tooltips.forEach( ( item ) => {
//remove click listener
item.tooltip.removeEventListener( 'click', item.listeners.onClick );
//remove position/resize listener: https://floating-ui.com/docs/autoUpdate
item.listeners.cleanup();
} );
const buttons = document.querySelectorAll( '.edac-highlight-btn' );
buttons.forEach( ( button ) => {
button.remove();
} );
}
/**
* This function adds a new button element to the DOM, which acts as a tooltip for the highlighted element.
*
* @param {HTMLElement} element - The DOM element before which the tooltip button will be inserted.
* @param {Object} value - An object containing properties used to customize the tooltip button.
* @param {number} index - The index of the element being processed.
* @return {Object} - information about the tooltip
*/
/* eslint-disable no-unused-vars */
addTooltip( element, value, index, totalItems ) {
// Create the tooltip.
const tooltip = document.createElement( 'button' );
tooltip.classList = 'edac-highlight-btn edac-highlight-btn-' + value.rule_type;
tooltip.setAttribute( 'aria-label', `Open details for ${ value.rule_title }, ${ index + 1 } of ${ totalItems }` );
tooltip.setAttribute( 'aria-expanded', 'false' );
tooltip.setAttribute( 'aria-haspopup', 'dialog' );
//add data-id to the tooltip/button so we can find it later.
tooltip.dataset.id = value.id;
const onClick = ( e ) => {
const id = e.currentTarget.dataset.id;
this.showIssue( id );
};
tooltip.addEventListener( 'click', onClick );
// Add the tooltip to the page.
document.body.append( tooltip );
const updatePosition = function() {
computePosition( element, tooltip, {
placement: 'top-start',
middleware: [],
} ).then( ( { x, y, middlewareData, placement } ) => {
const elRect = element.getBoundingClientRect();
const elHeight = element.offsetHeight === undefined ? 0 : element.offsetHeight;
const elWidth = element.offsetWidth === undefined ? 0 : element.offsetWidth;
const tooltipHeight = tooltip.offsetHeight === undefined ? 0 : tooltip.offsetHeight;
const tooltipWidth = tooltip.offsetWidth === undefined ? 0 : tooltip.offsetWidth;
let top = 0;
const left = 0;
if ( tooltipHeight <= ( elHeight * .8 ) ) {
top = tooltipHeight;
}
if ( tooltipWidth >= ( elWidth * .8 ) ) {
top = 0;
}
if ( elRect.left < tooltipWidth ) {
x = 0;
}
if ( elRect.left > window.screen ) {
x = window.screen.width - tooltipWidth;
}
if ( elRect.top < tooltipHeight ) {
y = 0;
}
Object.assign( tooltip.style, {
left: `${ x + left }px`,
top: `${ y + top }px`,
} );
} );
};
// Place the tooltip at the element's position on the page.
// See: https://floating-ui.com/docs/autoUpdate
const cleanup = autoUpdate(
element,
tooltip,
updatePosition, {
ancestorScroll: true,
ancestorResize: true,
elementResize: true,
layoutShift: true,
animationFrame: true, // TODO: Disable styles sometimes causes the toolbar to disappear until a scroll or resize event. This may help - but is expensive.
}
);
return {
element,
tooltip,
listeners: {
onClick,
cleanup,
},
};
}
/**
* This function adds a new div element to the DOM, which contains the accessibility checker panel.
*/
addHighlightPanel() {
const widgetPosition = edacFrontendHighlighterApp.widgetPosition || 'right';
const newElement = `
<div id="edac-highlight-panel" class="edac-highlight-panel edac-highlight-panel--${ widgetPosition }">
<button id="edac-highlight-panel-toggle" class="edac-highlight-panel-toggle" aria-haspopup="dialog" aria-label="Accessibility Checker Tools"></button>
<div id="edac-highlight-panel-description" class="edac-highlight-panel-description" role="dialog" aria-labelledby="edac-highlight-panel-description-title" tabindex="0">
<button class="edac-highlight-panel-description-close edac-highlight-panel-controls-close" aria-label="Close">×</button>
<div id="edac-highlight-panel-description-title" class="edac-highlight-panel-description-title"></div>
<div class="edac-highlight-panel-description-content"></div>
<div id="edac-highlight-panel-description-code" class="edac-highlight-panel-description-code"><code></code></div>
</div>
<div id="edac-highlight-panel-controls" class="edac-highlight-panel-controls" tabindex="0">
<button id="edac-highlight-panel-controls-close" class="edac-highlight-panel-controls-close" aria-label="Close">×</button>
<div class="edac-highlight-panel-controls-title">Accessibility Checker</div>
<div class="edac-highlight-panel-controls-summary">Loading...</div>
<div class="edac-highlight-panel-controls-buttons">
<div>
<button id="edac-highlight-previous" disabled="true"><span aria-hidden="true">« </span>Previous</button>
<button id="edac-highlight-next" disabled="true">Next<span aria-hidden="true"> »</span></button><br />
</div>
<div>
<button id="edac-highlight-disable-styles" class="edac-highlight-disable-styles" aria-live="polite" aria-label="${ __( 'Disable Page Styles', 'accessibility-checker' ) }">${ __( 'Disable Styles', 'text-domain' ) }</button>
</div>
</div>
</div>
</div>
`;
document.body.insertAdjacentHTML( 'afterbegin', newElement );
return document.getElementById( 'edac-highlight-panel' );
}
/**
* This function highlights the next element on the page. It uses the 'currentButtonIndex' property to keep track of the current element.
*/
highlightFocusNext = () => {
if ( this.currentButtonIndex === null ) {
this.currentButtonIndex = 0;
} else {
this.currentButtonIndex = ( this.currentButtonIndex + 1 ) % this.issues.length;
}
const id = this.issues[ this.currentButtonIndex ].id;
this.showIssue( id );
};
/**
* This function highlights the previous element on the page. It uses the 'currentButtonIndex' property to keep track of the current element.
*/
highlightFocusPrevious = () => {
if ( this.currentButtonIndex === null ) {
this.currentButtonIndex = this.issues.length - 1;
} else {
this.currentButtonIndex = ( this.currentButtonIndex - 1 + this.issues.length ) % this.issues.length;
}
const id = this.issues[ this.currentButtonIndex ].id;
this.showIssue( id );
};
/**
* This function sets a focus trap on the controls panel
*/
focusTrapControls = () => {
this.panelDescriptionFocusTrap.deactivate();
this.panelControlsFocusTrap.activate();
setTimeout( () => {
this.panelControls.focus();
}, 100 ); //give render time to complete.
};
/**
* This function sets a focus trap on the description panel
*/
focusTrapDescription = () => {
this.panelControlsFocusTrap.deactivate();
this.panelDescriptionFocusTrap.activate();
setTimeout( () => {
this.panelDescription.focus();
}, 100 ); //give render time to complete.
};
/**
* This function shows an issue related to an element.
* @param {string} id - The ID of the element.
*/
showIssue = ( id ) => {
this.removeSelectedClasses();
if ( id === undefined ) {
return;
}
const issue = this.issues.find( ( i ) => i.id === id );
this.currentButtonIndex = this.issues.findIndex( ( i ) => i.id === id );
const tooltip = issue.tooltip;
const element = issue.element;
if ( tooltip && element ) {
tooltip.classList.add( 'edac-highlight-btn-selected' );
element.classList.add( 'edac-highlight-element-selected' );
if ( element.offsetWidth < 20 ) {
element.classList.add( 'edac-highlight-element-selected-min-width' );
}
if ( element.offsetHeight < 5 ) {
element.classList.add( 'edac-highlight-element-selected-min-height' );
}
element.scrollIntoView( { block: 'center' } );
if ( isFocusable( tooltip ) ) {
//issueElement.focus();
if ( ! this.checkVisibility( tooltip ) || ! this.checkVisibility( element ) ) {
this.currentIssueStatus = 'The element is not visible. Try disabling styles.';
//TODO: console.log(`Element with id ${id} is not visible!`);
} else {
this.currentIssueStatus = null;
}
} else {
this.currentIssueStatus = 'The element is not focusable. Try disabling styles.';
//TODO: console.log(`Element with id ${id} is not focusable!`);
}
} else {
this.currentIssueStatus = 'The element was not found on the page.';
//TODO: console.log(`Element with id ${id} not found in the document!`);
}
this.descriptionOpen( id );
};
/**
* This function checks if a given element is visible on the page.
*
* @param {HTMLElement} el The element to check for visibility
* @return {boolean} isVisible
*/
checkVisibility = ( el ) => {
//checkVisibility is still in draft but well supported on many browsers.
//See: https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
//See: https://caniuse.com/mdn-api_element_checkvisibility
if ( typeof ( el.checkVisibility ) !== 'function' ) {
//See: https://github.com/jquery/jquery/blob/main/src/css/hiddenVisibleSelectors.js
return !! ( el.offsetWidth || el.offsetHeight || el.getClientRects().length );
}
return el.checkVisibility( {
checkOpacity: true, // Check CSS opacity property too
checkVisibilityCSS: true, // Check CSS visibility property too
} );
};
/**
* This function opens the accessibility checker panel.
* @param {number} id of the issue
*/
panelOpen( id ) {
this.highlightPanel.classList.add( 'edac-highlight-panel-visible' );
this.panelControls.style.display = 'block';
this.panelToggle.style.display = 'none';
// previous and next buttons are disabled until we have issues to show.
this.nextButton.disabled = true;
this.previousButton.disabled = true;
// Get the issues for this page.
this.highlightAjax().then(
( json ) => {
this.issues = json.issues;
this.fixes = json.fixes;
json.issues.forEach( function( value, index ) {
const element = this.findElement( value, index );
if ( element !== null ) {
this.issues[ index ].element = element;
}
}.bind( this ) );
this.showIssueCount();
if ( id !== undefined ) {
this.showIssue( id );
this.focusTrapDescription();
}
}
).catch( ( err ) => {
// Output a message that says that there are no issues or that the issues could not be loaded.
const summary = document.querySelector( '.edac-highlight-panel-controls-summary' );
if ( summary ) {
summary.textContent = __( 'An error occurred when loading the issues.', 'accessibility-checker' );
}
} );
}
/**
* This function closes the accessibility checker panel.
*/
panelClose() {
this.highlightPanel.classList.remove( 'edac-highlight-panel-visible' );
this.panelControls.style.display = 'none';
this.panelDescription.style.display = 'none';
this.panelToggle.style.display = 'block';
this.removeSelectedClasses();
this.removeHighlightButtons();
this.closePanel.removeEventListener( 'click', this.panelControlsFocusTrap.deactivate );
this.panelToggle.focus();
}
/**
* This function removes the classes that indicates a button or element are selected
*/
removeSelectedClasses = () => {
//remove selected class from previously selected buttons
const selectedButtons = document.querySelectorAll( '.edac-highlight-btn-selected' );
selectedButtons.forEach( ( selectedButton ) => {
selectedButton.classList.remove( 'edac-highlight-btn-selected' );
} );
//remove selected class from previously selected elements
const selectedElements = document.querySelectorAll( '.edac-highlight-element-selected' );
selectedElements.forEach( ( selectedElement ) => {
selectedElement.classList.remove(
'edac-highlight-element-selected',
'edac-highlight-element-selected-min-width',
'edac-highlight-element-selected-min-height'
);
if ( selectedElement.classList.length === 0 ) {
selectedElement.removeAttribute( 'class' );
}
} );
};
/**
* This function displays the description of the issue.
*
* @param {string} dataId
*/
descriptionOpen( dataId ) {
// get the value of the property by key
const searchTerm = dataId;
const keyToSearch = 'id';
const matchingObj = this.issues.find( ( obj ) => obj[ keyToSearch ] === searchTerm );
if ( matchingObj ) {
const descriptionTitle = document.querySelector( '.edac-highlight-panel-description-title' );
const descriptionContent = document.querySelector( '.edac-highlight-panel-description-content' );
const descriptionCode = document.querySelector( '.edac-highlight-panel-description-code code' );
let content = '';
// Get the index and total
content += ` <div class="edac-highlight-panel-description-index">${ this.currentButtonIndex + 1 } of ${ this.issues.length }</div>`;
// Get the status of the issue
if ( this.currentIssueStatus ) {
content += ` <div class="edac-highlight-panel-description-status">${ this.currentIssueStatus }</div>`;
}
// Get the summary of the issue
content += matchingObj.summary;
if ( this.fixes[ matchingObj.slug ] && window.edacFrontendHighlighterApp?.userCanFix ) {
// this is the markup to put in the modal.
content += `
<div style="display:none;" class="always-hide">
<div class="edac-fix-settings">
<div class="edac-fix-settings--fields">
${ this.fixes[ matchingObj.slug ].fields }
<div class="edac-fix-settings--action-row">
<button role="button" class="button button-primary edac-fix-settings--button--save">
${ __( 'Save', 'accessibility-checker' ) }
</button>
<span class="edac-fix-settings--notice-slot" aria-live="polite" role="alert"></span>
</div>
</div>
</div>
</div>
`;
// and the button that will trigger the modal.
content += ` <br />
<button role="button"
class="edac-fix-settings--button--open edac-highlight-panel-description--button"
aria-haspopup="true"
aria-controls="edac-highlight-panel-description-fix"
aria-label="Fix issue: ${ this.fixes[ matchingObj.slug ][ Object.keys( this.fixes[ matchingObj.slug ] )[ 0 ] ].group_name }"> Fix Issue</button>`;
} else {
content += ` <br />`;
}
// Get the link to the documentation
content += `<a class="edac-highlight-panel-description-reference" href="${ matchingObj.link }">Full Documentation</a>`;
// Get the code button
content += `<button class="edac-highlight-panel-description-code-button" aria-expanded="false" aria-controls="edac-highlight-panel-description-code">${ __( 'Show Code', 'accessibility-checker' ) }</button>`;
// title and content
descriptionTitle.innerHTML = matchingObj.rule_title + ' <span class="edac-highlight-panel-description-type edac-highlight-panel-description-type-' + matchingObj.rule_type + '" aria-label="' + __( 'Issue type:', 'accessibility-checker' ) + ' ' + matchingObj.rule_type + '"> ' + matchingObj.rule_type + '</span>';
// content
descriptionContent.innerHTML = content;
// code object
// remove any non-html from the object
const htmlSnippet = matchingObj.object;
const parser = new DOMParser();
const parsedHtml = parser.parseFromString( htmlSnippet, 'text/html' );
const firstParsedElement = parsedHtml.body.firstElementChild;
if ( firstParsedElement ) {
descriptionCode.innerText = firstParsedElement.outerHTML;
} else {
const textNode = document.createTextNode( matchingObj.object );
descriptionCode.innerText = textNode.nodeValue;
}
// show fix settings button if available
if ( this.fixes[ matchingObj.slug ] && window.edacFrontendHighlighterApp?.userCanFix ) {
this.fixSettingsButton = document.querySelector( '.edac-fix-settings--button--open' );
this.fixSettingsButton.addEventListener( 'click', ( event ) => {
this.showFixSettings( event );
} );
this.fixSettingsButton.display = 'block';
this.fixSettingsSaveButton = document.querySelector( '.edac-fix-settings--button--save' );
this.fixSettingsSaveButton.addEventListener( 'click', ( event ) => {
saveFixSettings( event.target.closest( '.edac-fix-settings' ) );
} );
}
// set code button listener
this.codeContainer = document.querySelector( '.edac-highlight-panel-description-code' );
this.codeButton = document.querySelector( '.edac-highlight-panel-description-code-button' );
this.codeButton.addEventListener( 'click', () => this.codeToggle() );
// close the code container each time the description is opened
this.codeContainer.style.display = 'none';
// show the description
this.panelDescription.style.display = 'block';
}
}
/**
* This function closes the description.
*/
descriptionClose() {
this.panelDescription.style.display = 'none';
this.focusTrapControls();
}
/**
* This function disables all styles on the page.
*/
disableStyles() {
/*
If the site compiles css into a combined file, our method for disabling styles will cause out app's css to break.
This checks if the app's css is loading into #edac-app-css as expected.
If not, then we assume the css has been combined, so we manually add it to the document.
*/
if ( ! document.querySelector( '#edac-app-css' ) ) {
//console.log( 'css is combined, so adding app.css to page.' );
const link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.id = 'edac-app-css';
link.type = 'text/css';
link.href = edacFrontendHighlighterApp.appCssUrl;
link.media = 'all';
document.head.appendChild( link );
}
this.originalCss = Array.from( document.head.querySelectorAll( 'style[type="text/css"], style, link[rel="stylesheet"]' ) );
const elementsWithStyle = document.querySelectorAll( '*[style]:not([class^="edac"])' );
elementsWithStyle.forEach( function( element ) {
element.removeAttribute( 'style' );
} );
this.originalCss = this.originalCss.filter( function( element ) {
if ( element.id === 'edac-app-css' || element.id === 'dashicons-css' ) {
return false;
}
return true;
} );
document.head.dataset.css = this.originalCss;
this.originalCss.forEach( function( element ) {
element.remove();
} );
document.querySelector( 'body' ).classList.add( 'edac-app-disable-styles' );
this.stylesDisabled = true;
this.disableStylesButton.textContent = __( 'Enable Styles', 'accessibility-checker' );
}
/**
* This function enables all styles on the page.
*/
enableStyles() {
this.originalCss.forEach( function( element ) {
if ( element.tagName === 'STYLE' ) {
document.head.appendChild( element.cloneNode( true ) );
} else {
const newElement = document.createElement( 'link' );
newElement.rel = 'stylesheet';
newElement.href = element.href;
document.head.appendChild( newElement );
}
} );
document.querySelector( 'body' ).classList.remove( 'edac-app-disable-styles' );
this.stylesDisabled = false;
this.disableStylesButton.textContent = __( 'Disable Styles', 'accessibility-checker' );
}
/**
* * This function retrieves the value of a given URL parameter.
*
* @param {string} sParam The name of the URL parameter to be retrieved.
* @return {string | boolean} Returns the value of the URL parameter, or false if the parameter is not found.
*/
get_url_parameter( sParam ) {
const sPageURL = window.location.search.substring( 1 );
const sURLVariables = sPageURL.split( '&' );
let sParameterName, i;
for ( i = 0; i < sURLVariables.length; i++ ) {
sParameterName = sURLVariables[ i ].split( '=' );
if ( sParameterName[ 0 ] === sParam ) {
return sParameterName[ 1 ] === undefined ? true : decodeURIComponent( sParameterName[ 1 ] );
}
}
return false;
}
/**
* This function toggles the code container.
*/
codeToggle() {
if ( this.codeContainer.style.display === 'none' || this.codeContainer.style.display === '' ) {
this.codeContainer.style.display = 'block';
this.codeButton.setAttribute( 'aria-expanded', 'true' );
} else {
this.codeContainer.style.display = 'none';
this.codeButton.setAttribute( 'aria-expanded', 'false' );
}
}
showFixSettings( event ) {
const fixSettingsContainer = event.target.closest( '.edac-highlight-panel-description-content' ).querySelector( '.edac-fix-settings' );
if ( ! fixSettingsContainer ) {
// this is a fail, it should do something.
return;
}
const placeholder = document.createElement( 'span' );
placeholder.classList.add( 'edac-fix-settings--origin-placeholder' );
// put the placeholder AFTER the fix container.
fixSettingsContainer.parentNode.insertBefore( placeholder, fixSettingsContainer );
// renive the fixSettingsContainer from the DOM.
fixSettingsContainer.remove();
fillFixesModal(
`<p class="modal-opening-message">${ __( 'These settings enable global fixes across your entire site. Pages may need to be resaved or a full site scan run to see fixes reflected in reports.', 'accessibility-checker' ) }</p>`,
fixSettingsContainer
);
// pause the highlighter panel focus trap.
this.panelDescriptionFocusTrap.pause();
openFixesModal( event.target );
// unpause the focus trap when the modal is closed.
document.addEventListener( 'edac-fixes-modal-closed', () => {
this.panelDescriptionFocusTrap.unpause();
} );
}
/**
* This function counts the number of issues of a given type.
*
* @param {string} ruleType The type of issue to be counted.
* @return {number} The number of issues of a given type.
*/
countIssues( ruleType ) {
let count = 0;
for ( const issue of this.issues ) {
if ( issue.rule_type === ruleType ) {
count++;
}
}
return count;
}
/**
* This function counts the number of ignored issues.
*
* @return {number} The number of ignored issues.
*/
countIgnored() {
let count = 0;
for ( const issue of this.issues ) {
if ( issue.ignored === 1 ) {
count++;
}
}
return count;
}
/**
* This function shows the count of issues in the panel.
*/
showIssueCount() {
const errorCount = this.countIssues( 'error' );
const warningCount = this.countIssues( 'warning' );
const ignoredCount = this.countIgnored();
const div = document.querySelector( '.edac-highlight-panel-controls-summary' );
let textContent = __( 'No issues detected.', 'accessibility-checker' );
if ( errorCount > 0 || warningCount > 0 || ignoredCount > 0 ) {
textContent = '';
// show buttons since we have issues.
this.nextButton.disabled = false;
this.previousButton.disabled = false;
if ( errorCount >= 0 ) {
textContent += errorCount + ' ' + _n( 'error', 'errors', errorCount, 'accessibility-checker' ) + ', ';
}
if ( warningCount >= 0 ) {
textContent += warningCount + ' ' + _n( 'warning', 'warnings', warningCount, 'accessibility-checker' ) + ', ';
}
if ( ignoredCount >= 0 ) {
textContent += __( 'and', 'accessibility-checker' ) + ' ' + ignoredCount + ' ' + _n( 'ignored issue', 'ignored issues', ignoredCount, 'accessibility-checker' ) + ' ' + __( 'detected.', 'accessibility-checker' );
} else {
// Remove the trailing comma and add "detected."
textContent = textContent.slice( 0, -2 ) + ' ' + __( 'detected.', 'accessibility-checker' );
}
}
div.textContent = textContent;
}
/**
* Kick off the accessibility scan.
*/
kickoffScan() {
const getPageDensity = () => {
const elementCount = document.body.getElementsByTagName( '*' ).length;
const contentLength = document.body.innerText.length;
return { elementCount, contentLength };
};
const densityMetrics = getPageDensity();
const self = this;
const scriptId = 'edac-accessibility-checker-scanner-script';
if ( ! document.getElementById( scriptId ) ) {
const script = document.createElement( 'script' );
script.src = window.edacFrontendHighlighterApp?.scannerBundleUrl || '/wp-content/plugins/accessibility-checker/build/pageScanner.bundle.js';
script.id = scriptId;
script.onload = function() {
setTimeout( () => {
self._runScanOrShowError( densityMetrics );
}, 100 );
};
script.onerror = function() {
self.showWait( false );
self.showScanError( 'Failed to load scanner script.' );
};
document.head.appendChild( script );
} else {
self._runScanOrShowError( densityMetrics );
}
}
_runScanOrShowError( densityMetrics ) {
if ( window.runAccessibilityScan ) {
this.runAccessibilityScanAndSave( densityMetrics );
} else {
this.showWait( false );
this.showScanError( __( 'Scanner function not found.', 'accessibility-checker' ) );
}
}
runAccessibilityScanAndSave( densityMetrics ) {
const self = this;
const summary = document.querySelector( '.edac-highlight-panel-controls-summary' );
if ( summary ) {
summary.textContent = __( 'Scanning...', 'accessibility-checker' );
summary.classList.remove( 'edac-error' );
}
window.runAccessibilityScan().then( ( result ) => {
const postId = window.edacFrontendHighlighterApp && window.edacFrontendHighlighterApp.postID;
const nonce = window.edacFrontendHighlighterApp && window.edacFrontendHighlighterApp.restNonce;
if ( ! postId || ! nonce ) {
self.showWait( false );
self.showScanError( __( 'Missing postId or nonce.', 'accessibility-checker' ) );
return;
}
if ( ! result || ! result.violations || result.violations.length === 0 ) {
self.showWait( false );
self.showScanError( __( 'No violations found, skipping save.', 'accessibility-checker' ) );
return;
}
self.saveScanResults( postId, nonce, result.violations, densityMetrics );
} ).catch( () => {
self.showWait( false );
self.showScanError( __( 'Accessibility scan error.', 'accessibility-checker' ) );
} );
}
saveScanResults( postId, nonce, violations, densityMetrics ) {
const self = this;
fetch( '/wp-json/accessibility-checker/v1/post-scan-results/' + postId, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': nonce,
},
body: JSON.stringify( {
violations,
isSkipped: false,
isFailure: false,
densityMetrics,
} ),
} )
.then( ( response ) => response.json() )
.then( ( data ) => {
self.showWait( false );
if ( data && data.success ) {
// Optionally show a success message or update UI
} else {
self.showScanError( __( 'Saving failed.', 'accessibility-checker' ) );
}
} )
.catch( () => {
self.showWait( false );
self.showScanError( __( 'Error saving scan results.', 'accessibility-checker' ) );
} );
}