66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import Critters from 'critters ' ;
9+ import Beasties from 'beasties ' ;
1010import { readFile } from 'node:fs/promises' ;
1111
1212/**
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.
1414 */
1515const MEDIA_SET_HANDLER_PATTERN = / ^ t h i s \. m e d i a = [ " ' ] ( .* ) [ " ' ] ; ? $ / ;
1616
1717/**
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.
1919 */
2020const CSP_MEDIA_ATTR = 'ngCspMedia' ;
2121
@@ -58,8 +58,7 @@ const LINK_LOAD_SCRIPT_CONTENT = `
5858 };
5959
6060 documentElement.addEventListener('load', listener, true);
61- })();
62- ` . trim ( ) ;
61+ })();` ;
6362
6463export interface InlineCriticalCssProcessOptions {
6564 outputPath : string ;
@@ -98,14 +97,14 @@ interface PartialDocument {
9897/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
9998
10099// 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 {
103102 embedLinkedStylesheet ( link : PartialHTMLElement , document : PartialDocument ) : Promise < unknown > ;
104103}
105- class CrittersBase extends Critters { }
104+ class BeastiesBase extends Beasties { }
106105/* eslint-enable @typescript-eslint/no-unsafe-declaration-merging */
107106
108- class CrittersExtended extends CrittersBase {
107+ class BeastiesExtended extends BeastiesBase {
109108 readonly warnings : string [ ] = [ ] ;
110109 readonly errors : string [ ] = [ ] ;
111110 private addedCspScriptsDocuments = new WeakSet < PartialDocument > ( ) ;
@@ -143,7 +142,7 @@ class CrittersExtended extends CrittersBase {
143142 }
144143
145144 /**
146- * Override of the Critters `embedLinkedStylesheet` method
145+ * Override of the Beasties `embedLinkedStylesheet` method
147146 * that makes it work with Angular's CSP APIs.
148147 */
149148 override async embedLinkedStylesheet (
@@ -165,19 +164,19 @@ class CrittersExtended extends CrittersBase {
165164 const cspNonce = this . findCspNonce ( document ) ;
166165
167166 if ( cspNonce ) {
168- const crittersMedia = link . getAttribute ( 'onload' ) ?. match ( MEDIA_SET_HANDLER_PATTERN ) ;
167+ const beastiesMedia = link . getAttribute ( 'onload' ) ?. match ( MEDIA_SET_HANDLER_PATTERN ) ;
169168
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,
172171 // we have to remove the handler, because it's incompatible with CSP. We save the value
173172 // in a different attribute and we generate a script tag with the nonce that uses
174173 // `addEventListener` to apply the media query instead.
175174 link . removeAttribute ( 'onload' ) ;
176- link . setAttribute ( CSP_MEDIA_ATTR , crittersMedia [ 1 ] ) ;
175+ link . setAttribute ( CSP_MEDIA_ATTR , beastiesMedia [ 1 ] ) ;
177176 this . conditionallyInsertCspLoadingScript ( document , cspNonce , link ) ;
178177 }
179178
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
181180 // a way of doing that at the moment so we fall back to doing it any time a `link` tag is
182181 // inserted. We mitigate it by only iterating the direct children of the `<head>` which
183182 // should be pretty shallow.
@@ -200,7 +199,7 @@ class CrittersExtended extends CrittersBase {
200199 return this . documentNonces . get ( document ) ! ;
201200 }
202201
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.
204203 const nonceElement = document . querySelector ( '[ngCspNonce], [ngcspnonce]' ) ;
205204 const cspNonce =
206205 nonceElement ?. getAttribute ( 'ngCspNonce' ) || nonceElement ?. getAttribute ( 'ngcspnonce' ) || null ;
@@ -240,16 +239,16 @@ export class InlineCriticalCssProcessor {
240239 html : string ,
241240 options : InlineCriticalCssProcessOptions ,
242241 ) : 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 ) ;
245244
246245 return {
247246 // Clean up value from value less attributes.
248247 // This is caused because parse5 always requires attributes to have a string value.
249248 // nomodule="" defer="" -> nomodule defer.
250249 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 ,
253252 } ;
254253 }
255254}
0 commit comments