forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline-critical-css_spec.ts
186 lines (162 loc) · 5.47 KB
/
inline-critical-css_spec.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { tags } from '@angular-devkit/core';
import { InlineCriticalCssProcessor } from './inline-critical-css';
describe('InlineCriticalCssProcessor', () => {
const styles: Record<string, string> = {
'/dist/styles.css': `
body { margin: 0; }
html { color: white; }
`,
'/dist/theme.css': `
span { color: blue; }
p { color: blue; }
`,
};
const readAsset = async (file: string): Promise<string> => {
const content = styles[file];
if (content) {
return content;
}
throw new Error('Cannot read asset.');
};
const getContent = (deployUrl: string, bodyContent = ''): string => {
return `
<html>
<head>
<link href="${deployUrl}styles.css" rel="stylesheet">
<link href="${deployUrl}theme.css" rel="stylesheet">
</head>
<body>${bodyContent}</body>
</html>`;
};
it('should inline critical css', async () => {
const inlineFontsProcessor = new InlineCriticalCssProcessor({
readAsset,
});
const { content } = await inlineFontsProcessor.process(getContent(''), {
outputPath: '/dist/',
});
expect(content).toContain(
`<link href="styles.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain(
`<link href="theme.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).not.toContain('color: blue');
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<style>
body { margin: 0; }
html { color: white; }
</style>`);
});
it('should inline critical css when using deployUrl', async () => {
const inlineFontsProcessor = new InlineCriticalCssProcessor({
readAsset,
deployUrl: 'http://cdn.com',
});
const { content } = await inlineFontsProcessor.process(getContent('http://cdn.com/'), {
outputPath: '/dist/',
});
expect(content).toContain(
`<link href="http://cdn.com/styles.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain(
`<link href="http://cdn.com/theme.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<style>
body { margin: 0; }
html { color: white; }
</style>`);
});
it('should compress inline critical css when minify is enabled', async () => {
const inlineFontsProcessor = new InlineCriticalCssProcessor({
readAsset,
minify: true,
});
const { content } = await inlineFontsProcessor.process(getContent(''), {
outputPath: '/dist/',
});
expect(content).toContain(
`<link href="styles.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain(
`<link href="theme.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain('<style>body{margin:0}html{color:white}</style>');
});
it(`should process the inline 'onload' handlers if a 'autoCsp' is true`, async () => {
const inlineCssProcessor = new InlineCriticalCssProcessor({
readAsset,
autoCsp: true,
});
const { content } = await inlineCssProcessor.process(getContent(''), {
outputPath: '/dist/',
});
expect(content).toContain(
'<link href="styles.css" rel="stylesheet" media="print" ngCspMedia="all">',
);
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<style>
body { margin: 0; }
html { color: white; }
</style>`);
});
it('should process the inline `onload` handlers if a CSP nonce is specified', async () => {
const inlineCssProcessor = new InlineCriticalCssProcessor({
readAsset,
});
const { content } = await inlineCssProcessor.process(
getContent('', '<app ngCspNonce="{% nonce %}"></app>'),
{
outputPath: '/dist/',
},
);
expect(content).toContain(
'<link href="styles.css" rel="stylesheet" media="print" ngCspMedia="all">',
);
expect(content).toContain(
'<link href="theme.css" rel="stylesheet" media="print" ngCspMedia="all">',
);
// Nonces shouldn't be added inside the `noscript` tags.
expect(content).toContain('<noscript><link href="theme.css" rel="stylesheet"></noscript>');
expect(content).toContain('<script nonce="{% nonce %}">');
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<style nonce="{% nonce %}">
body { margin: 0; }
html { color: white; }
</style>`);
});
it('should not modify the document for external stylesheets', async () => {
const inlineCssProcessor = new InlineCriticalCssProcessor({
readAsset,
});
const initialContent = `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://google.com/styles.css" />
</head>
<body>
<app ngCspNonce="{% nonce %}"></app>
</body>
</html>
`;
const { content } = await inlineCssProcessor.process(initialContent, {
outputPath: '/dist/',
});
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://google.com/styles.css">
</head>
`);
});
});