-
Notifications
You must be signed in to change notification settings - Fork 820
Expand file tree
/
Copy pathhint.ts
More file actions
212 lines (170 loc) · 7.69 KB
/
Copy pathhint.ts
File metadata and controls
212 lines (170 loc) · 7.69 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
/**
* @fileoverview Check if a single `<meta name="theme-color">` is
* specified in the `<head>`.
*/
/*
* ------------------------------------------------------------------------------
* Requirements
* ------------------------------------------------------------------------------
*/
import { get as parseColor, ColorDescriptor } from 'color-string';
import {
ElementFound,
HintContext,
IHint,
TraverseEnd
} from 'hint';
import { Severity } from '@hint/utils-types';
import { normalizeString } from '@hint/utils-string';
import { HTMLDocument, HTMLElement } from '@hint/utils-dom';
import { isSupported } from '@hint/utils-compat-data';
import meta from './meta';
import { getMessage } from './i18n.import';
/*
* ------------------------------------------------------------------------------
* Public
* ------------------------------------------------------------------------------
*/
export default class MetaThemeColorHint implements IHint {
public static readonly meta = meta;
public constructor(context: HintContext) {
let bodyElementWasReached: boolean = false;
let themeColorElementsAndMedia: Map<string, HTMLElement> = new Map();
const checkIfThemeColorMetaElementWasSpecified = (event: TraverseEnd) => {
const pageDOM = context.pageDOM as HTMLDocument;
const { resource } = event;
const linksToManifest = pageDOM.querySelectorAll('link[rel="manifest"]').length > 0;
if (themeColorElementsAndMedia.size === 0 && linksToManifest) {
context.report(
resource,
getMessage('metaElementNotSpecified', context.language),
{ severity: Severity.warning });
}
};
const isNotSupportedColorValue = (color: ColorDescriptor, normalizedColorValue: string): boolean => {
const hexWithAlphaRegex = /^#([0-9a-fA-F]{4}){1,2}$/;
/*
* `theme-color` can accept any CSS `<color>`:
*
* * https://html.spec.whatwg.org/multipage/semantics.html#meta-theme-color
* * https://drafts.csswg.org/css-color/#typedef-color
*
* However, `HWB` and `hex with alpha` values are not
* supported everywhere `theme-color` is. Also, values
* such as `currentcolor` don't make sense, but they
* will be catched by the above check.
*
* See also:
*
* * https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#Browser_compatibility
* * https://cs.chromium.org/chromium/src/third_party/WebKit/Source/platform/graphics/Color.cpp?rcl=6263bcf0ec9f112b5f0d84fc059c759302bd8c67
*/
// TODO: Use `isSupported` for all color syntax checks.
// `RGBA` support depends on the browser.
return (color.model === 'rgb' &&
hexWithAlphaRegex.test(normalizedColorValue) &&
!isSupported({ property: 'color', value: '#00000000' }, context.targetedBrowsers)) ||
// `HWB` is not supported anywhere (?).
color.model === 'hwb';
};
const checkContentAttributeValue = (resource: string, element: HTMLElement) => {
const contentValue = element.getAttribute('content')!;
const normalizedContentValue = normalizeString(contentValue, '')!; // Will not be null because default was provided.
const color = parseColor(normalizedContentValue);
if (color === null) {
const message = getMessage('metaElementInvalidContent', context.language);
context.report(
resource,
message,
{
element,
severity: Severity.error
});
return;
}
if (isNotSupportedColorValue(color, normalizedContentValue)) {
const message = getMessage('metaElementUnsupported', context.language);
context.report(
resource,
message,
{
element,
severity: Severity.error
});
}
};
const checkNameAttributeValue = (resource: string, element: HTMLElement) => {
/*
* Something such as `name=" theme-color"` is not valid,
* but if used, the user probably wanted `name="theme-color"`.
*
* From: https://html.spec.whatwg.org/multipage/semantics.html#meta-theme-color
*
* " The element has a name attribute, whose value is
* an ASCII case-insensitive match for `theme-color` "
*/
const nameAttributeValue = element.getAttribute('name');
if (nameAttributeValue && nameAttributeValue !== nameAttributeValue.trim()) {
const message = getMessage('metaElementInvalidName', context.language);
context.report(
resource,
message,
{
element,
severity: Severity.warning
});
}
};
const validate = ({ element, resource }: ElementFound) => {
// Check if it's a `theme-color` meta element.
if (normalizeString(element.getAttribute('name')) !== 'theme-color') {
return;
}
/*
* Check if a `theme-color` meta element with the same media attribute was already specified.
*
* Multiple theme-color meta elements are allowed if they have different media attributes.
* From MDN: "Most meta properties can be used only once. However, theme-color can be
* used multiple times if unique media values are provided."
*
* References:
* - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name/theme-color
* - https://html.spec.whatwg.org/multipage/semantics.html#meta-theme-color
*/
const mediaAttributeValue = normalizeString(element.getAttribute('media'), '');
const mediaKey = mediaAttributeValue || 'default'; // Use 'default' for elements without media attribute
if (themeColorElementsAndMedia.has(mediaKey)) {
context.report(
resource,
getMessage('metaElementDuplicated', context.language),
{
element,
severity: Severity.warning
});
return;
}
themeColorElementsAndMedia.set(mediaKey, element);
// Check if the `theme-color` meta element:
// * was specified in the `<body>`
if (bodyElementWasReached) {
context.report(
resource,
getMessage('metaElementInBody', context.language),
{
element,
severity: Severity.error
});
return;
}
// * has a valid `name` attribute value
checkNameAttributeValue(resource, element);
// * has a valid color value that is also supported
checkContentAttributeValue(resource, element);
};
context.on('element::meta', validate);
context.on('element::body', () => {
bodyElementWasReached = true;
});
context.on('traverse::end', checkIfThemeColorMetaElementWasSpecified);
}
}