6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import Critters from 'critters ' ;
9
+ import Beasties from 'beasties ' ;
10
10
import { readFile } from 'node:fs/promises' ;
11
11
12
12
/**
13
- * Pattern used to extract the media query set by Critters in an `onload` handler.
13
+ * Pattern used to extract the media query set by Beasties in an `onload` handler.
14
14
*/
15
15
const MEDIA_SET_HANDLER_PATTERN = / ^ t h i s \. m e d i a = [ " ' ] ( .* ) [ " ' ] ; ? $ / ;
16
16
17
17
/**
18
- * Name of the attribute used to save the Critters media query so it can be re-assigned on load.
18
+ * Name of the attribute used to save the Beasties media query so it can be re-assigned on load.
19
19
*/
20
20
const CSP_MEDIA_ATTR = 'ngCspMedia' ;
21
21
@@ -58,8 +58,7 @@ const LINK_LOAD_SCRIPT_CONTENT = `
58
58
};
59
59
60
60
documentElement.addEventListener('load', listener, true);
61
- })();
62
- ` . trim ( ) ;
61
+ })();` ;
63
62
64
63
export interface InlineCriticalCssProcessOptions {
65
64
outputPath : string ;
@@ -98,14 +97,14 @@ interface PartialDocument {
98
97
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
99
98
100
99
// We use Typescript declaration merging because `embedLinkedStylesheet` it's not declared in
101
- // the `Critters ` types which means that we can't call the `super` implementation.
102
- interface CrittersBase {
100
+ // the `Beasties ` types which means that we can't call the `super` implementation.
101
+ interface BeastiesBase {
103
102
embedLinkedStylesheet ( link : PartialHTMLElement , document : PartialDocument ) : Promise < unknown > ;
104
103
}
105
- class CrittersBase extends Critters { }
104
+ class BeastiesBase extends Beasties { }
106
105
/* eslint-enable @typescript-eslint/no-unsafe-declaration-merging */
107
106
108
- class CrittersExtended extends CrittersBase {
107
+ class BeastiesExtended extends BeastiesBase {
109
108
readonly warnings : string [ ] = [ ] ;
110
109
readonly errors : string [ ] = [ ] ;
111
110
private addedCspScriptsDocuments = new WeakSet < PartialDocument > ( ) ;
@@ -143,7 +142,7 @@ class CrittersExtended extends CrittersBase {
143
142
}
144
143
145
144
/**
146
- * Override of the Critters `embedLinkedStylesheet` method
145
+ * Override of the Beasties `embedLinkedStylesheet` method
147
146
* that makes it work with Angular's CSP APIs.
148
147
*/
149
148
override async embedLinkedStylesheet (
@@ -165,19 +164,19 @@ class CrittersExtended extends CrittersBase {
165
164
const cspNonce = this . findCspNonce ( document ) ;
166
165
167
166
if ( cspNonce ) {
168
- const crittersMedia = link . getAttribute ( 'onload' ) ?. match ( MEDIA_SET_HANDLER_PATTERN ) ;
167
+ const beastiesMedia = link . getAttribute ( 'onload' ) ?. match ( MEDIA_SET_HANDLER_PATTERN ) ;
169
168
170
- if ( crittersMedia ) {
171
- // If there's a Critters -generated `onload` handler and the file has an Angular CSP nonce,
169
+ if ( beastiesMedia ) {
170
+ // If there's a Beasties -generated `onload` handler and the file has an Angular CSP nonce,
172
171
// we have to remove the handler, because it's incompatible with CSP. We save the value
173
172
// in a different attribute and we generate a script tag with the nonce that uses
174
173
// `addEventListener` to apply the media query instead.
175
174
link . removeAttribute ( 'onload' ) ;
176
- link . setAttribute ( CSP_MEDIA_ATTR , crittersMedia [ 1 ] ) ;
175
+ link . setAttribute ( CSP_MEDIA_ATTR , beastiesMedia [ 1 ] ) ;
177
176
this . conditionallyInsertCspLoadingScript ( document , cspNonce , link ) ;
178
177
}
179
178
180
- // Ideally we would hook in at the time Critters inserts the `style` tags, but there isn't
179
+ // Ideally we would hook in at the time Beasties inserts the `style` tags, but there isn't
181
180
// a way of doing that at the moment so we fall back to doing it any time a `link` tag is
182
181
// inserted. We mitigate it by only iterating the direct children of the `<head>` which
183
182
// should be pretty shallow.
@@ -200,7 +199,7 @@ class CrittersExtended extends CrittersBase {
200
199
return this . documentNonces . get ( document ) ! ;
201
200
}
202
201
203
- // HTML attribute are case-insensitive, but the parser used by Critters is case-sensitive.
202
+ // HTML attribute are case-insensitive, but the parser used by Beasties is case-sensitive.
204
203
const nonceElement = document . querySelector ( '[ngCspNonce], [ngcspnonce]' ) ;
205
204
const cspNonce =
206
205
nonceElement ?. getAttribute ( 'ngCspNonce' ) || nonceElement ?. getAttribute ( 'ngcspnonce' ) || null ;
@@ -240,16 +239,16 @@ export class InlineCriticalCssProcessor {
240
239
html : string ,
241
240
options : InlineCriticalCssProcessOptions ,
242
241
) : Promise < { content : string ; warnings : string [ ] ; errors : string [ ] } > {
243
- const critters = new CrittersExtended ( { ...this . options , ...options } ) ;
244
- const content = await critters . process ( html ) ;
242
+ const beasties = new BeastiesExtended ( { ...this . options , ...options } ) ;
243
+ const content = await beasties . process ( html ) ;
245
244
246
245
return {
247
246
// Clean up value from value less attributes.
248
247
// This is caused because parse5 always requires attributes to have a string value.
249
248
// nomodule="" defer="" -> nomodule defer.
250
249
content : content . replace ( / ( \s (?: d e f e r | n o m o d u l e ) ) = " " / g, '$1' ) ,
251
- errors : critters . errors ,
252
- warnings : critters . warnings ,
250
+ errors : beasties . errors ,
251
+ warnings : beasties . warnings ,
253
252
} ;
254
253
}
255
254
}
0 commit comments