
Zhris
Enthusiast
Aug 8, 2016, 8:37 AM
Post #27 of 28
(2594 views)
|
Re: [mohan] Script to find unused CSS styles
[In reply to]
|
Can't Post
|
|
In case its useful to you, here is a slightly modified version, which can also be run at http://demo.massweb.co.uk/css/cssfind/cssfind.pl. Most importantly, it processes / prints the output data structure in a human readable format.
use strict; use warnings; use CSS; use CSS::Selector::Parser 'parse_selector'; use HTML::Selector::XPath 'selector_to_xpath'; use HTML::TreeBuilder::XPath; use Data::Dumper; # input my $input = { filepaths_css => [ glob( "*.css" ) ], filepaths_html => [ glob( "*.html" ) ], properties => [ qw/margin width/ ], }; # output my $output = { }; # iterate over each css file for my $filepath_css ( @{$input->{filepaths_css}} ) { # create new css object and populate it my $css = CSS->new; $css->read_file( $filepath_css ); # iterate over each css block for my $style ( @{$css->{styles}} ) { # create list of properties that match those we want my $properties = [ grep { $style->get_property_by_name( $_ ) } @{$input->{properties}} ]; # next iteration if list of properties that match is empty next unless @$properties; # iterate over each selector for my $selector ( map { $_->{name} } @{$style->{selectors}} ) { # if we have not seen this selector before, populate output with selector and xpath unless ( exists $output->{$selector} ) { $output->{$selector}->{selector} = eval { ( parse_selector( $selector ) )[0] }; $output->{$selector}->{xpath} = eval { selector_to_xpath( $selector ) }; } # populate output with property and css counts $output->{$selector}->{properties}->{$_}++ for @$properties; $output->{$selector}->{css}->{$filepath_css}++; } } } # iterate over each html file for my $filepath_html ( @{$input->{filepaths_html}} ) { # create new tree object and populate it my $tree = HTML::TreeBuilder::XPath->new_from_file( $filepath_html ); # iterate over each selector for my $selector ( grep { defined $output->{$_}->{xpath} } keys %$output ) { # populate output with html counts my $count = $tree->findnodes( $output->{$selector}->{xpath} )->size; $output->{$selector}->{html}->{$filepath_html} += $count if $count; } } # output local $, = "\n"; local $\ = "\n"; for my $selector ( sort keys %$output ) { my $ref = $output->{$selector}; print "report for selector $selector"; ########## print "\tclass:"; if ( exists $ref->{selector}->[-1]->{class} ) { if ( @{$ref->{selector}} > 1 ) { print "\t\tthis selector is an indirect class selector that refers to the class $ref->{selector}->[-1]->{class}"; } else { print "\t\tthis selector is a direct class selector that refers to the class $ref->{selector}->[-1]->{class}"; } } else { print "\t\tthis selector is not a class selector"; } ########## print "\tproperties:"; while ( my ( $property, $count ) = each %{$ref->{properties}} ) { print "\t\tthe property $property is referenced $count times across all css files"; } ########## print "\tcss:"; while ( my ( $filepath_css, $count ) = each %{$ref->{css}} ) { print "\t\tthis selector is referenced $count times in the css file $filepath_css"; } ########## print "\thtml:"; if ( defined $ref->{xpath} ) { if ( keys %{$ref->{html}} ) { while ( my ( $filepath_html, $count ) = each %{$ref->{html}} ) { print "\t\tthis selector is referenced $count times in the html file $filepath_html"; } } else { print "\t\tthis selector is not referenced in any of the html files"; } } else { print "\t\tthis selector could not be converted to an xpath, therefore we were unable to check if its referenced across any html files"; } ########## print ''; }
(This post was edited by Zhris on Aug 8, 2016, 8:47 AM)
|