-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhas-ambiguous-text.js
More file actions
66 lines (58 loc) · 2 KB
/
Copy pathhas-ambiguous-text.js
File metadata and controls
66 lines (58 loc) · 2 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
import { __ } from '@wordpress/i18n';
const ambiguousPhrases = [
__( 'click', 'accessibility-checker' ),
__( 'click here', 'accessibility-checker' ),
__( 'here', 'accessibility-checker' ),
__( 'go here', 'accessibility-checker' ),
__( 'more', 'accessibility-checker' ),
__( 'more...', 'accessibility-checker' ),
__( 'more…', 'accessibility-checker' ),
__( 'details', 'accessibility-checker' ),
__( 'more details', 'accessibility-checker' ),
__( 'link', 'accessibility-checker' ),
__( 'this page', 'accessibility-checker' ),
__( 'continue', 'accessibility-checker' ),
__( 'continue reading', 'accessibility-checker' ),
__( 'read more', 'accessibility-checker' ),
__( 'open', 'accessibility-checker' ),
__( 'download', 'accessibility-checker' ),
__( 'button', 'accessibility-checker' ),
__( 'keep reading', 'accessibility-checker' ),
__( 'learn more', 'accessibility-checker' ),
__( 'opens a new window', 'accessibility-checker' ),
];
const normalizedPhrases = ambiguousPhrases.map(
( p ) => p.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim()
);
const checkAmbiguousPhrase = ( text ) => {
if ( ! text ) {
return false;
}
text = text.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim();
return normalizedPhrases.includes( text );
};
export default {
id: 'has_ambiguous_text',
evaluate: ( node ) => {
if ( node.hasAttribute( 'aria-label' ) ) {
const ariaLabel = node.getAttribute( 'aria-label' );
return checkAmbiguousPhrase( ariaLabel );
}
if ( node.hasAttribute( 'aria-labelledby' ) ) {
const label = node.getAttribute( 'aria-labelledby' );
const labelText = document.getElementById( label )?.textContent;
return checkAmbiguousPhrase( labelText );
}
if ( node.textContent && node.textContent !== '' ) {
return checkAmbiguousPhrase( node.textContent );
}
const images = node.querySelectorAll( 'img' );
for ( const image of images ) {
const altText = image.getAttribute( 'alt' );
if ( checkAmbiguousPhrase( altText ) ) {
return true;
}
}
return false;
},
};