-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathlink-text.js
More file actions
229 lines (215 loc) · 6.4 KB
/
link-text.js
File metadata and controls
229 lines (215 loc) · 6.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
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {Audit} from '../audit.js';
import UrlUtils from '../../lib/url-utils.js';
import * as i18n from '../../lib/i18n/i18n.js';
/** @type {Record<string, Set<string>>} */
const nonDescriptiveLinkTexts = {
// English
'en': new Set([
'click here',
'click this',
'go',
'here',
'information',
'learn more',
'more',
'more info',
'more information',
'right here',
'read more',
'see more',
'start',
'this',
]),
// Japanese
'ja': new Set([
'ここをクリック',
'こちらをクリック',
'リンク',
'続きを読む',
'続く',
'全文表示',
]),
// Spanish
'es': new Set([
'click aquí',
'click aqui',
'clicka aquí',
'clicka aqui',
'pincha aquí',
'pincha aqui',
'aquí',
'aqui',
'más',
'mas',
'más información',
'más informacion',
'mas información',
'mas informacion',
'este',
'enlace',
'este enlace',
'empezar',
]),
// Portuguese
'pt': new Set([
'clique aqui',
'ir',
'mais informação',
'mais informações',
'mais',
'veja mais',
]),
// Korean
'ko': new Set([
'여기',
'여기를 클릭',
'클릭',
'링크',
'자세히',
'자세히 보기',
'계속',
'이동',
'전체 보기',
]),
// Swedish
'sv': new Set([
'här',
'klicka här',
'läs mer',
'mer',
'mer info',
'mer information',
]),
// German
'de': new Set([
'klicke hier',
'hier klicken',
'hier',
'mehr',
'siehe',
'dies',
'das',
'weiterlesen',
]),
// Tamil
'ta': new Set([
'அடுத்த பக்கம்',
'மறுபக்கம்',
'முந்தைய பக்கம்',
'முன்பக்கம்',
'மேலும் அறிக',
'மேலும் தகவலுக்கு',
'மேலும் தரவுகளுக்கு',
'தயவுசெய்து இங்கே அழுத்தவும்',
'இங்கே கிளிக் செய்யவும்',
]),
// Persian
'fa': new Set([
'اطلاعات بیشتر',
'اطلاعات',
'این',
'اینجا بزنید',
'اینجا کلیک کنید',
'اینجا',
'برو',
'بیشتر بخوانید',
'بیشتر بدانید',
'بیشتر',
'شروع',
]),
};
const UIStrings = {
/** Title of a Lighthouse audit that tests if each link on a page contains a sufficient description of what a user will find when they click it. Generic, non-descriptive text like "click here" doesn't give an indication of what the link leads to. This descriptive title is shown when all links on the page have sufficient textual descriptions. */
title: 'Links have descriptive text',
/** Title of a Lighthouse audit that tests if each link on a page contains a sufficient description of what a user will find when they click it. Generic, non-descriptive text like "click here" doesn't give an indication of what the link leads to. This descriptive title is shown when one or more links on the page contain generic, non-descriptive text. */
failureTitle: 'Links do not have descriptive text',
/** Description of a Lighthouse audit that tells the user *why* they need to have descriptive text on the links in their page. This is displayed after a user expands the section to see more. No character length limits. The last sentence starting with 'Learn' becomes link text to additional documentation. */
description: 'Descriptive link text helps search engines understand your content. ' +
'[Learn how to make links more accessible](https://developer.chrome.com/docs/lighthouse/seo/link-text/).',
/** [ICU Syntax] Label for the audit identifying the number of links found. "link" here refers to the links in a web page to other web pages. */
displayValue: `{itemCount, plural,
=1 {1 link found}
other {# links found}
}`,
};
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
class LinkText extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'link-text',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['URL', 'AnchorElements'],
};
}
/**
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
const failingLinks = artifacts.AnchorElements
.filter(link => {
if (!link.href || link.rel.includes('nofollow')) return false;
const href = link.href.toLowerCase();
if (
href.startsWith('javascript:') ||
href.startsWith('mailto:') ||
// This line prevents the audit from flagging anchor links.
// In this case it is better to use `finalDisplayedUrl` than `mainDocumentUrl`.
UrlUtils.equalWithExcludedFragments(link.href, artifacts.URL.finalDisplayedUrl)
) {
return false;
}
const searchTerm = link.text.trim().toLowerCase();
if (searchTerm) {
// Use language if detected, otherwise look at everything.
if (link.textLang) {
const lang = link.textLang.split('-')[0];
if (nonDescriptiveLinkTexts[lang] && nonDescriptiveLinkTexts[lang].has(searchTerm)) {
return true;
}
} else {
for (const texts of Object.values(nonDescriptiveLinkTexts)) {
if (texts.has(searchTerm)) {
return true;
}
}
}
}
return false;
})
.map(link => {
return {
href: link.href,
text: link.text.trim(),
textLang: link.textLang,
};
});
/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'href', valueType: 'url', label: 'Link destination'},
{key: 'text', valueType: 'text', label: 'Link Text'},
];
const details = Audit.makeTableDetails(headings, failingLinks);
let displayValue;
if (failingLinks.length) {
displayValue = str_(UIStrings.displayValue, {itemCount: failingLinks.length});
}
return {
score: Number(failingLinks.length === 0),
details,
displayValue,
};
}
}
export default LinkText;
export {UIStrings};