@@ -130,7 +130,9 @@ const parseStyleDeclarations = (css) => {
130
130
} ;
131
131
132
132
/**
133
- * @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles }
133
+ * @param {Stylesheet } stylesheet
134
+ * @param {XastElement } node
135
+ * @returns {ComputedStyles }
134
136
*/
135
137
const computeOwnStyle = ( stylesheet , node ) => {
136
138
/** @type {ComputedStyles } */
@@ -139,7 +141,7 @@ const computeOwnStyle = (stylesheet, node) => {
139
141
140
142
// collect attributes
141
143
for ( const [ name , value ] of Object . entries ( node . attributes ) ) {
142
- if ( attrsGroups . presentation . includes ( name ) ) {
144
+ if ( attrsGroups . presentation . has ( name ) ) {
143
145
computedStyle [ name ] = { type : 'static' , inherited : false , value } ;
144
146
importantStyles . set ( name , false ) ;
145
147
}
@@ -217,29 +219,31 @@ exports.compareSpecificity = compareSpecificity;
217
219
* @type {(root: XastRoot) => Stylesheet }
218
220
*/
219
221
const collectStylesheet = ( root ) => {
220
- /** @type {Array< StylesheetRule> } */
222
+ /** @type {StylesheetRule[] } */
221
223
const rules = [ ] ;
222
224
/** @type {Map<XastElement, XastParent> } */
223
225
const parents = new Map ( ) ;
226
+
224
227
visit ( root , {
225
228
element : {
226
229
enter : ( node , parentNode ) => {
227
- // store parents
228
230
parents . set ( node , parentNode ) ;
229
- // find and parse all styles
230
- if ( node . name === 'style' ) {
231
+
232
+ if ( node . name !== 'style' ) {
233
+ return ;
234
+ }
235
+
236
+ if (
237
+ node . attributes . type == null ||
238
+ node . attributes . type === '' ||
239
+ node . attributes . type === 'text/css'
240
+ ) {
231
241
const dynamic =
232
242
node . attributes . media != null && node . attributes . media !== 'all' ;
233
- if (
234
- node . attributes . type == null ||
235
- node . attributes . type === '' ||
236
- node . attributes . type === 'text/css'
237
- ) {
238
- const children = node . children ;
239
- for ( const child of children ) {
240
- if ( child . type === 'text' || child . type === 'cdata' ) {
241
- rules . push ( ...parseStylesheet ( child . value , dynamic ) ) ;
242
- }
243
+
244
+ for ( const child of node . children ) {
245
+ if ( child . type === 'text' || child . type === 'cdata' ) {
246
+ rules . push ( ...parseStylesheet ( child . value , dynamic ) ) ;
243
247
}
244
248
}
245
249
}
@@ -253,21 +257,21 @@ const collectStylesheet = (root) => {
253
257
exports . collectStylesheet = collectStylesheet ;
254
258
255
259
/**
256
- * @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles }
260
+ * @param {Stylesheet } stylesheet
261
+ * @param {XastElement } node
262
+ * @returns {ComputedStyles }
257
263
*/
258
264
const computeStyle = ( stylesheet , node ) => {
259
265
const { parents } = stylesheet ;
260
- // collect inherited styles
261
266
const computedStyles = computeOwnStyle ( stylesheet , node ) ;
262
267
let parent = parents . get ( node ) ;
263
268
while ( parent != null && parent . type !== 'root' ) {
264
269
const inheritedStyles = computeOwnStyle ( stylesheet , parent ) ;
265
270
for ( const [ name , computed ] of Object . entries ( inheritedStyles ) ) {
266
271
if (
267
272
computedStyles [ name ] == null &&
268
- // ignore not inheritable styles
269
- inheritableAttrs . includes ( name ) === true &&
270
- presentationNonInheritableGroupAttrs . includes ( name ) === false
273
+ inheritableAttrs . has ( name ) &&
274
+ ! presentationNonInheritableGroupAttrs . has ( name )
271
275
) {
272
276
computedStyles [ name ] = { ...computed , inherited : true } ;
273
277
}
0 commit comments