-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzz.js
225 lines (206 loc) · 9.31 KB
/
fuzz.js
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
const fontSnapper = require('../lib/fontSnapper');
const normalizeFontWeight = require('../lib/normalizeFontWeight');
const normalizeFontStretch = require('../lib/normalizeFontStretch');
const { atRule, namedSyntax } = require('css-generators');
const { pickone, shape, array } = require('chance-generators');
const postcss = require('postcss');
const _ = require('lodash');
const expect = require('unexpected')
.clone()
.use(require('unexpected-check'));
const fontFamilyGenerator = pickone(['foo', 'bar']);
const fontFaceDeclarationGenerator = atRule({ type: 'font-face' }).map(
atRule => {
const fontFaceDeclaration = {
'font-family': fontFamilyGenerator
};
const ast = postcss.parse(atRule);
for (const node of ast.nodes[0].nodes) {
if (['font-weight', 'font-stretch', 'font-style'].includes(node.prop)) {
fontFaceDeclaration[node.prop] = node.value;
}
}
return fontFaceDeclaration;
}
);
const computedStyleGenerator = shape({
'font-family': fontFamilyGenerator,
'font-style': namedSyntax('font-style'),
'font-weight': namedSyntax('font-weight'),
'font-stretch': namedSyntax('font-stretch')
});
const inputs = shape({
fontFaceDeclarations: array(fontFaceDeclarationGenerator, { min: 2, max: 8 }),
propsToSnap: computedStyleGenerator
});
function stringifyCssProps(props) {
return Object.keys(props)
.map(propertyName => `${propertyName}: ${props[propertyName]};`)
.join('\n');
}
function indent(text, indentation = ' ') {
return text.replace(/^/gm, indentation);
}
function stringifyFontFaceDeclaration(fontFaceDeclaration) {
return `@font-face {\n${indent(stringifyCssProps(fontFaceDeclaration))}\n}`;
}
let browser;
async function getBrowser() {
if (!browser) {
browser = await require('puppeteer').launch();
after(async function() {
await browser.close();
});
}
return browser;
}
// Contains "foo":
const font = Buffer.from(
'd09GMgABAAAAAAiIABEAAAAADrAAAAgsAAEAAQAAAAAAAAAAAAAAAAAAAAAAAAAAGhYbEBwQBmAANAiBKgmPYBEMCoIMggwBNgIkAwwLCAAEIAWCegcgDIF4G4MNUZSu0SrER2LsNhqiIVaKDxiEIu5YoGji4f/vx7fPffEjEos+WA9QBbaNjSVVUIMn9fIEmqbQTy9+eqf6TjKF5QCpqU+9OksMgbrlgAxSnDlA2qRkIKV25pTR3Vhphn9IX30aph3+IsDl2vs2e3cFJllhq2yNSTalPOJcCXF8p+MrTIVh/GCBJRrxF2uuns1zlUgVeFFEoBfUJgEIopuk9m+t6fasGEivFTqLB0UMxIJ7g+yrE1F1ptTTWWZFnlIuXolfdG1jPAnUjK3lQjPqws75pFPkKeTH3a+wiEfIhheVW8oq5YIJt7Qtxbb2YXuvi8RyVJ29eFUgu3cXz0JUz0iZB0H7b5EPbEa54FFj1f32b3Or/d68dvs/3OyHIZ2u+2M3xKqXPQvO7v1f8t83z4iX3iv3TU0aujYxPjY6Mjw0OJA5vT/dpyqpZCIu9/Z0d61bu2b1qpUrOjva26KRltOag0vZEilQX+0XKsrLSoqLfP+n/VgkpoJ8kPpViynMSkcjVKnPp+7FFaaaSC2K3a5mlk632pgViSmandX6IjPIJc1ocoA8hAi0C7p6OZJRfDbF6AKZHNWrZD7FoPitMsiE+BPK7JJk+cBwwLT8GFT35G3FTEUjxCkpTrJkrhiSwCkuESVh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);
const initialValueByProp = {
'font-stretch': 'normal',
'font-weight': 'normal',
'font-style': 'normal'
};
function normalizeFontFaceDeclaration(fontFaceDeclaration) {
const normalized = {};
for (const propertyName of Object.keys(fontFaceDeclaration)) {
let value = fontFaceDeclaration[propertyName];
if (propertyName === 'font-stretch') {
value = String(value)
.split(/\s+/)
.map(normalizeFontStretch)
.join(' ');
} else if (propertyName === 'font-weight') {
value = String(value)
.split(/\s+/)
.map(normalizeFontWeight)
.join(' ');
}
normalized[propertyName] = value;
}
return normalized;
}
function areFontFaceDeclarationsEquivalent(a, b) {
return _.isEqual(
normalizeFontFaceDeclaration({ ...initialValueByProp, ...a }),
normalizeFontFaceDeclaration({ ...initialValueByProp, ...b })
);
}
async function renderPage(html) {
const browser = await getBrowser();
const page = await browser.newPage();
await page.setRequestInterception(true);
const loadedFonts = [];
page.on('request', request => {
const url = request.url();
if (url.endsWith('.woff2')) {
loadedFonts.push(url);
request.respond({
status: 200,
contentType: 'font/woff2',
body: font
});
return;
} else if (url === 'https://example.com/') {
request.respond({
status: 200,
contentType: 'text/html',
body: html
});
return;
}
request.continue();
});
await page.goto('https://example.com/');
return loadedFonts;
}
describe('font-snapper', function() {
it('should not crash on any valid input', function() {
expect(
({ fontFaceDeclarations, propsToSnap }) => {
fontSnapper(fontFaceDeclarations, propsToSnap);
},
'to be valid for all',
inputs
);
});
it('should snap to the same font as Chrome', async function() {
this.timeout(300000);
await expect(
async ({ fontFaceDeclarations, propsToSnap }) => {
// Remove some features that font-snapper doesn't support yet:
if (/oblique/.test(propsToSnap['font-style'])) {
propsToSnap['font-style'] = 'oblique';
}
for (const fontFaceDeclaration of fontFaceDeclarations) {
// Right now font-snapper only supports numerical font-weight values of 100, 200, ... 900
// but css-generators also puts out values that aren't multiples of 100 as per CSS Fonts Module Level 4:
// https://www.w3.org/TR/css-fonts-4/#font-weight-numeric-values
if (/^\d+$/.test(fontFaceDeclaration['font-weight'])) {
let fontWeight = parseInt(fontFaceDeclaration['font-weight']);
fontWeight -= fontWeight % 100;
if (fontWeight < 100 || fontWeight > 900) {
fontWeight = 400;
}
fontFaceDeclaration['font-weight'] = String(fontWeight);
}
if (/oblique/.test(fontFaceDeclaration['font-style'])) {
fontFaceDeclaration['font-style'] = 'oblique';
}
}
// Remove duplicate @font-face declarations so there's always a correct choice:
for (let i = 1; i < fontFaceDeclarations.length; i += 1) {
const fontFaceDeclaration = fontFaceDeclarations[i];
if (
fontFaceDeclarations
.slice(0, i)
.some(prev =>
areFontFaceDeclarationsEquivalent(prev, fontFaceDeclaration)
)
) {
fontFaceDeclarations.splice(i, 1);
i -= 1;
}
}
for (const [i, fontFaceDeclaration] of fontFaceDeclarations.entries()) {
fontFaceDeclaration.i = i;
}
const snapped = fontSnapper(fontFaceDeclarations, propsToSnap);
for (const [i, fontFaceDeclaration] of fontFaceDeclarations.entries()) {
fontFaceDeclaration.src = `url(${
snapped && fontFaceDeclaration.i === snapped.i
? 'correct'
: `wrong${i}`
}.woff2)`;
}
const html = `<!DOCTYPE html>
<html>
<head>
<style>
${indent(
fontFaceDeclarations.map(stringifyFontFaceDeclaration).join('\n\n'),
' '
)}
body {
${indent(stringifyCssProps(propsToSnap), ' ')}
}
</style>
</head>
<body>foo</body>
</html>
`;
const loadedFonts = await renderPage(html);
if (snapped) {
expect(loadedFonts, 'to equal', [
`https://example.com/correct.woff2`
]);
}
},
'to be valid for all',
inputs
);
});
});