@@ -198,79 +198,80 @@ <h2 id="result-header"></h2>
198
198
}
199
199
200
200
function validate ( ) {
201
- var css = sourceInput . value . trim ( ) ;
202
- var parseErrors = [ ] ;
203
- var ast ;
201
+ const css = sourceInput . value ;
202
+ const noContent = css . trim ( ) . length === 0 ;
203
+ const parseErrors = [ ] ;
204
+ let ast ;
204
205
205
- document . getElementById ( 'intro' ) . style . display = css ? 'none ' : '' ;
206
- document . getElementById ( 'result' ) . style . display = css ? '' : 'none ' ;
206
+ document . getElementById ( 'intro' ) . style . display = noContent ? '' : 'none ' ;
207
+ document . getElementById ( 'result' ) . style . display = noContent ? 'none ' : '' ;
207
208
fillIssueBlock . style . display = 'none' ;
208
209
wellDoneBlock . style . display = 'none' ;
209
210
210
- if ( ! css ) {
211
+ if ( noContent ) {
211
212
return ;
212
213
}
213
214
214
215
try {
215
216
ast = csstree . parse ( css , {
216
217
positions : true ,
217
- tolerant : true ,
218
- onParseError : function ( e , fallbackNode ) {
218
+ onParseError ( e , fallbackNode ) {
219
219
parseErrors . push ( e ) ;
220
220
}
221
221
} ) ;
222
222
} catch ( e ) {
223
223
result . classList . add ( 'error' ) ;
224
224
resultHeader . innerHTML = 'Ooops, I can\'t parse your CSS:' ;
225
225
resultBlock . innerHTML = escapeHtml ( e . formattedMessage || e . message )
226
- . replace ( / ^ ( P a r s e e r r o r : ) ( [ ^ \r \n ] * ) / , function ( m , prefix , message ) {
227
- return prefix + createSourceLink ( message , e . offset ) ;
228
- } ) ;
226
+ . replace ( / ^ ( P a r s e e r r o r : ) ( [ ^ \r \n ] * ) / , ( m , prefix , message ) =>
227
+ prefix + createSourceLink ( message , e . offset )
228
+ ) ;
229
229
return ;
230
230
}
231
231
232
232
result . classList . remove ( 'error' ) ;
233
233
resultHeader . innerHTML = 'OK, that\'s what I know about your CSS:' ;
234
234
235
- var declCount = 0 ;
236
- var uniqueDecls = { } ;
237
- var decls = [ ] ;
238
- var props = { } ;
239
- var errors = { } ;
240
- var fails = 0 ;
235
+ const uniqueDecls = new Map ( ) ;
236
+ const decls = [ ] ;
237
+ const props = new Set ( ) ;
238
+ const errors = Object . create ( null ) ;
239
+ let declCount = 0 ;
241
240
242
241
csstree . walk ( ast , function ( node ) {
243
242
if ( node . type === 'Declaration' ) {
243
+ const id = csstree . generate ( node ) ;
244
+
244
245
declCount ++ ;
245
246
246
- var id = csstree . generate ( node ) ;
247
- if ( id in uniqueDecls ) {
248
- uniqueDecls [ id ] ++ ;
247
+ if ( uniqueDecls . has ( id ) ) {
248
+ uniqueDecls . set ( id , uniqueDecls . get ( id ) + 1 ) ;
249
249
return ;
250
250
}
251
251
252
252
node . value . important = false ;
253
- uniqueDecls [ id ] = 1 ;
253
+ props . add ( node . property ) ;
254
+ uniqueDecls . set ( id , 1 ) ;
254
255
decls . push ( {
255
- id : id ,
256
- node : node ,
256
+ id,
257
+ node,
257
258
property : node . property ,
258
259
value : node . value
259
260
} ) ;
260
- props [ node . property ] = true ;
261
261
}
262
262
} ) ;
263
263
264
- var syntax = csstree . lexer ;
265
- var warns = [ ] ;
266
- decls . forEach ( function ( decl ) {
267
- var node = decl . value ;
268
- var match = syntax . matchProperty ( decl . property , node ) ;
269
- var error = match . error ;
264
+ const syntax = csstree . lexer ;
265
+ const warns = [ ] ;
266
+
267
+ for ( const decl of decls ) {
268
+ const node = decl . value ;
269
+ const match = syntax . matchProperty ( decl . property , node ) ;
270
+ const error = match . error ;
270
271
271
272
if ( error !== null ) {
272
- var type = error . rawMessage || error . message ;
273
- var message = error . message ;
273
+ let type = error . rawMessage || error . message ;
274
+ let message = error . message ;
274
275
275
276
if ( / ^ U n k n o w n p r o p e r t y / . test ( message ) ) {
276
277
type = 'Unknown property' ;
@@ -284,16 +285,15 @@ <h2 id="result-header"></h2>
284
285
285
286
errors [ type ] = ( errors [ type ] || 0 ) + 1 ;
286
287
warns . push ( {
287
- decl : decl ,
288
- message : message
288
+ decl,
289
+ message
289
290
} ) ;
290
291
}
291
- } ) ;
292
- var fails = warns . length ;
293
- var uniqueDeclCount = decls . length ;
294
- var maxErrorCountLength = String ( Math . max . apply ( null , Object . keys ( errors ) . map ( function ( k ) {
295
- return errors [ k ] ;
296
- } ) ) ) . length ;
292
+ }
293
+
294
+ const fails = warns . length ;
295
+ const uniqueDeclCount = decls . length ;
296
+ const maxErrorCountLength = String ( Math . max ( ...Object . values ( errors ) ) ) . length ;
297
297
298
298
// clean results
299
299
resultBlock . innerHTML = '' ;
@@ -308,7 +308,7 @@ <h2 id="result-header"></h2>
308
308
} ) ;
309
309
310
310
// warn summary
311
- var summaryBlock = document . createElement ( 'div' ) ;
311
+ const summaryBlock = document . createElement ( 'div' ) ;
312
312
summaryBlock . innerHTML = [
313
313
'Unique properties: ' + Object . keys ( props ) . length ,
314
314
'Unique declarations: ' + uniqueDeclCount + ' (total: ' + declCount + ')' ,
@@ -322,14 +322,16 @@ <h2 id="result-header"></h2>
322
322
323
323
// warn list
324
324
warns . forEach ( function ( warn ) {
325
- var warnBlock = document . createElement ( 'div' ) ;
326
- var count = uniqueDecls [ warn . decl . id ] ;
325
+ const warnBlock = document . createElement ( 'div' ) ;
326
+ const count = uniqueDecls . get ( warn . decl . id ) ;
327
+
327
328
warnBlock . innerHTML = escapeHtml ( warn . message )
328
329
. replace ( / ^ [ ^ \r \n ] + / , '<span class="warn-type">$&</span>' + ( count > 1 ? ' \u00d7 ' + count : '' ) )
329
330
. replace ( / v a l u e ( : [ ^ \r \n ] * ) / , createSourceLink ( warn . decl . property , warn . decl . node . loc . start . offset ) + '$1' )
330
331
. replace ( / ( U n k n o w n p r o p e r t y : ) \S + / , '$1' + createSourceLink ( warn . decl . property , warn . decl . node . loc . start . offset ) )
331
332
. replace ( '------^' , repeat ( '-' , warn . decl . property . length ) + '^' ) ;
332
333
warnBlock . className = 'warn' ;
334
+
333
335
resultBlock . appendChild ( warnBlock ) ;
334
336
} ) ;
335
337
@@ -340,26 +342,27 @@ <h2 id="result-header"></h2>
340
342
}
341
343
}
342
344
343
- var sourceInput = document . getElementById ( 'source' ) ;
344
- var resultBlock = document . getElementById ( 'result-output' ) ;
345
- var resultHeader = document . getElementById ( 'result-header' ) ;
346
- var fillIssueBlock = document . getElementById ( 'fill-issue' ) ;
347
- var wellDoneBlock = document . getElementById ( 'well-done' ) ;
345
+ const sourceInput = document . getElementById ( 'source' ) ;
346
+ const resultBlock = document . getElementById ( 'result-output' ) ;
347
+ const resultHeader = document . getElementById ( 'result-header' ) ;
348
+ const fillIssueBlock = document . getElementById ( 'fill-issue' ) ;
349
+ const wellDoneBlock = document . getElementById ( 'well-done' ) ;
348
350
349
351
sourceInput . addEventListener ( 'input' , validate , false ) ;
350
- resultBlock . addEventListener ( 'click' , function ( e ) {
352
+ resultBlock . addEventListener ( 'click' , ( e ) => {
351
353
if ( e . target . classList . contains ( 'link-to-source' ) ) {
352
354
if ( sourceInput . setSelectionRange ) {
353
355
sourceInput . setSelectionRange ( e . target . dataset . offset , e . target . dataset . offset ) ;
354
356
}
355
357
sourceInput . focus ( ) ;
356
358
}
357
359
} , false ) ;
360
+
358
361
validate ( ) ;
359
362
360
363
if ( window . FileReader && window . DataTransfer ) {
361
364
sourceInput . placeholder += ' or drop CSS file' ;
362
- document . addEventListener ( 'dragover' , function ( e ) {
365
+ document . addEventListener ( 'dragover' , ( e ) => {
363
366
if ( e . dataTransfer . items && e . dataTransfer . items [ 0 ] ) {
364
367
if ( e . dataTransfer . items [ 0 ] . kind == 'file' ) {
365
368
e . preventDefault ( ) ;
@@ -371,14 +374,14 @@ <h2 id="result-header"></h2>
371
374
if ( e . dataTransfer . items [ 0 ] . kind == 'file' ) {
372
375
e . preventDefault ( ) ;
373
376
374
- var file = e . dataTransfer . items [ 0 ] . getAsFile ( ) ;
377
+ const file = e . dataTransfer . items [ 0 ] . getAsFile ( ) ;
375
378
376
379
if ( ! / \. c s s $ / . test ( file . name ) ) {
377
380
alert ( 'Only CSS files may be loaded.' ) ;
378
381
return ;
379
382
}
380
383
381
- var reader = new FileReader ( ) ;
384
+ const reader = new FileReader ( ) ;
382
385
383
386
reader . readAsText ( file ) ;
384
387
reader . addEventListener ( 'load' , function ( ) {
@@ -390,7 +393,7 @@ <h2 id="result-header"></h2>
390
393
} ) ;
391
394
document . addEventListener ( 'dragend' , function ( e ) {
392
395
if ( e . dataTransfer . items ) {
393
- for ( var i = 0 ; i < e . dataTransfer . items . length ; i ++ ) {
396
+ for ( let i = 0 ; i < e . dataTransfer . items . length ; i ++ ) {
394
397
e . dataTransfer . items . remove ( i ) ;
395
398
}
396
399
} else {
0 commit comments