@@ -113,11 +113,31 @@ export function updateStylesWithSCS(
113
113
return promises ;
114
114
}
115
115
116
+ /**
117
+ * Cache of promises per style elements.
118
+ *
119
+ * Each style element has their own associated `Promise` that resolves
120
+ * once the element has been loaded and is ready.
121
+ */
116
122
const stylePromiseCache = new WeakMap <
117
123
StyleElement ,
118
124
Promise < StyleElement >
119
125
> ( ) ;
120
126
127
+ /**
128
+ * Prepare and return the corresponding `Promise` for the passed style
129
+ * element.
130
+ *
131
+ * It returns the cached promise if it exists. Otherwise, constructs
132
+ * a `Promise` that resolves once the element has finished loading.
133
+ *
134
+ * For those elements that are not in the DOM yet, this function
135
+ * injects a `media="preload"` attribute to the passed element so the
136
+ * style is loaded without applying any styles to the document.
137
+ *
138
+ * @param element Style element.
139
+ * @return The associated `Promise` to the passed element.
140
+ */
121
141
const prepareStylePromise = (
122
142
element : StyleElement
123
143
) : Promise < StyleElement > => {
@@ -159,8 +179,32 @@ const prepareStylePromise = (
159
179
return promise ;
160
180
} ;
161
181
182
+ /**
183
+ * Cache of style promise lists per URL.
184
+ *
185
+ * It contains the list of style elements associated to the page with the
186
+ * passed URL. The original order is preserved to respect the CSS cascade.
187
+ *
188
+ * Each included promise resolves when the associated style element is ready.
189
+ */
162
190
const styleSheetCache = new Map < string , Promise < StyleElement > [ ] > ( ) ;
163
191
192
+ /**
193
+ * Prepare all style elements contained in the passed document.
194
+ *
195
+ * This function calls {@link updateStylesWithSCS|`updateStylesWithSCS`}
196
+ * to insert only the minimum amount of style elements into the DOM, so
197
+ * those present in the passed document end up in the DOM while the order
198
+ * is respected.
199
+ *
200
+ * New appended style elements contain a `media=prefetch` attribute to
201
+ * make them effectively disabled until they are applied with the
202
+ * {@link applyStyles|`applyStyles`} function.
203
+ *
204
+ * @param doc Document instance.
205
+ * @param url URL for the passed document.
206
+ * @return A list of promises for each style element in the passed document.
207
+ */
164
208
export const prepareStyles = (
165
209
doc : Document ,
166
210
url : string = ( doc . location || window . location ) . href
@@ -186,6 +230,15 @@ export const prepareStyles = (
186
230
return styleSheetCache . get ( url ) ;
187
231
} ;
188
232
233
+ /**
234
+ * Traverse all style elements in the DOM, enabling only those included
235
+ * in the passed list and disabling the others.
236
+ *
237
+ * If the style element has the `data-original-media` attribute, the
238
+ * original `media` value is restored.
239
+ *
240
+ * @param styles List of style elements to apply
241
+ */
189
242
export const applyStyles = ( styles : StyleElement [ ] ) => {
190
243
window . document
191
244
. querySelectorAll ( 'style,link[rel=stylesheet]' )
0 commit comments