1
- /* global exports, _, jQuery, OBJ, console, CSSFontFaceRule */
1
+ /* global exports, _, jQuery, OBJ, CSSFontFaceRule */
2
+ /* exported CSS */
2
3
3
4
var CSS = ( function ( ) {
4
- " use strict" ;
5
+ ' use strict' ;
5
6
var my = { } ,
6
7
$ = jQuery . noConflict ( ) ;
7
8
@@ -275,9 +276,9 @@ var CSS = (function () {
275
276
'overflow' : 'visible' ,
276
277
'overflow-x' : 'visible' ,
277
278
'overflow-y' : 'visible' ,
278
- 'padding' : node . prop ( " tagName" ) . match ( / [ U O ] L / ) ? '' : '0px' ,
279
+ 'padding' : node . prop ( ' tagName' ) . match ( / [ U O ] L / ) ? '' : '0px' ,
279
280
'padding-bottom' : '0px' ,
280
- 'padding-left' : node . prop ( " tagName" ) . match ( / [ U O ] L / ) ? '40px' : '0px' ,
281
+ 'padding-left' : node . prop ( ' tagName' ) . match ( / [ U O ] L / ) ? '40px' : '0px' ,
281
282
'padding-right' : '0px' ,
282
283
'padding-top' : '0px' ,
283
284
'page-break-after' : 'auto' ,
@@ -321,7 +322,7 @@ var CSS = (function () {
321
322
} ;
322
323
323
324
if ( node . data ( 'style' ) ) {
324
- if ( defaultDisplayForTag ( node . prop ( " tagName" ) ) === node . data ( 'style' ) . display ) {
325
+ if ( defaultDisplayForTag ( node . prop ( ' tagName' ) ) === node . data ( 'style' ) . display ) {
325
326
delete node . data ( 'style' ) . display ;
326
327
}
327
328
_ . each ( _ . keys ( defaults ) , function ( def ) {
@@ -339,7 +340,7 @@ var CSS = (function () {
339
340
function stripIrrelevantStyles ( node ) {
340
341
var style = node . data ( 'style' ) ;
341
342
if ( style ) {
342
- if ( node . prop ( " tagName" ) !== 'UL' && node . prop ( " tagName" ) !== 'OL' ) {
343
+ if ( node . prop ( ' tagName' ) !== 'UL' && node . prop ( ' tagName' ) !== 'OL' ) {
343
344
delete style [ 'list-style-type' ] ;
344
345
}
345
346
@@ -364,77 +365,63 @@ var CSS = (function () {
364
365
root . data ( 'style' , OBJ . difference ( root . data ( 'style' ) , defaults ) ) ;
365
366
}
366
367
367
- function selectorsUsed ( node , so_far ) {
368
- var tag = node . prop ( " tagName" ) . toLowerCase ( ) ;
368
+ function selectorsUsed ( node , soFar ) {
369
+ var tag = node . prop ( ' tagName' ) . toLowerCase ( ) ;
369
370
if ( tag === 'head' || tag === 'script' ) {
370
371
return { } ;
371
372
}
372
- so_far = so_far || { } ;
373
- so_far [ tag ] = true ;
373
+ soFar = soFar || { } ;
374
+ soFar [ tag ] = true ;
374
375
if ( tag !== 'html' ) { // disregard classes on root element
375
376
if ( node . attr ( 'class' ) ) {
376
377
_ . each ( node . attr ( 'class' ) . split ( / \s + / ) , function ( klass ) {
377
378
if ( klass ) {
378
- so_far [ '.' + klass ] = true ;
379
- so_far [ node . prop ( " tagName" ) . toLowerCase ( ) + '.' + klass ] = true ;
379
+ soFar [ '.' + klass ] = true ;
380
+ soFar [ node . prop ( ' tagName' ) . toLowerCase ( ) + '.' + klass ] = true ;
380
381
}
381
382
} ) ;
382
383
}
383
384
if ( node . attr ( 'id' ) ) {
384
- so_far [ '#' + node . attr ( 'id' ) ] = true ;
385
+ soFar [ '#' + node . attr ( 'id' ) ] = true ;
385
386
}
386
387
}
387
388
node . children ( ) . each ( function ( ) {
388
- selectorsUsed ( $ ( this ) , so_far ) ;
389
+ selectorsUsed ( $ ( this ) , soFar ) ;
389
390
} ) ;
390
- return so_far ;
391
+ return soFar ;
391
392
}
392
393
393
- function fontsUsed ( node , so_far ) {
394
- var fonts = node . data ( 'style' ) [ 'font-family' ] ;
395
- so_far = so_far || { } ;
396
- if ( fonts ) {
397
- _ . each ( fonts . split ( ', ' ) , function ( font ) {
398
- so_far [ font ] = true ;
399
- } ) ;
400
- }
401
- node . children ( ) . each ( function ( ) {
402
- fontsUsed ( $ ( this ) , so_far ) ;
403
- } ) ;
404
- return so_far ;
405
- }
406
-
407
- function originatingSelectors ( node , max_depth ) {
408
- var escape_selector = function ( sel ) { return sel . replace ( / ( : ) / g, '\\$1' ) ; } ,
409
- selectors = _ . map ( _ . keys ( selectorsUsed ( node ) ) , escape_selector ) ,
410
- not_id = function ( selector ) { return selector [ 0 ] !== '#' ; } ,
411
- sub_selectors = _ . filter ( selectors , not_id ) ,
412
- build_selector = function ( tuple ) { return tuple . join ( ' ' ) ; } ,
413
- add_if_matches = function ( selector ) {
394
+ function originatingSelectors ( node , maxDepth ) {
395
+ var escapeSelector = function ( sel ) { return sel . replace ( / ( : ) / g, '\\$1' ) ; } ,
396
+ selectors = _ . map ( _ . keys ( selectorsUsed ( node ) ) , escapeSelector ) ,
397
+ notId = function ( selector ) { return selector [ 0 ] !== '#' ; } ,
398
+ subSelectors = _ . filter ( selectors , notId ) ,
399
+ buildSelector = function ( tuple ) { return tuple . join ( ' ' ) ; } ,
400
+ addIfMatches = function ( selector ) {
414
401
if ( node . find ( selector ) . length ) {
415
402
selectors . push ( selector ) ;
416
403
}
417
404
} ;
418
405
419
- while ( max_depth > 1 ) {
406
+ while ( maxDepth > 1 ) {
420
407
_ . each (
421
408
_ . map (
422
- OBJ . cartesianProduct ( selectors , sub_selectors ) ,
423
- build_selector
409
+ OBJ . cartesianProduct ( selectors , subSelectors ) ,
410
+ buildSelector
424
411
) ,
425
- add_if_matches
412
+ addIfMatches
426
413
) ;
427
- max_depth -= 1 ;
414
+ maxDepth -= 1 ;
428
415
}
429
416
return selectors ;
430
417
}
431
418
432
419
function importance ( root , choice ) {
433
420
var number = root . andSelf ( ) . find ( choice . selector ) . length ,
434
421
size = _ . keys ( choice . style ) . length ,
435
- selector_length = ( choice . selector . match ( / / ) || [ 1 ] ) . length ,
422
+ selectorLength = ( choice . selector . match ( / / ) || [ 1 ] ) . length ,
436
423
result = - ( number * number * size ) ;
437
- if ( selector_length > 1 ) {
424
+ if ( selectorLength > 1 ) {
438
425
result += 1 ; // favor shallow selectors
439
426
}
440
427
return result ;
@@ -490,6 +477,20 @@ var CSS = (function () {
490
477
return style ;
491
478
}
492
479
480
+ my . fontsUsed = function ( node , soFar ) {
481
+ var fonts = node . data ( 'style' ) [ 'font-family' ] ;
482
+ soFar = soFar || { } ;
483
+ if ( fonts ) {
484
+ _ . each ( fonts . split ( ', ' ) , function ( font ) {
485
+ soFar [ font ] = true ;
486
+ } ) ;
487
+ }
488
+ node . children ( ) . each ( function ( ) {
489
+ my . fontsUsed ( $ ( this ) , soFar ) ;
490
+ } ) ;
491
+ return soFar ;
492
+ } ;
493
+
493
494
my . fontDeclarations = function ( ) {
494
495
$ ( 'html' ) . find ( '*' ) . andSelf ( ) . each ( function ( i , elt ) {
495
496
$ ( elt ) . data ( 'style' , abbreviate ( computedCssProperties ( elt ) ) ) ;
@@ -555,11 +556,11 @@ var CSS = (function () {
555
556
my . renderStyle = function ( selector , properties ) {
556
557
var css = '' ;
557
558
if ( ! _ . isEmpty ( properties ) ) {
558
- css += ( selector + " {\n" ) ;
559
+ css += ( selector + ' {\n' ) ;
559
560
_ . each ( properties , function ( val , key ) {
560
- css += ( "\t" + key + ': ' + val + " ;\n" ) ;
561
+ css += ( '\t' + key + ': ' + val + ' ;\n' ) ;
561
562
} ) ;
562
- css += " }\n" ;
563
+ css += ' }\n' ;
563
564
}
564
565
return css ;
565
566
} ;
0 commit comments